Remove Duplicates from Sorted Array II

subject

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?
For example, Given sorted array A = [1, 1, 1, 2, 2, 3],
Your function should return length = 5, and A is now [1, 1, 2, 2, 3]

Then remove the repeated numbers in the ordered array. What should I do if I allow two repetitions?

For example, give you an ordered array A = [1, 1, 1, 2, 2, 3], your function needs to return length = 5, and A= [1, 1, 2, 2, 3]

Analysis

It's a little similar to the previous topic, but it allows two consecutive repetitions. There are three solutions:

1. We add a duplicate counter. If the counter reaches the given value, we will not continue to fill our new array. Otherwise, we will continue to fill. If a new value is encountered, the counter is updated to 0.

Advantage: easy to expand to allow n repetitions

Disadvantage: an extra counter needs to be maintained, which increases the complexity of the code and has a high probability of error

2. You can judge whether the current value and the previous value of the new array are equal or not. If they are equal for a long time, they will be added.

Advantage: the logic is relatively simple. It is only necessary to judge whether the value to be traversed is equal to the previous value and the previous value of the new array. It is clear and not easy to make mistakes.

Disadvantage: it is difficult to expand to allow n repetitions

3. Improvement strategy based on scheme 2: since the data is ordered, if n repetitions are allowed, then we only need to judge whether the current value is equal to the n-1 values of the new array at this time.

Advantages: simple and clear logic, not easy to make mistakes

Disadvantage: it is difficult to consider the situation that data needs to be analyzed (data can be listed and summarized with several examples)

Code

The code only implements scheme 1 and scheme 3, and scheme 2 can be implemented by yourself.

/****************************************
* Follow up for "Remove Duplicates": What if duplicates are allowed at most twice?
* For example, Given sorted array A = [1, 1, 1, 2, 2, 3],
* Your function should return length = 5, and A is now [1, 1, 2, 2, 3]
****************************************/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
	/* Time: O(n), Space: O(1) */
	int removeDuplicates1(vector<int> &nums) {
		if (nums.empty()) {
			return 0;
		}
		
		int start = 0;
		int duplicate = 1;
		for (int i = 1; i < nums.size(); i++) {
			if (nums[start] != nums[i] || duplicate < 2) {
				if (nums[start] != nums[i]) {
					duplicate = 0;
				}
				
				start++;
				nums[start] = nums[i];
			}
			
			duplicate++;
		}
		
		for (int i = nums.size() - 1; i > start; i--) {
			nums.pop_back();
		}
		
		return start + 1;
	}
	
	/* Time: O(n), Space: O(1) */
	int removeDuplicates2(vector<int> &nums) {
		if (nums.empty()) {
			return 0;
		}
		
		int start = 1;
		int duplicate = 2;
		for (int i = 2; i < nums.size(); i++) {
			if (nums[start - 1] != nums[i]) {
				start++;
				nums[start] = nums[i];
			}
		}
		
		for (int i = nums.size() - 1; i > start; i--) {
			nums.pop_back();
		}
		
		return start + 1;
	}
};

int main(void) {
	Solution* s = new Solution();
	vector<int> nums;
	nums.push_back(1);
	nums.push_back(1);
	nums.push_back(1);
	nums.push_back(2);
	nums.push_back(2);
	nums.push_back(2);
	nums.push_back(3);
	nums.push_back(3);
	nums.push_back(4);
	
	cout << "Solution 1: " << s->removeDuplicates1(nums) << endl;
	for (int i = 0; i < nums.size(); i++) {
		cout << nums[i] << ",";
	}
	
	cout << endl;
	cout << "Solution 2: " << s->removeDuplicates2(nums) << endl;
	
	for (int i = 0; i < nums.size(); i++) {
		cout << nums[i] << ",";
	}
	
	delete s;
	return 0;
}

 

83 original articles published, 18 praised, 90000 visitors+
Private letter follow

Posted on Wed, 15 Jan 2020 07:30:36 -0500 by sing2trees