leetcode 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
![](https://img-blog.csdnimg.cn/img_convert/2a14501d0bcbf10524cdc6429eeba5de.png)
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> map = new HashMap<>();
int res = 0;
for(int i : nums1){
for(int j : nums2){
int temp = i+j;
if(map.containsKey(temp)){
map.put(temp,map.get(temp)+1);
}else{
map.put(temp,1);
}
}
}
for(int i : nums3){
for(int j : nums4){
int temp = i+j;
if(map.containsKey(0-temp)){
res += map.get(0-temp);
}
}
}
return res;
}
}
leetcode 383. 赎金信
给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
如果可以,返回 true ;否则返回 false 。
magazine 中的每个字符只能在 ransomNote 中使用一次。
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] res = new int[26];
for(char i : magazine.toCharArray()){
res[i-'a']++;
}
for(char i : ransomNote.toCharArray()){
res[i-'a']--;
}
for(int i:res){
if(i < 0){
return false;
}
}
return true;
}
}