四数相加
题目:LeetCode454
链接:https://leetcode.cn/problems/4sum-ii/
看到该题第一次的想法就是用哈希表两两列完所有组合,然后就编程了力扣的第一题,可以开始做了,但是想了下觉得时间复杂度是n^2有点高,但是后来想不出好的办法,就先按自己的想法先做了,代码如下:
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
Map<Integer,Integer> map1 = new HashMap<Integer,Integer>();
Map<Integer,Integer> map2 = new HashMap<Integer,Integer>();
int count=0;
for(int x=0;x<nums1.length;x++){
for(int y=0;y<nums2.length;y++){
int temp=nums1[x]+nums2[y];
map1.put(temp,map1.getOrDefault(temp,0)+1);
}
}
for(int x=0;x<nums3.length;x++){
for(int y=0;y<nums4.length;y++){
int temp=nums3[x]+nums4[y];
map2.put(temp,map2.getOrDefault(temp,0)+1);
if(map1.containsKey(-temp)){
count=count+map1.get(-temp);
}
}
}
return count;
}
}
赎金信
题目:LeetCode383
链接:https://leetcode.cn/problems/ransom-note/
本题感觉比较简单,一下子就有思路了,是之前做过很像的,代码如下
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] letter = new int[26];
for(int i=0;i<magazine.length();i++){
letter[magazine.charAt(i)-'a']++;
}
for(int i=0;i<ransomNote.length();i++){
letter[ransomNote.charAt(i)-'a']--;
}
for(int i=0;i<26;i++){
if(letter[i]<0)return false;
}
return true;
}
}
三数之和
题目:LeetCode15
链接:https://leetcode.cn/problems/3sum/
代码如下:
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
// 枚举 a
for (int first = 0; first < n; ++first) {
// 需要和上一次枚举的数不相同
if (first > 0 && nums[first] == nums[first - 1]) {
continue;
}
// c 对应的指针初始指向数组的最右端
int third = n - 1;
int target = -nums[first];
// 枚举 b
for (int second = first + 1; second < n; ++second) {
// 需要和上一次枚举的数不相同
if (second > first + 1 && nums[second] == nums[second - 1]) {
continue;
}
// 需要保证 b 的指针在 c 的指针的左侧
while (second < third && nums[second] + nums[third] > target) {
--third;
}
// 如果指针重合,随着 b 后续的增加
// 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
if (second == third) {
break;
}
if (nums[second] + nums[third] == target) {
List<Integer> list = new ArrayList<Integer>();
list.add(nums[first]);
list.add(nums[second]);
list.add(nums[third]);
ans.add(list);
}
}
}
return ans;
}
}
四数之和
题目:LeetCode18
链接:https://leetcode.cn/problemset/all/
官方题解代码如下:
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;
}
}
后两题第一次看到的时候有点懵,智能照着题解码,还需要继续学习提升这方面的题目