Removal of an ordered array of repeating number 2 (Remove Duplicates from Sorted Array II)

topic

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 inside of an ordered array of digital duplicate topics that road, if the situation permits under repeated twice, how should I handle it?

For example you give an ordered array A = [1, 1, 1, 2, 2, 3], you need to function returns the length = 5, and A = [1, 1, 2, 2, 3]

analysis

And before that topic is somewhat similar to Ha, just that allow two consecutive repeat, there are three possible solution:

1. we add a duplicate of the counter, if the counter reaches a given value, then do not continue to fill our new array, otherwise it continues to fill. If you encounter a new value, it updates the counter to zero.

Advantage: easily extended to allow the situation repeated n times

Disadvantages: the need for additional maintenance a counter, increases the complexity of the code, the chance of error is relatively large

2. You can determine the current value and a value on the new array and a value on equality, equal if ignored for a long time, or else join.

Advantages: simple logic, requires only a value judgment on the value and the new value to traverse the array are equal can, clear-cut and less error prone.

Disadvantages: more difficult to allow extended to the case of n-times

3. Improved Policy Scenario 2: Because the data is ordered, if allowed to be repeated n times, so this time we just need to determine whether the current value if the new value of n-1 array is equal to it.

Advantage: the logic is simple and clear, less error prone

Disadvantages: more difficult to take into account the need to analyze the situation of data (the data can be listed, summed up a few examples)

Code

Only code for Scheme 1 and Scheme 3, Scheme 2 it can achieve on their own.

/****************************************
* 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;
}

 

Published 83 original articles · won praise 18 · views 90000 +

Guess you like

Origin blog.csdn.net/lwc5411117/article/details/103994291