代码随想录算法训练营Day7|LC454 四数相加II&LC383 赎金信&LC15 三数之和&LC18四数之和(哈希表/双指针)

今日总结:双指针不难,难的是去重过程。

原题链接:454 四数相加II

思路很快就想到了,一个数组用哈希表收集,然后三重遍历找target,时间复杂度O(n^3),超时了。 

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int n = nums1.length;
        int ans = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int x : nums1) {
            map.put(x, map.getOrDefault(x, 0) + 1);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    int target = -nums2[i] - nums3[j] - nums4[k];
                    if (map.containsKey(target)) ans += map.get(target);
                }
            }
        }
        return ans;
    }
}

然后nums1和nums2数组之和重新组成一个哈希表,再去两重遍历找target,时间复杂度降到了O(n^2),ac了。

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int ans = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int x : nums1) {
            for (int y : nums2) {
                int sum = x + y;
                map.put(sum, map.getOrDefault(sum, 0) + 1);
            }
        }
        for (int x : nums3) {
            for (int y : nums4) {
                int target = - x - y;
                if (map.containsKey(target)) {
                    ans += map.get(target);
                }
            }
        }
        return ans;
    }
}

原题链接:383 赎金信

简单题直接一个哈希表秒了。

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int rl = ransomNote.length(), ml = magazine.length();
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < ml; i++) {
            char ch = magazine.charAt(i);
            map.put(ch, map.getOrDefault(ch, 0) + 1);
        }
        for (int i = 0; i < rl; i++) {
            char c = ransomNote.charAt(i);
            map.put(c, map.getOrDefault(c, 0) - 1);
            if (map.get(c) < 0) return false;
        }
        return true;
    }
}

 看题解还有更快的方法,用的字符数组。思想还是大同小异的,不过开数组确实比开哈希表要快很多。

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        int[] list = new int[26];
        for (char ch : magazine.toCharArray()) {
            list[ch - 'a']++;
        }
        for (char ch : ransomNote.toCharArray()) {
            list[ch - 'a']--;
            if (list[ch - 'a'] < 0) return false;
        } 
        return true;
    }
}

原题链接:15 三数之和

虽然半年前做过但是重新做起来还是有问题,双指针思想知道,代码也写出来了,但就是输出错误。看了题解以后发现是去重操作的问题。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        Arrays.sort(nums);
        int n = nums.length;
        if (n < 3) return ans;
        for (int i = 0; i < n - 2; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            int l = i + 1, r = n - 1;
            while (l < r) {
                int sum = nums[i] + nums[l] + nums[r];
                if (sum == 0) {
                    ans.add(Arrays.asList(nums[i], nums[l], nums[r]));
                    while (l < r && nums[l] == nums[l + 1]) l++;
                    while (l < r && nums[r] == nums[r - 1]) r--;
                    l++;
                    r--;
                } else if (sum < 0) l++;
                else r--;
            }
        }
        return ans;
    }
}

原题链接:18 四数之和

思想跟前一题三数之和是一样的,不同的是需要多一重遍历和去重操作。 

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<>();
        int n = nums.length;
        if (n < 4) return ans;
        for (int i = 0; i < n - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
            if ((long) nums[i] + nums[n - 1] + nums[n - 2] + nums[n - 3] < target) continue;
            for (int j = i + 1; j < n - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
                if ((long) nums[i] + nums[j] + nums[n - 1] + nums[n - 2] < target) continue;
                int l = j + 1, r = n - 1;
                while (l < r) {
                    long sum = (long) nums[i] + nums[j] + nums[l] + nums[r];
                    if (sum == target) {
                        ans.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
                        while (l < r && nums[l] == nums[l + 1]) l++;
                        while (l < r && nums[r] == nums[r - 1]) r--;
                        l++;
                        r--;
                    } else if (sum < target) l++;
                    else r--;
                }
            }
        }
        return ans;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值