Skip to content

Commit

Permalink
update 26 java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 3, 2024
1 parent 622cded commit 7b7b5ab
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 33 deletions.
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20241103: 26
20241102: 802,1197,26
20241027: 855,846
20241026: 932
Expand Down
16 changes: 9 additions & 7 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2024-12-28 -> ['26']
2024-12-27 -> ['802,1197,26']
2024-12-21 -> ['855,846']
2024-12-20 -> ['932']
Expand All @@ -6,7 +7,7 @@
2024-12-13 -> ['298,729']
2024-12-12 -> ['1146']
2024-12-08 -> ['737']
2024-12-07 -> ['686,734,737']
2024-12-07 -> ['26', '686,734,737']
2024-12-06 -> ['802,1197,26', '353']
2024-12-05 -> ['528,334']
2024-12-03 -> ['1145']
Expand All @@ -15,24 +16,25 @@
2024-11-27 -> ['951,792', '524,221,889']
2024-11-26 -> ['743,889']
2024-11-25 -> ['837']
2024-11-24 -> ['26']
2024-11-23 -> ['802,1197,26', '163,1048', '981']
2024-11-22 -> ['298,729', '1087']
2024-11-21 -> ['1146']
2024-11-20 -> ['939']
2024-11-18 -> ['430']
2024-11-17 -> ['855,846', '737', '363']
2024-11-16 -> ['932', '686,734,737', '1032,844,1011']
2024-11-16 -> ['26', '932', '686,734,737', '1032,844,1011']
2024-11-15 -> ['802,1197,26', '353', '947']
2024-11-14 -> ['951,792', '528,334']
2024-11-12 -> ['1145', '753']
2024-11-11 -> ['727']
2024-11-11 -> ['26', '727']
2024-11-10 -> ['802,1197,26', '163,1048']
2024-11-09 -> ['855,846', '298,729', '1145,1219']
2024-11-08 -> ['932', '1146']
2024-11-08 -> ['26', '932', '1146']
2024-11-07 -> ['802,1197,26']
2024-11-06 -> ['951,792', '524,221,889']
2024-11-05 -> ['802,1197,26', '743,889']
2024-11-04 -> ['802,1197,26', '855,846', '737', '837', '659']
2024-11-06 -> ['26', '951,792', '524,221,889']
2024-11-05 -> ['26', '802,1197,26', '743,889']
2024-11-04 -> ['26', '802,1197,26', '855,846', '737', '837', '659']
2024-11-03 -> ['802,1197,26', '932', '686,734,737', '801,552']
2024-11-02 -> ['163,1048', '353', '981', '1057,1066,1110']
2024-11-01 -> ['855,846', '951,792', '298,729', '528,334', '1087']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,85 @@ public class RemoveDuplicatesFromSortedArray {

// V0
// IDEA : 2 POINTERS
/**
* //--------------------------------
* Example 1
* //--------------------------------
*
* nums = [1,1,2]
*
* [1,1,2]
* s f
*
* [1,2, 1] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
*
* //--------------------------------
* Example 2
* //--------------------------------
*
* nums = [0,0,1,1,1,2,2,3,3,4]
*
* [0,0,1,1,1,2,2,3,3,4]
* s f
*
* [0,1,0,1,1,2,2,3,3,4] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
* [0,1,0,1,1,2,2,3,3,4]
* s f
*
* [0,1,0,1,1,2,2,3,3,4]
* s f
*
* [0,1,2,1,1,0,2,3,3,4] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
* [0,1,2,1,1,0,2,3,3,4]
* s f
*
* [0,1,2,3,1,0,2,1,3,4] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
* [0,1,2,3,1,0,2,1,3,4]
* s f
*
* [0,1,2,3,4,0,2,1,3,1] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
*/
public int removeDuplicates(int[] nums) {
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
/**
* NOTE !!! we init slow, fast pointers as s, f
* and use for loop go through over fast pointer (f)
*/
int s = 0;
for (int f = 1; f < nums.length; f++){
/**
* NOTE !!!
*
* we do swap when f NOT EQUALS s
*/
if (nums[f] != nums[s]){
/**
* NOTE !!!
*
* we move slow pointer first,
* then do swap
*/
s += 1;
int tmp = nums[f];
nums[f] = nums[s];
nums[s] = tmp;
}
}
return i + 1;
/**
* NOTE !!!
*
* we return s+1 as non-duplicated array length, since length = index + 1
*/
return s+1;
}

// V1
Expand Down
99 changes: 79 additions & 20 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -2250,8 +2250,67 @@ public int minKnightMoves(int x, int y) {

// LC 26
// https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
// 7.05 - 7.15 PM
// 8.31 am - 8.40 am
/**
* exp 1
*
* nums = [1,1,2]
*
* [1,1,2]
* s f
*
* [1,2, 1] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
*
* exp 2
*
* nums = [0,0,1,1,1,2,2,3,3,4]
*
* [0,0,1,1,1,2,2,3,3,4]
* s f
*
* [0,1,0,1,1,2,2,3,3,4] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
* [0,1,0,1,1,2,2,3,3,4]
* s f
*
* [0,1,0,1,1,2,2,3,3,4]
* s f
*
* [0,1,2,1,1,0,2,3,3,4] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
* [0,1,2,1,1,0,2,3,3,4]
* s f
*
* [0,1,2,3,1,0,2,1,3,4] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
* [0,1,2,3,1,0,2,1,3,4]
* s f
*
* [0,1,2,3,4,0,2,1,3,1] if nums[f] != nums[s], move s, then swap f, s
* s s f
*
*/
// 2 pointers
public int removeDuplicates(int[] nums) {
//
int s = 0;
for (int f = 1; f < nums.length; f++){
if (nums[f] != nums[s]){
//
s += 1;
int tmp = nums[f];
nums[f] = nums[s];
nums[s] = tmp;
}
}
//
return s+1;
}
/**
* - exp 1
*
Expand Down Expand Up @@ -2309,25 +2368,25 @@ public int minKnightMoves(int x, int y) {
*
*
*/
public int removeDuplicates(int[] nums) {
if (nums.length==0){
return 0;
}
int s = 0;
int f = 1;

while (f < nums.length){
if (nums[s] == nums[f]){

s += 1;
int tmp = nums[f];
nums[f] = nums[s];
nums[s] = tmp;
}
f += 1;
}
return s;
}
// public int removeDuplicates(int[] nums) {
// if (nums.length==0){
// return 0;
// }
// int s = 0;
// int f = 1;
//
// while (f < nums.length){
// if (nums[s] == nums[f]){
//
// s += 1;
// int tmp = nums[f];
// nums[f] = nums[s];
// nums[s] = tmp;
// }
// f += 1;
// }
// return s;
// }

}

Expand Down

0 comments on commit 7b7b5ab

Please sign in to comment.