454.四数相加II
思路:大体思路看完后,代码能写出来一部分,但是之前不知道map.getOrDefault方法,所以卡了一会,再就是,匹配剩下两个数组的时候没有想到直接取减,反而准备再建一个hashmap,思路还是有些局限。
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> map1 = new HashMap<>(); // 前两个数组构成的map
int n = nums1.length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int sum = nums2[j] + nums1[i];
map1.put(sum, map1.getOrDefault(sum, 0) + 1);
}
}
for (int k = 0; k < n; k++) {
for (int l = 0; l < n; l++) {
count += map1.getOrDefault(0 - nums4[l] - nums3[k], 0);
}
}
return count;
}
}
383. 赎金信
思路:和昨天做的字母题基本思路一样,以后碰见类似的题可以直接想到用数组哈希表的方式。
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']--;
}
for (int count : record) {
if (count < 0){
return false;
}
}
return true;
}
}
15. 三数之和
思路:这道题感觉比较复杂,看了k神的题解之后,试图自己重复一下整个的思路:
主要看代码注释部分吧。
while(i < j && nums[i] == nums[++i])
// 这是一个指针前进并去重的很好的写法,省略循环体
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
for(int k = 0; k < nums.length - 2; k++){
if(nums[k] > 0) break;
if(k > 0 && nums[k] == nums[k - 1]) continue;
int i = k + 1, j = nums.length - 1;
while(i < j){
int sum = nums[k] + nums[i] + nums[j];
if(sum < 0){
while(i < j && nums[i] == nums[++i]);
} else if (sum > 0) {
while(i < j && nums[j] == nums[--j]);
} else {
res.add(new ArrayList<Integer>(Arrays.asList(nums[k], nums[i], nums[j])));
while(i < j && nums[i] == nums[++i]);
while(i < j && nums[j] == nums[--j]);
}
}
}
return res;
}
}
18. 四数之和
思路:和上一条的思路类似,尝试自己写出完整代码,感觉还是有一点点困难,回头再重新做。
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> quadruplets = new ArrayList<List<Integer>>();
if (nums == null || nums.length < 4) {
return quadruplets;
}
Arrays.sort(nums);
int length = nums.length;
for (int i = 0; i < length - 3; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
}
if ((long) nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
break;
}
if ((long) nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target) {
continue;
}
for (int j = i + 1; j < length - 2; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue;
}
if ((long) nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) {
break;
}
if ((long) nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target) {
continue;
}
int left = j + 1, right = length - 1;
while (left < right) {
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
if (sum == target) {
quadruplets.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while (left < right && nums[left] == nums[left + 1]) {
left++;
}
left++;
while (left < right && nums[right] == nums[right - 1]) {
right--;
}
right--;
} else if (sum < target) {
left++;
} else {
right--;
}
}
}
}
return quadruplets;
}
}