-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay_17.java
More file actions
45 lines (44 loc) · 1.11 KB
/
Copy pathDay_17.java
File metadata and controls
45 lines (44 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Remove Duplicates from Sorted Array
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if(n == 1) return 1;
int i = 0;
for(int j=i+1;j<n;j++){
if(nums[i]!=nums[j]){
i++;
nums[i] = nums[j];
}
}
return i+1;
}
}
// Remove Duplicates from Sorted Array II
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if(n == 1 || n == 2) return n;
int i = 0, cnt = 1;
for(int j=i+1;j<n;j++){
if(nums[i] == nums[j]) cnt++;
else if(nums[i] != nums[j]){
if(cnt == 1){
i++;
nums[i] = nums[j];
}
else if(cnt>=2){
int val = nums[i];
i++;
nums[i++] = val;
nums[i] = nums[j];
cnt = 1;
}
}
}
if(cnt != 1){
i++;
nums[i] = nums[n-1];
}
return i+1;
}
}