Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit701823b

Browse files
EASY/src/easy/RemoveDuplicatesFromSortedArray.java
1 parent8d9bc47 commit701823b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packageeasy;
2+
3+
publicclassRemoveDuplicatesFromSortedArray {
4+
5+
/**Same idea as the editorial solution, mine just got more verbose.*/
6+
publicstaticintremoveDuplicates_my_original(int[]nums) {
7+
inti =0;
8+
for(intj =i+1;i <nums.length &&j <nums.length;){
9+
while(j <nums.length &&nums[i] ==nums[j]){
10+
j++;
11+
}
12+
if(j ==nums.length)j--;
13+
inttemp =nums[j];
14+
nums[j] =nums[i+1];
15+
nums[i+1] =temp;
16+
if(nums[i] !=nums[i+1])i++;
17+
if(j ==nums.length)break;
18+
j++;
19+
}
20+
returni+1;
21+
}
22+
23+
24+
publicintremoveDuplicates_editorial_solution(int[]nums) {
25+
inti =0;
26+
for(intj =1;j <nums.length;j++){
27+
if(nums[i] !=nums[j]){
28+
i++;
29+
nums[i] =nums[j];
30+
}
31+
}
32+
returni+1;
33+
}
34+
35+
36+
publicstaticvoidmain(String...strings){
37+
int[]nums =newint[]{1,1,2};
38+
// int[] nums = new int[]{1,1,2,2,3};
39+
// int[] nums = new int[]{1,1};
40+
System.out.println(removeDuplicates_my_original(nums));
41+
}
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp