代码随想录训练Day7[哈希表]
第454题.四数相加II
给定四个包含整数的数组列表 A , B , C , D ,计算有多少个元组 (i, j, k, l) ,使得 A[i] + B[j] + C[k] + D[l] = 0。
两个两个找
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
//想:将两个放入map中
// 先遍历ab的数组放入map中key:a+b value:次数,再历cd的数组时再判断map中有没有匹配项
//时间复杂度n2
HashMap<Integer, Integer> result = new HashMap<>();
int cont = 0;
for (int a : nums1) {
for (int b : nums2) {
result.put(a+b, result.getOrDefault(a+b , 0)+1 );
}
}
for (int c : nums3) {
for (int d : nums4) {
if (result.containsKey(0 - (c + d))) {
cont += result.get(0 - (c + d));
}
}
}
return cont;
}
383. 赎金信
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成,返回 true ;否则返回 false。
public boolean canConstruct(String ransomNote, String magazine) {
//思路:两个字符串都是小写字母,用数组来做hash
//将ransomNote.charAt(i) - 'a' 做为做索引,
int[] arr = new int[26];
for (int i = 0; i < magazine.length(); i++) {
arr[magazine.charAt(i) - 'a']++;
}
for (int i = 0; i < ransomNote.length(); i++) {
if (arr[ransomNote.charAt(i) - 'a'] != 0){
arr[ransomNote.charAt(i) - 'a']--;
}else {
return false;
}
}
return true;
}
15. 三数之和
给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组。
public List<List<Integer>> threeSum(int[] nums) {
//a + b + c = 0
//a 作为定位在数组的首位,b : 是左边的指针
//c : 是右边的指针
//先排序
//要让结果集中没有重复的三元组,那么a就不能重复 nums[i] == nums[i - 1]
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 (right > left){
if (nums[i] + nums[left] + nums[right] > 0){
right --;
}
else if (nums[i] + nums[left] + nums[right] < 0 ){
left ++;
}
else {
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
//对 b 和 c 进行去重
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
//移动到下一个
right--;
left++;
}
}
}
return result;
}
四数之和
题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
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++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
for (int j = i + 1; j < nums.length; j++) {
//不能直接剪枝 考虑到负数 改变
if (nums[i] > target && target > 0 && nums[i] > 0) {
return result;
}
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
if (nums[i]+ nums[j] > target && target > 0){
break;
}
int left = j + 1;
int right = nums.length - 1;
while(right > left){
//这里要用long
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]));
while (right > left && nums[right] == nums[right - 1]) right--;
while (right > left && nums[left] == nums[left + 1]) left++;
right --;
left ++;
}
}
}
}
return result;
}

3357

被折叠的 条评论
为什么被折叠?



