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
andnums2
, save the sum ofnums1[i]
andnums2[j]
as key, the frequency is saved as value. - Iterate over
nums3
andnums4
, 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
, andd
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;
}
}