代码随想录算法训练营第6天 | (Hash) Leetcode 454, 383, 15, 18

454. 4Sum II

Given four integer arrays nums1nums2nums3, and nums4 all of length n, return the number of tuples (i, j, k, l) such that:

  • 0 <= i, j, k, l < n
  • nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0

Need Review

The question is a little bit confusing. We need to return how many times the sum of the 4 values from each array is 0. 

Adding values of two values (A and B) storing them as keys and looking the sum of (C & D) in the same hashmap and finding the value associated with it.

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int count = 0;
        HashMap<Integer, Integer> map = new HashMap<>();
        // sum the values from nums1 and nums2
        for (int i : nums1) {
            for (int j : nums2) {
                // if (map.containsKey(i + j)) {
                //     map.put(i + j, map.get(i+j)+1);
                // } else {
                // map.put(i+j, 1);
                // }
                map.put(i + j, map.getOrDefault(i + j, 0) + 1);
            }
        }
        for (int k : nums3) {
            for (int p : nums4) {
                //没找到就是0, 找到后value有几个数就可以加几次
                count += map.getOrDefault(0 - k - p, 0);
            }
        }
        return count;
    }
}

TC: O(n^2)

SC: O(n)

383. Ransom Note

Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the letters from magazine and false otherwise.

Each letter in magazine can only be used once in ransomNote.

 check whether each letter from the ransomNote string is in the magazine hash map.

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
        HashMap<Character, Integer> map = new HashMap<>();
        char[] charNote= ransomNote.toCharArray();
        char[] charMag= magazine.toCharArray();

        for (char i : charMag) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }
        for (char j : charNote) {
            if (map.containsKey(j) && map.get(j) > 0) {
                map.put(j, map.get(j) - 1);
            } else if (!map.containsKey(j) || map.get(j) <= 0){
                return false;
            }
        }
        return true;
    }
}
15. 3 sum

Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != ji != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.

Notice that the solution set must not contain duplicate triplets.

Need Review!

Three pointers point at a sorted array. Only when we test all the combinations for index 0 (left) then move left to left++.

Don't forget to deal with duplicates.

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> arr = new ArrayList<>();
        // need to sort the array fist
        Arrays.sort(nums);
// need to have three pointers
        for (int left = 0; left < nums.length - 2; left++) {
            if (nums[left] > 0) {
                return arr;
            }
            // avoid duplicate left
            if (left > 0 && nums[left] == nums[left - 1]) {
                continue;
            }
            int mid = left + 1;
            int right = nums.length - 1;
            while (mid < right) {
                int sum = nums[left] + nums[mid] + nums[right];
                if (sum > 0) {
                    right --;
                } else if (sum < 0) {
                    mid++;
                } else {
                    arr.add(Arrays.asList(nums[left], nums[mid], nums[right]));
                    // avoid duplicates
                    while (right > mid && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    while (mid < right && nums[mid] == nums[mid + 1]) {
                        mid++;
                    }
                    // still need to move right and mid out of duplicates
                    right --;
                    mid ++;
                }
            }
        }
        return arr;
    }
}

TC: O(n^2)

SC: O(1)

18. 4SUM

Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:

  • 0 <= a, b, c, d < n
  • abc, and d are distinct.
  • nums[a] + nums[b] + nums[c] + nums[d] == target

You may return the answer in any order.

 similar solution as 3Sum

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> arr = new ArrayList<>();
        Arrays.sort(nums);
        for (int l = 0; l < nums.length - 3; l++) {
            if (nums[l] > 0 && nums[l] >= target) {
                return arr;
            }
            if (l > 0 && nums[l] == nums[l-1]) {
                continue;
            }
            for (int i = l + 1; i < nums.length - 2; i++) {
                if (i > l + 1 && nums[i] == nums[i-1]) {
                    continue;
                }
                int left = i + 1;
                int right = nums.length - 1;
                while (left < right) {
                // use long in case the space is not big enough
                    long sum = (long) nums[l] + nums[i] + nums[left] + nums[right];

                    if (sum == target) {
                        arr.add(Arrays.asList(nums[l], 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 if (sum > target) {
                        right --;
                    } else {
                        left ++;
                    }
                }
            }
        }
        return arr;
    }
}

TC: O(x^3)

SC: O(1)

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值