454.四数相加II
给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足:
- 0 <= i, j, k, l < n
- nums1[i] + nums2[j] + nums3[k] + nums4[l] == 0
先遍历前两个数组,保存所有的结果和数量,再遍历后两个数组,查看其对应的负数是否存在以及存在的个数。将4数之后的问题就简化成了2数之和。
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> map = new HashMap<>();
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);
}
}
}
int result = 0;
for(int i : nums3) {
for(int j: nums4) {
int key = -(i + j);
if(map.containsKey(key)) {
result += map.get(key);
}
}
}
return result;
}
383. 赎金信
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
构建一个26位的数组来记录字母出现的次数。
public boolean canConstruct(String ransomNote, String magazine) {
int[] alphabet = new int[26];
for(int i = 0; i < ransomNote.length(); i++) {
alphabet[ransomNote.charAt(i) - 'a'] ++;
}
for(int i = 0; i < magazine.length(); i++) {
alphabet[magazine.charAt(i) - 'a'] --;
}
boolean result = true;
for(int i: alphabet) {
if(i > 0) {
result = false;
break;
}
}
return result;
}
15. 三数之和
题目要求不能出现重复的三元组,所以使用hash来实现的话去重会比较麻烦,为了提高效率,减少循环的次数,这里使用的双指针的方式来解决。
只有排序之和,才能方便的使用双指针来解决问题。为了防止重复的三元组出现,不仅仅第一个i需要做去重判断,其他的left和right指针同样需要做去重判断。
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
for(int i = 0; i < nums.length; i ++) {
int left = i + 1;
int right = nums.length - 1;
if(i >= 1 && nums[i -1] == nums[i]) {
continue;
}
while(left < right) {
if(nums[i] + nums[left] + nums[right] < 0) {
left ++;
} else if(nums[i] + nums[left] + nums[right] > 0) {
right --;
} else {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
left ++;
right --;
}
}
}
return result;
}
18. 四数之和
整体的解决思路和三数之和是一样的,通过剪枝来避免一些无意义的计算的时候需要注意target可以是负值,数组中的元素也可以是负数
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++) {
// nums[i] > target 直接返回, 剪枝操作
if (nums[i] > 0 && nums[i] > target) {
return result;
}
if (i > 0 && nums[i - 1] == nums[i]) { // 对nums[i]去重
continue;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
// nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
// 对nums[left]和nums[right]去重
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
left++;
right--;
}
}
}
}
return result;
}
}