LeetCode:454.四数相加ll && 383.赎金信 && 15.三数之和 && 18.四数之和

454.四数相加ll

题目

给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
    在这里插入图片描述
    来源:力扣(LeetCode)
    链接:四数相加ll

思路

  • 首先定义一个map,key放 i 和 j 两数之和,value 放 i 和 j 两数之和出现的次数。
  • 遍历 nums1 和 nums2 数组,统计两个数组元素之和,和出现的次数,放到 map 中。
  • 定义int变量 res ,用来统计 i + j + x + y = 0 出现的次数。
  • 在遍历 nums3 和 nums4 数组,找到如果 0 - (x + y) 在 map 中出现过的话,就用 res 把 map
    中key对应的value也就是出现次数统计出来。
  • 最后返回统计值 res 就可以了

哈希+双循环1

  • 时间复杂度:O(n2)
  • 空间复杂度:O(n2)
class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map = new HashMap<>();
        int res = 0;
        for(int i = 0; i < nums1.length; i++){
            for(int j = 0; j < nums2.length; j++){
                int nums12 = nums1[i] + nums2[j];
                if(map.containsKey(nums12)){
                    map.put(nums12, map.get(nums12) + 1);
                }else{
                    map.put(nums12, 1);
                }
            }
        }
        for(int x = 0; x < nums3.length; x++){
            for(int y = 0; y < nums4.length; y++){
                int nums34 = nums3[x] + nums4[y];
                if(map.containsKey(nums34)){
                    res += map.get(nums34);
                }
            }
        }
        return res;
    }
}

哈希+双循环2

  • 时间复杂度:O(n2)
  • 空间复杂度:O(n2)
class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int res = 0;
        Map<Integer, Integer> map = new HashMap<>();
        // 统计两个数组中元素之和,并统计出现次数放入map中
        for(int i : nums1){
            for(int j : nums2){
                int sum = i + j;
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }
        // 统计剩余的两个数组,在map中找到相加是否为0的元素,并记录次数
        for(int x : nums3){
            for(int y : nums4){
                res += map.getOrDefault(0 - x - y, 0);
            }
        }
        return res;
    }
}

383.赎金信

题目

给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
在这里插入图片描述
来源:力扣(LeetCode)
链接:赎金信

思路

  • 字母不可重复使用
  • 只有小写字母

算法(方法上跟242.有效的字母异位词相类似)

  • 时间复杂度:O(r + m),两个字符串的长度
  • 空间复杂度:O(S),S = 26,数组的容量
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if(ransomNote.length() > magazine.length()){
            return false;
        }
        int[] sum = new int[26];
        for(int i = 0; i < magazine.length(); i++){
            sum[magazine.charAt(i) - 'a']++;
        }
        for(int i = 0; i < ransomNote.length(); i++){
            sum[ransomNote.charAt(i) - 'a']--;
            if(sum[ransomNote.charAt(i) - 'a'] < 0){
                return false;
            }
        }
        return true;
    }
}

哈希

  • 时间复杂度: O(n)
  • 空间复杂度: O(1)
class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        if(ransomNote.length() > magazine.length()) return false;
        // 定义哈希映射数组
        int[] record = new int[26];
        // 遍历
        for(char c : magazine.toCharArray()){
            record[c - 'a'] += 1;
        }
        for(char c : ransomNote.toCharArray()){
            record[c - 'a'] -= 1;
        }
        // 如果数组中存在负数,证明ransomNote中有magazine不存在的字符
        for(int i : record){
            if(i < 0) return false;
        }
        return true;
    }
}

15.三数之和

题目

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != j、i != k 且 j != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请
你返回所有和为 0 且不重复的三元组。
注意:答案中不可以包含重复的三元组
在这里插入图片描述
来源:力扣(LeetCode)
链接:三数之和

思路

for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0,这样做的好处就是,暴力破解需要O(n3),而它只需要O(n2)

