-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram2.java
More file actions
51 lines (36 loc) · 1.21 KB
/
Program2.java
File metadata and controls
51 lines (36 loc) · 1.21 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
46
47
48
49
50
51
/*
80. Remove Duplicates from Sorted Array II
Example 1:
Input: nums = [1,1,1,2,2,3]
Output: 5, nums = [1,1,2,2,3,_]
Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
Example 2:
Input: nums = [0,0,1,1,1,1,2,3,3]
Output: 7, nums = [0,0,1,1,2,3,3,_,_]
Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively.
It does not matter what you leave beyond the returned k (hence they are underscores).
*/
package LeetCode;
public class Program2 {
public static void main(String[] args) {
int nums[] = {0,0,1,1,1,1,2,3,3,3};
int cnt=0;
int ind=1;
for(int i=1;i<nums.length;i++){
if(nums[i]==nums[i-1]){
cnt++;
}
else{
cnt=0;
}
if(cnt <=1){
nums[ind] = nums[i];
ind++;
}
}
for(int i=0;i<nums.length;i++){
System.out.println(nums[i]+" ");
}
}
}