-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path026_RemoveDuplicatesFromSortedArray26.java
85 lines (81 loc) · 1.75 KB
/
026_RemoveDuplicatesFromSortedArray26.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
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length == 0) return 0;
int n = nums.length, i = 0, j = 1, last = nums[0];
while(j < n) {
if(nums[j] != last) {
i++;
last = nums[j];
nums[i] = nums[j];
}
j++;
}
return i+1;
}
}
*******************************************************************
class Solution {
public int removeDuplicates(int[] nums) {
int startindex=1;
int n=nums.length;
for(int i=0;i<n-1;i++)
{
if(nums[i+1]==nums[i])
{
int a=nums[i];
while(i<n && nums[i]==a)
i++;
if(i<n){
nums[startindex]=nums[i];
startindex++;
i--;
}
else
break;
}
else
{
nums[startindex]=nums[i+1];
startindex++;
}
}
return startindex;
}
}
******************************************************************
class Solution {
public int removeDuplicates(int[] nums) {
int pointer=0;
int current=1;
for(int index=0;index<nums.length-1;index++)
{
if(nums[current]==nums[pointer])
{
current++;
}
else
{
pointer++;
nums[pointer]=nums[current];
current++;
}
}
return pointer+1;
}
}
*******************************************************************
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length<=1) return nums.length;
int curRepPos=1;
int i=0;
while(i<nums.length-1){
int j=i+1;
while(j<nums.length && nums[i]==nums[j]) j++;
if(j==nums.length) break;
nums[curRepPos++] = nums[j];
i=j;
}
return curRepPos;
}
}