1. 454. 四数相加||
- 题目链接:454. 四数相加 II - 力扣(LeetCode)
- 题意:四个长度为n的整数数组,计算有多少个元组可以满足和为0
- 思路:先将前两个数组的和做成map={key:sum(num1[i]+num2[i], value:times),再将后两个数两个数组的和与map相比较,如果= -key in map 则次数+value个
- 复杂度:time: O(n^2) Space: O(n^2)
- 代码:
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
// 思路:两两计算
Map<Integer, Integer> sum1and2 = new HashMap<>();
// array 1+2
for(int i=0; i<nums1.length; i++){
for(int j=0; j<nums2.length; j++){
sum1and2.put(nums1[i] + nums2[j], sum1and2.getOrDefault(nums1[i] + nums2[j], 0) + 1);
}
}
// array 3+4
int count = 0;
int[] add3and4 = new int[nums3.length*nums4.length];
for(int i=0; i<nums3.length; i++){
for(int j=0; j<nums4.length; j++){
int complement = -nums3[i] - nums4[j];
if(sum1and2.containsKey(complement)){
count += sum1and2.get(complement);}
}
}
return count;
}
}
2. 383. 赎金信
- 链接:383. 赎金信 - Medium
- 题目含义:判断字符串t中的元素能不能由magazine里面的字符构成,可以则返回true,不可以返回false,每个元素只能用一次
- 思路: 首先由于这两个字符串都由小写英文字母组成,所以可以考虑数组的方法,其次,也可以考虑用map构建一个字符串。前者会更快一些
- 复杂度:空间O(1),时间0(n)
- 代码实现:
-
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
//用数组的方法
int[] dictionary = new int[26];
// magazine的值存入哈希表
char[] aimed = magazine.toCharArray();
for(char alphabet : aimed){
int index = alphabet - 'a';
dictionary[index]++;
}
// 将哈希表与ransomNote进行比较
char[] compared = ransomNote.toCharArray();
for(char alphabet : compared){
int index = alphabet - 'a';
if(dictionary[index] == 0) return false;
else
dictionary[index]--;
}
return true;
}
}
3. 15. 三数之和
4. 18.四数之和
- 链接:18. 四数之和 - Medium- 力扣(LeetCode)
- 题意:和上一道题差不多,只不过是四个数,且和变成了target
- 思路:和上一题的总体思路是一样的,但是要注意一个去重问题,就是对第二个元素进行去重的时候,如果此时第一个元素已经发生了变化,则需要更新去重条件
- 复杂度:O(B^3), O(1)
- 各种各样的问题:
- 数组排序:Arrays.sort(nums);
- 数据溢出:转换为long的类型,避免溢出问题
- left和right指针去重问题:避免溢出的数组的情况发生
- index2去重问题,当index1更新为新的值的时候,就得更新去重条件了
- 代码:
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
// Step 1: sort the array
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<>();
if(nums.length < 4)
return result;
// step 2:
for(int index1 = 0; index1 < nums.length - 3; index1++){
int lastIndex1 = 0;
if(index1>=1){
while(nums[index1] == nums[index1-1] && index1 < nums.length-3)
index1++;
}
for(int index2 = index1 + 1; index2 < nums.length - 2; index2++){
if(index2>=2 && ((index1 == lastIndex1)|| index1 == 0) ){
while(nums[index2] == nums[index2-1] && index2 < nums.length - 2)
index2++;
}
if(index1!= lastIndex1)
lastIndex1 = index1;
int left = index2+1;
int right = nums.length - 1;
while(left<right){
long sum = (long)nums[index1]+(long)nums[index2]+(long)nums[left]+(long)nums[right];
if(sum > target)
right--;
else if(sum < target)
left++;
else if(sum == target){
// Now get 1+2+3+4 == target
List<Integer> current = new ArrayList<>();
current.add(nums[index1]);
current.add(nums[index2]);
current.add(nums[left]);
current.add(nums[right]);
result.add(current);
// delete replicated index groups
while(nums[left] == nums[left-1] && left < nums.length-1 && left-1 > index2)
left++;
while(nums[right] == nums[right-1] && right >= 3)
right--;
left++;
right--;
}
}
}
}
return result;
}
}
小结
- 总体来说是为了判断一个元素是否在集合里。数组,set,map这三种常用的哈希结构
- 难度不大,但是由于之前对于set和map操作不熟悉,因此遇到各种各样的问题,需要经常复习这块。