排序+双指针1

  • 时间复杂度: O(n2)
  • 空间复杂度: O(1)
    在这里插入图片描述
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        // 排序
        Arrays.sort(nums);
        
        for(int i = 0; i < nums.length; i++){
            // 排序后的数组如果第一个元素已经大于0,那么是不可能组成三元组的
            if(nums[i] > 0) return result;
            // 去重
            if(i > 0 && nums[i] == nums[i - 1]) continue;
            
            // 将left标记在i的后一位
            int left = i + 1;
            // 将right标记在数组的最有一位
            int right = nums.length - 1;
            
            while(right > left){
                int sum = nums[i] + nums[left] + nums[right];
                // 当sum大于0,降低最高的数值
                if(sum > 0){
                    right--;
                // 当sum小于0,提高最小的数值
                }else if(sum < 0){
                    left++;    
                }else{
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));
                    // 去重应当放在找到第一个三元组之后,对left和right去重
                    while(right > left && nums[right] == nums[right - 1]) right--;
                    while(right > left && nums[left] == nums[left + 1]) left++;
                    right--;
                    left++;
                }
            }
        }
        return result;
    }
}

排序+双指针2

  • 时间复杂度:O(n2)
  • 空间复杂度:O(nlogn)
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        List<List<Integer>> sum = new ArrayList<List<Integer>>();
        for(int first = 0; first < n; first++){
            if(first > 0 && nums[first] == nums[first - 1]){
                continue;
            }
            int third = n - 1;
            int target = -nums[first];
            for(int second = first + 1; second < n; second++){
                if(second > first + 1 && nums[second] == nums[second - 1]){
                    continue;
                }
                while(second < third && nums[second] + nums[third] > target){
                    --third;
                }
                if(second == third){
                    break;
                }
                if(nums[second] + nums[third] == target){
                    List<Integer> ans = new ArrayList<>();
                    ans.add(nums[first]);
                    ans.add(nums[second]);
                    ans.add(nums[third]);
                    sum.add(ans);
                }
            }
        }
        return sum;
    }
}

18.四数之和

题目

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • a、b、c 和 d 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案 。
在这里插入图片描述
来源:力扣(LeetCode)
链接:四数之和

思路

“四数之和”与“三数之和”最大的不同就在于:

  1. “三树之和”可以通过nums[i] > 0就返回值,而“四数之和”却不能这样做,因为“三树之和”中的 0已经是确定的值,“四数之和”中的 target 是随机的值,也就意味着不能直接使用nums[i] > target,但可以加上约束条件nums[i] > target && (nums[i] >=0 || target >= 0)
  2. “三树之和”是一层 for 循环 nums[i] 加 left 和 right 双指针找到nums[i] + nums[left] + nums[right] == 0,而“四数之和”是两层 for 循环 nums[i] + nums[j] 加 left 和 right 双指针找到nums[i] + nums[j] + nums[left] + nums[right] == 0
  3. 时间复杂度为O(n3)

排序+双指针

  • 时间复杂度:O(n3)
  • 空间复杂度:O(1)
    在这里插入图片描述
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        // 排序
        Arrays.sort(nums);
        // 遍历
        for(int i = 0; i < nums.length; i++){
            // 如果排序后第一个数已经大于目标值和0,也就不可能组成四元组
            if(nums[i] > 0 && nums[i] > target) return result;
            // nums[i]去重
            if(i > 0 && nums[i] == nums[i - 1]) continue;
            for(int j = i + 1; j < nums.length; j++){
                // nums[j]去重
                if(j > i + 1 && nums[j] == nums[j - 1]) continue;
                int left = j + 1;
                int right = nums.length - 1;
                while(right > left){
                    // int sum = nums[i] + nums[j] + nums[left] + nums[right];会溢出
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    // 当sum大于0,降低right的数值
                    if(sum > target){
                        right--;
                    // 当sum小于0,提高left的数值
                    }else if(sum < target){
                        left++;
                    }else{
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        // 对left和right去重
                        while(right > left && nums[right] == nums[right - 1]) right--;
                        while(right > left && nums[left] == nums[left + 1]) left++;
                        right--;
                        left++;
                    }
                }
            }
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值