|
2 | 2 | // Author : Hao Chen
|
3 | 3 | // Date : 2014-06-22
|
4 | 4 |
|
5 |
| -/********************************************************************************** |
6 |
| -* |
7 |
| -* Given a sorted array, remove the duplicates in place such that each element appear |
8 |
| -* only once and return the new length. |
9 |
| -* |
10 |
| -* Do not allocate extra space for another array, you must do this in place with constant memory. |
11 |
| -* |
12 |
| -* For example, |
13 |
| -* Given input array A = [1,1,2], |
14 |
| -* |
15 |
| -* Your function should return length = 2, and A is now [1,2]. |
16 |
| -* |
17 |
| -* |
18 |
| -**********************************************************************************/ |
| 5 | +/********************************************************************************** |
| 6 | + * |
| 7 | + * Given a sorted array, remove the duplicates in place such that each element appear |
| 8 | + * only once and return the new length. |
| 9 | + * |
| 10 | + * Do not allocate extra space for another array, you must do this in place with constant memory. |
| 11 | + * |
| 12 | + * For example, |
| 13 | + * Given input array A = [1,1,2], |
| 14 | + * |
| 15 | + * Your function should return length = 2, and A is now [1,2]. |
| 16 | + * |
| 17 | + * |
| 18 | + **********************************************************************************/ |
19 | 19 | // my implm
|
| 20 | +// 双指针 |
20 | 21 | class Solution {
|
21 |
| -public: |
22 |
| - int removeDuplicates(vector<int>& nums) { |
23 |
| - if(nums.empty()) return 0; |
24 |
| - int i = 0; //输出结果index |
25 |
| - for(int j = 1; j < nums.size(); ++j){ |
26 |
| - if(nums[i] == nums[j]) continue; |
27 |
| - else nums[++i] = nums[j]; |
28 |
| - } |
29 |
| - return i+1; |
| 22 | + public: |
| 23 | + int removeDuplicates(vector<int>& nums) { |
| 24 | + if (nums.empty()) { |
| 25 | + return 0; |
30 | 26 | }
|
| 27 | + int i = 0; //输出结果index |
| 28 | + for (int j = 1; j < nums.size(); ++j) { |
| 29 | + if (nums[i] == nums[j]) { |
| 30 | + continue; |
| 31 | + } else { |
| 32 | + nums[++i] = nums[j]; |
| 33 | + } |
| 34 | + } |
| 35 | + return i + 1; |
| 36 | + } |
31 | 37 | };
|
32 | 38 |
|
| 39 | +// 变种:如果数组未排序,输出结果数组。[1,3,4,2,2,2,1,5] ->[1, 3, 4, 2, 5] |
| 40 | +// 哈希+双指针 |
| 41 | +void removeDuplicates(vector<int>& nums) { |
| 42 | + unordered_set<int> st; |
| 43 | + |
| 44 | + int i = 0; |
| 45 | + for (int j = 0; j < nums.size(); ++j) { |
| 46 | + if (st.find(nums[j]) != st.end()) { |
| 47 | + continue; |
| 48 | + } else { |
| 49 | + st.insert(nums[j]); |
| 50 | + nums[i++] = nums[j]; |
| 51 | + } |
| 52 | + } |
| 53 | + nums.erase(nums.begin() + i, nums.end()); |
| 54 | + // for (const auto &x : nums) { |
| 55 | + // cout << x << ", "; |
| 56 | + // } |
| 57 | +} |
| 58 | + |
33 | 59 | class Solution {
|
34 |
| -public: |
35 |
| - int removeDuplicates(int A[], int n) { |
36 |
| - |
37 |
| - if (n<=1) return n; |
38 |
| - |
39 |
| - int pos=0; |
40 |
| - for(int i=0; i<n-1; i++){ |
41 |
| - if (A[i]!=A[i+1]){ |
42 |
| - A[++pos] = A[i+1]; |
43 |
| - } |
44 |
| - } |
45 |
| - return pos+1; |
| 60 | + public: |
| 61 | + int removeDuplicates(int A[], int n) { |
| 62 | + if (n <= 1) return n; |
| 63 | + |
| 64 | + int pos = 0; |
| 65 | + for (int i = 0; i < n - 1; i++) { |
| 66 | + if (A[i] != A[i + 1]) { |
| 67 | + A[++pos] = A[i + 1]; |
| 68 | + } |
46 | 69 | }
|
| 70 | + return pos + 1; |
| 71 | + } |
47 | 72 | };
|
0 commit comments