class Solution {
public:
int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
unordered_map<int,int> ump;
for(int a:nums1){
for(int b:nums2){
ump[a+b]++;
}
}
int count = 0;
for(int c:nums3){
for(int d:nums4){
if(ump.find(0-(c+d))!=ump.end()){
count += ump[0-(c+d)];
}
}
}
return count;
}
};
用unorderd_map来写比较容易,key存相加的值,value存出现的次数;
lass Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
for(int i = 0;i<magazine.length();i++){
for(int j = 0;j<ransomNote.length();j++){
if(ransomNote[j]==magazine[i]){
ransomNote.erase(ransomNote.begin() + j); // ransomNote删除这个字符
break;
}
}
}
if(ransomNote.length() == NULL){
return true;
}
return false;
}
};
两个for循环暴力解,注意删除字母的写法;
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int rseult[26] = {0};
if (ransomNote.size() > magazine.size()) {
return false;
}
for(int i = 0;i<magazine.length();i++){
rseult[magazine[i] - 'a']++;
}
for(int j = 0;j<ransomNote.length();j++){
rseult[ransomNote[j] - 'a']--;
if(rseult[ransomNote[j] - 'a'] < 0){
return false;
}
}
return true;
}
};
采用哈希表——数组的写法,注意以后见到字符串,只有小写字母,直接定义数组26大小,加一代表出现一次;
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
unordered_set<int> ump;
vector<vector<int>> result;//定义三元组
sort(nums.begin(),nums.end());
for(int i =0;i<nums.size();i++){
if(nums[i]>0){
break;
}
if(i > 0 && nums[i] == nums[i - 1]){
continue;
}
for(int j = i+1;j<nums.size();j++){
if(j > i + 2
&& nums[j] == nums[j-1]
&& nums[j-1] == nums[j-2]){
continue;
}
int tmp = 0 - (nums[i] + nums[j]);
if(ump.find(tmp)!=ump.end()){
result.push_back({nums[i], nums[j], tmp});
ump.erase(tmp);
}else{
ump.insert(nums[j]);
}
}
}
return result;
}
};
采用Unoreded_set方法来写,主要是由于去重问题,因此比较不好写,但是好想;再set里找到值等于-a+b的就行;
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;//定义三元组
sort(nums.begin(),nums.end());
for(int i = 0;i<nums.size();i++){
if(nums[i]>0){
return result;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.size() - 1;
while(right > left){
if(nums[i] + nums[left] + nums[right]>0){
right--;
}
else if(nums[i] + nums[left] + nums[right]<0){
left++;
}
else{
result.push_back(vector<int>{nums[i],nums[left],nums[right]});
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;
}
}
}
return result;
}
};
双指针想法比较巧妙,主要是得理解去重方法,不是很好理解;
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<vector<int>> result;
sort(nums.begin(),nums.end());
for(int k =0;k<nums.size();k++){
if(nums[k]>target&&nums[k]>=0){
break;
}
if(k>0&&nums[k]==nums[k-1]){
continue;
}
for(int i = k + 1;i<nums.size();i++){
if(nums[k]+nums[i]>target&&nums[k]+nums[i]>=0){
break;
}
if(i>k+1&&nums[i]==nums[i-1]){
continue;
}
int right = nums.size() - 1;
int left = i + 1;
while(left<right){
//不加long会超时
if((long) nums[i] + nums[k] + nums[left] + nums[right] > target){
right--;
}else if((long) nums[i] + nums[k] + nums[left] + nums[right] < target){
left++;
}
else{
result.push_back(vector<int>{nums[k],nums[i],nums[left],nums[right]});
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
right--;
left++;//这俩得放在else内
}
}
}
}
return result;
}
};
去重就是后一个等于前一个,加上一个循环的边界条件