-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRemoveDuplicatesFromSortedArray2.java
104 lines (86 loc) · 2.66 KB
/
RemoveDuplicatesFromSortedArray2.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/*/
class Solution {
public int removeDuplicates(int[] nums) {
int curr = 0;
boolean flag = true; //helps to store elements at most twice
for (int i = 1; i < nums.length; ++i)
{
//till ith element is equal to current element
while(i < nums.length && nums[i] == nums[curr])
{
//if flag is true
if (flag)
{
//increase current index
++curr;
//store ith element to current index
nums[curr] = nums[i];
//reverse flag to indicate that we have visited current element once
flag = false;
}
//if flag is false then this case resembles to the normal case
++i;
}
if (i < nums.length)
{
//store the next element to the right position
nums[curr+1] = nums[i];
//increase current index
++curr;
//reset flag
flag = true;
}
}
return curr+1;
}
}
/*Binary Seacrh*/
import java.util.*;
import java.util.*;
class Solution {
public int[] solve(int[] nums) {
if (nums.length == 0) return nums;
int i, j, k;
i = j = k = 0;
while (j < nums.length) //till all elements are parsed
{
if (nums[i] == nums[j]) ++j; //increase j till the end of the window
else
{
if (j-i >= 2) //if window size is at least 2, keep an element
nums[k++] = nums[i];
nums[k++] = nums[i]; //keep at least one element
i = j; //close the old window and start a new one
}
}
//last window operation
if (j-i >= 2)
nums[k++] = nums[i];
nums[k++] = nums[i];
//copy the result
int[] result = new int[k];
for (i = 0; i < k; ++i)
result[i] = nums[i];
nums = result;
return nums;
}
}
class Solution {
public int removeDuplicates(int[] nums) {
int n=nums.length;
if( nums==null || n==0) return 0;
int currNum=nums[0], count=1, left=1;
for(int i=1;i<n;i++){
if(currNum!=nums[i]){
nums[left++] = nums[i];
currNum = nums[i];
count=1;
continue;
}
if(count>=2) continue;
nums[left++] = nums[i];
count++;
}
return left;
}
}