26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first two elements of nums being 1
and 2
respectively. It doesn't matter what you leave beyond the new length.
这题室友面微软的时候被问过【笑哭】
因为限制了重新开空间,只能在原空间上操作。就设定两个指针,一个遍历数组,另一个指针负责记录有多少个数,相同就不赋值,不相同就覆盖原数组的值。
时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.empty()) return 0;
int ans = 1;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] == nums[i-1]) continue;
nums[ans++] = nums[i];
}
return ans;
}
};
80. Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?
For example,
Given sorted array nums = [1,1,1,2,2,3]
,
Your function should return length = 5
, with the first five elements of nums being 1
, 1
, 2
, 2
and 3
. It doesn't matter what you leave beyond the new length.
因为最多只会用到num[ans-2]的值,于是找个数存了一下,就OK了
当然,看了看评论区后,我依然是服气的,明明大家思路都一样。。。好优雅啊QAQ
时间复杂度O(n),空间复杂度O(1)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if (nums.size() <= 2) return nums.size();
int ans = 2;
int cur = 0;
bool flag = false;
for (int i = 2; i < nums.size(); ++i) {
if (nums[i] == nums[i-2]) continue;
if (!flag) {
cur = nums[i];
flag = true;
continue;
}
nums[ans++] = cur;
cur = nums[i];
}
if (flag)
nums[ans++] = cur;
return ans;
}
};
int removeDuplicates(vector<int>& nums) {
int i = 0;
for (int n : nums)
if (i < 2 || n > nums[i-2])
nums[i++] = n;
return i;
}
27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3]
, val = 3
Your function should return length = 2, with the first two elements of nums being 2.
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int ans = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == val) continue;
nums[ans++] = nums[i];
}
return ans;
}
};
283. Move Zeroes
Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
嗯,就是把所有的0移到最后。。。感觉还是一样的?
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int ans = 0;
for (int i = 0; i < nums.size(); ++i) {
if (nums[i] == 0) continue;
nums[ans++] = nums[i];
}
for (int i = ans; i < nums.size(); ++i) {
nums[i] = 0;
}
return;
}
};