454.四数相加II
题目
文章讲解
视频讲解
思路:
使用map分别对两组数组值和出现次数进行存放,需要注意的是,最后计数是不是简单的res++;应该是对所有匹配的情况进行记录,应该是 res+=map.get(0-temp);
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;
}
}
383. 赎金信
题目
文章讲解
思路:在范围大的里面去找小的
注意:foreach主要用于数组和集合遍历的增强型for循环,可以认为它是普通for循环的增强版,主要是用于处理数组或集合中的元素,而不必指定数组或集合的下标,字符串不能用foreach结构
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] record = new int[26];
for (int i = 0; i < magazine.length(); i++) {
record[magazine.charAt(i) - 'a']++;
}
for (int j = 0; j < ransomNote.length(); j++) {
record[ransomNote.charAt(j) - 'a']--;
if (record[ransomNote.charAt(j) - 'a'] < 0) {
return false;
}
}
return true;
}
}
15. 三数之和
- 思路:创建一个二维数组,用来存放一组结果的三个值。先对数组进行排序,使用双指针思路,左右夹击找目标元组。
- 注意:
- 三数之和结果中不能有重复的元组,但是元组中可以有重复的元素。判断条件应该为 nums[i] == nums[i - 1],若是nums[i] == nums[i + 1]),则例:[-1,-1,2] 则会被跳过。
- Arrays.asList:当需要从一个数组或者其他集合类创建一个不可变的 List 集合时,我们可以使用 Arrays.asList() 来方便地转换
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 (right > left) {
int sum = nums[i] + nums[left] + nums[right];
if (sum < 0) {
left++;
} else if (sum > 0) {
right--;
} else {
result.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++;
}
}
}
return result;
}
}
18. 四数之和
题目
文章讲解
视频讲解
思路:和三数之和的原理一样,但是需要多加一层循环,也多加了一层剪枝判断
需要注意:涉及了long类型
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> reault = new ArrayList<>();
Arrays.sort(nums);// 排序
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0 && nums[i] > target) {// 剪枝
return reault;
}
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 {
reault.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while (right > left && nums[left] == nums[left + 1]) {
left++;
}
while (right > left && nums[right] == nums[right - 1]) {
right--;
}
left++;
right--;
}
}
}
}
return reault;
}
}
总结
- 哈希策略属于空间换取时间,如果可以使用数组解决,更建议使用数组。
- 不同类型的对象使用不同的方法来获取其长度或大小。length适用于数组,length()适用于字符串,size()适用于集合类,确保使用正确的方法来获取相应对象的长度或大小