18. 4Sum
Given an array nums
of n integers and an integer target
, are there elements a, b, c, and d in nums
such that a + b + c + d = target
? Find all unique quadruplets in the array which gives the sum of target
.
Note:
The solution set must not contain duplicate quadruplets.
Example:
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
- 思路:还是遍历两个元素,剩下的两个元素用两个指针去找。
- 注意:
- 主要是去重,我用unordered_set去重,速度还不错,就是内存用的比较多。
- 还有一种思路就是遍历元素的时候,如果发现和之前的相同,就跳过。
class MyHash{
public:
size_t operator()(const vector<int> nums) const {
size_t ret = 0;
for(int a : nums) {
ret ^= hash<int>()(a);
}
return ret;
}
};
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
unordered_set<vector<int>, MyHash> checkSet;
vector<vector<int>> ret;
for(int i = 0; i < nums.size(); i++) {
for(int j = i + 1; j < nums.size(); j++) {
int l = j + 1, r = nums.size() - 1;
int newTarget = target - nums[i] - nums[j];
while(l < r) {
int sum = nums[l] + nums[r];
if(sum == newTarget) {
vector<int> vec({nums[i], nums[j], nums[l], nums[r]});
if(checkSet.find(vec) == checkSet.end()) {
ret.push_back(vec);
checkSet.insert(vec);
}
l++;
r--;
} else if(sum < newTarget) {
l++;
} else {
r--;
}
}
}
}
return ret;
}
};
26. Remove Duplicates from Sorted Array
Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
Given 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 returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4
respectively.
It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
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.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); }
- 思路:因为是排序过的,所以两个指针扫描一遍就可以。
- 注意:太水了,以后这种题不做了。再做一道题补充一下。
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size() < 2)
return nums.size();
int putPtr = 0;
for(int i = 1; i < nums.size(); i++) {
if(nums[i] != nums[putPtr]) {
nums[++putPtr] = nums[i];
}
}
return putPtr+1;
}
};
31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3
→ 1,3,2
3,2,1
→ 1,2,3
1,1,5
→ 1,5,1
- 思路:要找下一个排列数,全都是降序的数组是没有下一个排序数的,所以就从后往前找第一个变小的数x[i],在i后面逆序找第一个比x[i]大的数x[ j ],交换这两个数,并把 i 后面的部分逆序即可。这样就可以得到下一个比当前数大的排列数。
- 注意:纯逆序的下一个排列书就是全正序。
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i = nums.size() - 2;
while(i >= 0 && nums[i + 1] <= nums[i]) i--;
if(i >= 0) {
int j = nums.size() - 1;
while(j >= 0 && nums[j] <= nums[i]) j--;
swap(nums, i, j);
reverse(nums, i + 1);
} else {
reverse(nums, 0);
}
}
void reverse(vector<int>& nums, int start) {
int i = start, j = nums.size() - 1;
while(i < j) {
swap(nums, i, j);
i++;
j--;
}
}
void swap(vector<int>& nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
};