代码随想录day7|454. 四数相加II,383. 赎金信,15. 三数之和18. 四数之和
第一思路:两个hashmap分别算出nums1,nums2和nums3,nums4的和,再遍历查找
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> map1 = new HashMap<>();
int index = 0;
for(int i = 0 ; i < nums1.length ; i++){
for(int j = 0 ; j < nums2.length ; j++){
int temp = nums1[i]+nums2[j];
map1.put(-temp,map1.getOrDefault(-temp,0)+1);
}
}
int count = 0;
for(int i = 0 ; i < nums3.length ; i++){
for(int j = 0 ; j < nums4.length ; j++){
int temp = nums3[i]+nums4[j];
if(map1.containsKey(temp))count+=map1.get(temp);
}
}
return count;
}
}
刚开始又看错题了,以为和为指定数,结果和为0就行
思路:用map记录下nums1和nums2的和以及该和出现的次数,再遍历nums3,nums4累加上符合条件的数就行
383. 赎金信
出现字母就既可以使用哈希又可以用数组
//数组法
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] mag = new int[26];
for(int i = 0 ; i < magazine.length() ; i++){
mag[magazine.charAt(i) - 'a']++;
}
for(int i = 0 ;i < ransomNote.length() ; i++){
if(mag[ransomNote.charAt(i)-'a'] < 1)return false;
mag[ransomNote.charAt(i)-'a']--;
}
return true;
}
}
//哈希法
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
Map<Character , Integer> map = new HashMap<>();
for(int i = 0 ; i < magazine.length() ; i++){
map.put(magazine.charAt(i),map.getOrDefault(magazine.charAt(i),0)+1);
}
for(int i = 0 ;i < ransomNote.length() ; i++){
if(map.get(ransomNote.charAt(i))==null||map.get(ransomNote.charAt(i))<1)return false;
map.put(ransomNote.charAt(i),map.get(ransomNote.charAt(i))-1);
}
return true;
}
}
15. 三数之和
//错误思路
//双循环+map
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Map<Integer , Integer> map = new HashMap<>();
for(int i = 0 ; i < nums.length;i++){
map.put(-nums[i],i);
}
for(int i = 0 ; i < nums.length ; i++){
for(int j = i+1 ; j < nums.length ; j++){
if(map.containsKey(nums[i]+nums[j])
&& map.get(nums[i]+nums[j]) != i
&& map.get(nums[i]+nums[j]) != j){
List<Integer> temp =new ArrayList<>();
temp.add(nums[i]);
temp.add(nums[j]);
temp.add(nums[map.get(nums[i]+nums[j])]);
ans.add(temp);
}
}
}
return ans;
}
}
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<>();
for(int i = 0 ; i < nums.length && nums[i] <= 0 ; i++){
int left = i + 1;
int right = nums.length-1;
//i去重,nums[i]==nums[i-1]是判断组外是否重复,nums[i]==nums[i+1]是判断组内是否重复
if(i>0 &&nums[i]==nums[i-1])continue;
//不能为(left < right)因为当right==left是不能加入循环
while(left < right){
if(nums[right]+nums[left] == -nums[i]){
ans.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++;
right--;
left++;
}else if(nums[right]+nums[left] > -nums[i]){
right --;
}else{
left++;
}
}
}
return ans;
}
}
18. 四数之和
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0 ; i < nums.length ; i++){
//去重
if(nums[i] > 0 && nums[i] > target)return ans;
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(right > left){
long sum = (long)(nums[i] + nums[j] + nums[left] + nums[right]);
if (sum > target) {
right--;
} else if (sum < target) {
left++;
} else {
ans.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++;
left++;
right--;
}
}
}
}
return ans;
}
}