代码随想录第七天打卡|454.四数相加II, 383. 赎金信 ,15. 三数之和 , 18. 四数之和

admin2024-05-15  1

454.四数相加II

Python

class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        n=len(nums1)
        res=0
        Map=defaultdict(int)
        for i in range(n):
            for j in range(n):
                Map[nums1[i]+nums2[j]]+=1
        for i in range(n):
            for j in range(n):
                if Map[-(nums3[i]+nums4[j])]>0:
                    res+=Map[-(nums3[i]+nums4[j])]
        return res
        

C++

class Solution {
public:
    int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) {
        int n=nums1.size();
        unordered_map<int,int> mp;
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                mp[nums1[i]+nums2[j]]++;
            }
        }
        int res=0;
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                if (mp[-(nums3[i]+nums4[j])]>0){
                    res+=mp[-(nums3[i]+nums4[j])];
                }
            }
        }
        return res;
    }
};

383. 赎金信

C++

class Solution {
public:
    bool canConstruct(string ransomNote, string magazine) {
        unordered_map<char,int> mp;
        for (auto s:magazine)mp[s]++;
        for (char s:ransomNote){
            if (mp[s]>0){
                mp[s]--;
            }
            else return false;
        }    
        return true;
       
    }
};

Python

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        Map=defaultdict(int)
        for s in magazine:
            Map[s]+=1
        for s in ransomNote:
            if Map[s]>0:
                Map[s]-=1
            else:
                return False
        return True

15. 三数之和

Python

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        if nums[0]>0:
            return []
        res=[]
        for i in range(len(nums)-2):
            left=i+1
            right=len(nums)-1
            if i>0 and nums[i]==nums[i-1]:  
                continue
            while left<right:
                if nums[i]+nums[left]+nums[right]>0:
                    right-=1
                elif nums[i]+nums[left]+nums[right]<0:
                    left+=1
                else:
                    res.append([nums[i],nums[left],nums[right]])
                    while left<right and nums[right]==nums[right-1]:
                        right-=1
                    while left<right and nums[left]==nums[left+1]:
                        left+=1
                    left+=1
                    right-=1
        return res

C++

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        sort(nums.begin(),nums.end());
        if (nums[0]>0){
            return res;
        }
        int left=0,right=nums.size();
        for (int i=0;i<nums.size()-2;i++){
            if (i>0 && nums[i]==nums[i-1])continue;
            int left=i+1,right=nums.size()-1;
            while (left<right){
                if (nums[i]+nums[left]+nums[right]>0)right--;
                else if (nums[i]+nums[left]+nums[right]<0)left++;
                else {
                    res.push_back(vector<int>{nums[i],nums[left],nums[right]});
                    while (left<right && nums[left]==nums[left+1]) left++;
                    while (left<right && nums[right]==nums[right-1]) right--;
                    left++;
                    right--;
                }
            }
        }
        return res;
    }
};

18. 四数之和

Python

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums.sort()
        res=[]
        for i in range(len(nums)-3):
            if i>0 and nums[i]==nums[i-1]:
                continue
            for j in range(i+1,len(nums)-2):
                if j-1>i and nums[j]==nums[j-1]:
                    continue
                left,right=j+1,len(nums)-1
                get=target-nums[i]-nums[j]
                while left<right:
                    if nums[left]+nums[right]>get:
                        right-=1
                    elif nums[left]+nums[right]<get:
                        left+=1
                    else:
                        res.append([nums[i],nums[j],nums[left],nums[right]])
                        while left<right and nums[left]==nums[left+1]:
                            left+=1
                        while left<right and nums[right]==nums[right-1]:
                            right-=1
                        left+=1
                        right-=1  
        return res      

C++

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        sort(nums.begin(),nums.end());
        vector<vector<int>>res;
        if (nums.size()<4)return res;
        for (int i=0;i<nums.size()-3;i++){
            if (i>0 && nums[i]==nums[i-1])continue;
            for (int j=i+1;j<nums.size()-2;j++){
                if (j>i+1 && nums[j]==nums[j-1])continue;
                int left=j+1,right=nums.size()-1;
                while (left<right){
                    if ((long)nums[left]+nums[right]+nums[i]+nums[j]>target)right--;
                    else if ((long)nums[left]+nums[right]+nums[i]+nums[j]<target)left++;
                    else{
                        res.push_back(vector<int>{nums[left],nums[right],nums[i],nums[j]});
                        while (left<right && nums[left]==nums[left+1])left++;
                        while (left<right && nums[right]==nums[right-1])right--;
                        left++;
                        right--;
                    }
                }
            }
        }
        return res;
    }
};

总结

这道题的要求可以用四个相同的指针,每个指针不能指向同一个值且相对位置必须是相同的,每个指针遍历的数不能有相同的,来表示。忘记了,三刷用map来试试。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!