Leetcode 454.四数相加II
题目链接:454.四数相加II
思路:
没思路。看了题解,这道题其实就是两数相加的拓展。
解题:失败
代码实现
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer, Integer> map = new HashMap<>();
int count = 0;
for(int a : nums1){
for(int b : nums2){
int sum = a + b;
map.put(sum, map.getOrDefault(sum, 0) + 1);
}
}
for(int c : nums3){
for(int d : nums4){
int target = 0 - (c + d);
if(map.containsKey(target)){
count += map.get(target);
}
}
}
return count;
}
}
Leetcode 383. 赎金信
题目链接:383. 赎金信
思路:
简单题。这道题目和 Leetcode 242.有效的字母异位词 很像。
解题:成功
代码实现
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
// 这一个if判断,算是优化吧
if(ransomNote.length() > magazine.length()){
return false;
}
int[] arr = new int[26];
for(int i = 0; i < magazine.length(); i++){
arr[magazine.charAt(i) - 'a']++;
}
for(int i = 0; i < ransomNote.length(); i++){
arr[ransomNote.charAt(i) - 'a']--;
}
for(int i = 0; i < arr.length; i++){
if(arr[i] < 0){
return false;
}
}
return true;
}
}
Leetcode 15. 三数之和
题目链接:15. 三数之和
思路:
难点在于要去重。
解题:失败
代码实现
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < n - 2; i++){
if(nums[i] > 0){
return ans;
}
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
int left = i + 1;
int right = n - 1;
while(left < right){
if(nums[i] + nums[left] + nums[right] > 0){
right--;
}
else if(nums[i] + nums[left] + nums[right] < 0){
left++;
}
else{
// 简洁写法:res.add(Arrays.asList(nums[i], nums[left], nums[right]));
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);
temp.add(nums[left]);
temp.add(nums[right]);
ans.add(temp);
left++;
right--;
while(left < right && nums[left] == nums[left-1]){
left++;
}
while(left < right && nums[right] == nums[right+1]){
right--;
}
}
}
}
return ans;
}
}
Leetcode 18. 四数之和
题目链接:18. 四数之和
思路:
- 做了 “15. 三数之和” 之后,就有思路了。五数之和、六数之和等等都采用这种解法。
- 之前的题目:454.四数相加II (opens new window),相对于本题简单很多。因为本题是要求在一个集合中找出四个数相加等于target,同时四元组不能重复。而454.四数相加II (opens new window)是四个独立的数组,只要找到A[i] + B[j] + C[k] + D[l] = 0就可以,不用考虑有重复的四个元素相加等于0的情况,所以相对于本题还是简单了不少。
解题:成功
代码实现
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
int n = nums.length;
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < n - 3; i++){
// 剪枝
if(nums[i] > target && nums[i] >= 0){
break;
}
// 去重
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
for(int j = i + 1; j < n; j++){
if(nums[i] + nums[j] > target && nums[i] + nums[j] >= 0){
// 这里不能写 return ans
break;
}
if(j > i + 1 && nums[j] == nums[j-1]){
continue;
}
int left = j + 1;
int right = n - 1;
while(left < right){
// 注意数据溢出
long sum = (long)nums[i] + nums[j] + nums[left] + nums[right];
if(sum < target){
left++;
}
else if(sum > target){
right--;
}
else{
ans.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
left++;
right--;
while(left < right && nums[left] == nums[left - 1]){
left++;
}
while(left < right && nums[right] == nums[right + 1]){
right--;
}
}
}
}
}
return ans;
}
}