LeetCode | 454. 4Sum II, 383. Ransom Note, 15. 3Sum, 18. 4Sum

454. 4Sum II

Link: https://leetcode.com/problems/4sum-ii/

Description

Given four integer arrays nums1, nums2, nums3, 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

Approach

  • Initialize a hashmap, iterate over nums1 and nums2, save the sum of nums1[i] and nums2[j] as key, the frequency is saved as value.
  • Iterate over nums3 and nums4, check whether (-nums3[k]-nums[l]) exists in the map.

Solution

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int cnt = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for (int s1: nums1)
            for (int s2: nums2)
                map.put(s1 + s2, map.getOrDefault(s1 + s2, 0) + 1);
        
        for (int s3: nums3) 
            for (int s4: nums4) 
                cnt += map.getOrDefault(-s3 - s4, 0);

        return cnt;
    }
}

383. Ransom Note

Link: https://leetcode.com/problems/ransom-note/

Description

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.

Solution

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

15. 3Sum

Link: https://leetcode.com/problems/3sum/

Description

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

Notice that the solution set must not contain duplicate triplets.

Approach

Two pointers

  • Sort the array and traverse the sorted array, starts at i = 0:
    • Set left = i + 1, right = nums.length - 1.
    • Check whether nums[i] + nums[left] + nums[right] = 0.
      • If less than 0, left++;
      • If greater than 0, right–;
      • If equals to 0, save the three points.

Solution

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0)
                break;
            if (i > 0 && nums[i] == nums[i - 1])
                continue;
            int left = i + 1;
            int right = nums.length - 1;
            while (right > left) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum > 0)
                    right--;
                else if (sum < 0)
                    left++;
                else {
                    result.add(Arrays.asList(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 result;
    }
}

Remark

Remeber to remove duplicates when the element equals to the previous element.

18. 4Sum

Link: https://leetcode.com/problems/4sum/}

Description

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
  • a, b, c, and d are distinct.
  • nums[a] + nums[b] + nums[c] + nums[d] == target

You may return the answer in any order.

Solution

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > target && (nums[i] >= 0 || target >= 0))
                break;
            if (i > 0 && nums[i] == nums[i - 1])
                continue;

            for (int j = i + 1; j < nums.length; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1])
                    continue;
                    
                int left = j + 1;
                int right = nums.length - 1;
                while (left < right) {
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if (sum < target)
                        left++;
                    else if (sum > target)
                        right--;
                    else {
                        result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
                        while (left < right && nums[left + 1] == nums[left])
                            left++;
                        while (left < right && nums[right - 1] == nums[right])
                            right--;
                        left++;
                        right--;
                    }
                }
            }
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值