Skip to content

Commit a18f3df

Browse files
committed
0080. Remove Duplicates from Sorted Array II
1 parent 4a85faa commit a18f3df

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### [80\. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a sorted array _nums_, remove the duplicates such that duplicates appeared at most _twice_ and return the new length.
7+
8+
Do not allocate extra space for another array, you must do this by **modifying the input array** with O(1) extra memory.
9+
10+
**Example 1:**
11+
12+
```
13+
Given nums = [1,1,1,2,2,3],
14+
15+
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
16+
17+
It doesn't matter what you leave beyond the returned length.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Given nums = [0,0,1,1,1,1,2,3,3],
24+
25+
Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
26+
27+
It doesn't matter what values are set beyond the returned length.
28+
```
29+
30+
**Clarification:**
31+
32+
Confused why the returned value is an integer but your answer is an array?
33+
34+
Note that the input array is passed in by **reference**, which means modification to the input array will be known to the caller as well.
35+
36+
Internally you can think of this:
37+
38+
```
39+
// nums is passed in by reference. (i.e., without making a copy)
40+
int len = removeDuplicates(nums);
41+
42+
// any modification to nums in your function would be known by the caller.
43+
// using the length returned by your function, it prints the first len elements.
44+
for (int i = 0; i < len; i++) {
45+
    print(nums[i]);
46+
}
47+
```
48+
49+
50+
#### Solution
51+
52+
Language: **Java**
53+
54+
```java
55+
class Solution {
56+
   public int removeDuplicates(int[] nums) {
57+
       int i = 0;
58+
       for (int num : nums) {
59+
           if (i < 2 || num > nums[i - 2]) {
60+
               nums[i++] = num;
61+
          }
62+
      }
63+
       return i;
64+
  }
65+
}
66+
```
67+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-26-Dsh2ou.jpg)

src/main/java/leetcode/_80_/Main.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leetcode._80_;
2+
3+
import leetcode.common.Printer;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
int[] nums = {1, 1, 1, 2, 2, 3};
12+
System.out.println(solution.removeDuplicates(nums));
13+
Printer.printArrays(nums);
14+
15+
16+
int[] nums1 = {0, 0, 1, 1, 1, 1, 2, 3, 3};
17+
System.out.println(solution.removeDuplicates(nums1));
18+
Printer.printArrays(nums1);
19+
20+
}
21+
}
22+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._80_;
2+
3+
class Solution { // 当看到这个解法的时候,我只能说一句:真 tmd 牛逼
4+
public int removeDuplicates(int[] nums) {
5+
int i = 0;
6+
for (int num : nums) {
7+
if (i < 2 || num > nums[i - 2]) {
8+
nums[i++] = num;
9+
}
10+
}
11+
return i;
12+
}
13+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
### [80\. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a sorted array _nums_, remove the duplicates such that duplicates appeared at most _twice_ and return the new length.
7+
8+
Do not allocate extra space for another array, you must do this by **modifying the input array** with O(1) extra memory.
9+
10+
**Example 1:**
11+
12+
```
13+
Given nums = [1,1,1,2,2,3],
14+
15+
Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively.
16+
17+
It doesn't matter what you leave beyond the returned length.
18+
```
19+
20+
**Example 2:**
21+
22+
```
23+
Given nums = [0,0,1,1,1,1,2,3,3],
24+
25+
Your function should return length = 7, with the first seven elements of nums being modified to 0, 0, 1, 1, 2, 3 and 3 respectively.
26+
27+
It doesn't matter what values are set beyond the returned length.
28+
```
29+
30+
**Clarification:**
31+
32+
Confused why the returned value is an integer but your answer is an array?
33+
34+
Note that the input array is passed in by **reference**, which means modification to the input array will be known to the caller as well.
35+
36+
Internally you can think of this:
37+
38+
```
39+
// nums is passed in by reference. (i.e., without making a copy)
40+
int len = removeDuplicates(nums);
41+
42+
// any modification to nums in your function would be known by the caller.
43+
// using the length returned by your function, it prints the first len elements.
44+
for (int i = 0; i < len; i++) {
45+
    print(nums[i]);
46+
}
47+
```
48+
49+
50+
#### Solution
51+
52+
Language: **Java**
53+
54+
```java
55+
class Solution {
56+
   public int removeDuplicates(int[] nums) {
57+
       int i = 0;
58+
       for (int num : nums) {
59+
           if (i < 2 || num > nums[i - 2]) {
60+
               nums[i++] = num;
61+
          }
62+
      }
63+
       return i;
64+
  }
65+
}
66+
```
67+
![pic](https://raw.githubusercontent.com/PicGoBed/PicBed/master/2019-07-26-Dsh2ou.jpg)

src/main/java/leetcode/common/Printer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public static void printArrays(int[] arrays) {
1414
System.out.print(arrays[i] + ",");
1515
}
1616
System.out.print(arrays[arrays.length - 1] + "]");
17+
System.out.println();
1718
}
1819

1920
public static void printArrays(int[][] arrays) {

0 commit comments

Comments
 (0)