454.四数相加II
细节:
数值大小不可控,不可以用array;题意要统计元组出现的次数,所以我们需要有key-value的键值对, 所以说用Map。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int count = 0;
for (int i : nums1){
for (int j : nums2){
int sum = i + j;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
for (int i : nums3){
for (int j : nums4){
count += map.getOrDefault(0 - (i + j), 0);
}
}
return count;
}
}
383. 赎金信
细节:
1.哈希表的思维考虑,根据题意的大小
2.可以用数组或者map
3.注意Java中String / char /int 语法
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
// 都是小写字母,那就可以用一个长度为26的数组count记录
if (ransomNote.length() > magazine.length()) {
return false;
}
int[] count = new int[26];
for (char i : magazine.toCharArray()) {
count[i - 'a']++;
}
for (char i : ransomNote.toCharArray()) {
count[i - 'a']--;
if (count[i - 'a'] < 0) {
return false;
}
}
return true;
}
}
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
// 用map,但是空间大了
Map<Character, Integer> map = new HashMap<>();
// int res = 0;
for (int i = 0; i < magazine.length(); i++) {
char count = magazine.charAt(i);
map.put(count, map.getOrDefault(count, 0) + 1);
}
for (int i = 0; i < ransomNote.length(); i++) {
char count = ransomNote.charAt(i);
map.put(count, map.getOrDefault(count, 0) - 1);
// res += map.getOrDefault(count, 0);
if (map.get(count) < 0) {
return false;
}
}
return true;
}
}
15. 三数之和
链接:https://leetcode.cn/problems/3sum/
细节:
1.哈希?可以,但是要去重,选择双指针
2.不返回下标,那就排序
3.去重a(后一位对比)
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) { // 第一个数大于零,直接不可能了
return result;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
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[right] == nums[right - 1])right--;
while (left < right && nums[left] == nums[left + 1])left++;
right--;
left++;
}
}
}
return result;
}
}
18. 四数之和
细节:
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
// 创建接收集合
List<List<Integer>> result = new ArrayList<>();
// 对数组进行排序
Arrays.sort(nums);
for (int k = 0; k < nums.length; k++) {
// 剪枝操作
// 因为这里的target不是0,也有可能数组中全是负数,所以不能像三数之和一样进行剪枝
if (nums[k] > target && nums[k] > 0 && target > 0) {
break;
}
// 去重操作
if (k > 0 && nums[k] == nums[k - 1]) {
continue;
}
for (int i = k + 1; i < nums.length; i++) {
// 剪枝操作
// 这里剪枝去重的话k和i是一个整体
if (nums[k] + nums[i] > target && nums[k] + nums[i] > 0 && target > 0) {
break;
}
// 去重操作
if (i > k + 1 && nums[i] == nums[i - 1]) {
continue;
}
// 定义left和right指针
int left = i + 1;
int right = nums.length - 1;
while (right > left) {
long sum = (long) nums[k] + nums[i] + nums[left] + nums[right];
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
// 收割结果
result.add(Arrays.asList(nums[k], 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++;
}
}
}
}
return result;
}
}