Leecode.454 四数相加Ⅱ
题目链接:四数相加Ⅱ
思路
4个数组全遍历的话是n的4次方,分成1个数组和3个数组的话就是n的3次方,最优解是分为2,2两组,时间复杂度为n的2次方。四数相加等于0,所以num1 + num2 = - (num3+ num4),可以用map来记录sum12和sum34出现的次数,最后相乘相加得到返回值
解题方法
同思路,注意map的遍历方法!
复杂度
- 时间复杂度:
O(n2)
- 空间复杂度:
O(n2)
Code
class Solution {
public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
HashMap<Integer, Integer> mapAB = new HashMap<>();
HashMap<Integer, Integer> mapCD = new HashMap<>();
int result = 0;
for(int i = 0; i < nums1.length; i++){
for(int j = 0; j < nums1.length; j++){
int sumAB = nums1[i] + nums2[j];
int sumCD = nums3[i] + nums4[j];
mapAB.put(sumAB, mapAB.getOrDefault(sumAB, 0) + 1);
mapCD.put(sumCD, mapCD.getOrDefault(sumCD, 0) + 1);
}
}
for(Map.Entry<Integer, Integer> entry : mapAB.entrySet()){
int temp = 0 - entry.getKey();
if(mapCD.containsKey(temp)){
result += mapCD.get(temp)*entry.getValue();
}
}
return result;
}
}
Leecode.383 赎金信
题目链接:赎金信
思路
与242 有效的字母异位词完全相同,请看有效的字母异位词
解题方法
同思路
复杂度
- 时间复杂度:
O(m+n)
- 空间复杂度:
O(|s|),s=26
Code
class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
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']--;
if(arr[ransomNote.charAt(i)-'a'] < 0){
return false;
}
}
return true;
}
}
Leecode.15 三数之和
题目链接:三数之和
思路
这个题哈希法太过复杂,所以使用三指针法,思路就是用i做循环,left = i+1做左指针,right = nums.length-1做右指针。此题关键在于去重,除去重复的三元组,还不能将重复数值的三元组除去,所以i指针去重的方法是关键。
解题方法
首先将数组排序,然后用i遍历数组,当nums[i]>0时,说明三数之和再也不可能为0,直接返回值。接下来对i进行去重,主要要与i-1对比,不能与i+1对比,i+1是left指针,这样直接就把可能的结果给漏掉了。之后开始判断三数之和,移动left和right指针,当三数之和为0时,用Arrays.asList方法创建三元组列表加入结果,并且对left和right指针进行去重,最后不要忘记对left和right再进行一次移动,不然还停留在上一个重复的值上。
复杂度
- 时间复杂度:
O(n2)
- 空间复杂度:
O(1)
Code
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++){
//如果i指针所指的值大于0,那么三数肯定都大于0,所以直接返回结果
if(nums[i] > 0){
return result;
}
//对i进行去重
if(i > 0 && nums[i] == nums[i-1]){
continue;
}
int left = i + 1;
int right = nums.length - 1;
while(left < right){
//开始判断三数之和,移动left和right指针
if(nums[i] + nums[left] + nums[right] > 0){
right--;
}else if(nums[i] + nums[left] + nums[right] < 0){
left++;
}else{
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
while(right > left && nums[left] == nums[left+1]){
left++;
}
while(right > left && nums[right] == nums[right-1]){
right--;
}
right--;
left++;
}
}
}
return result;
}
}
Leecode.18 四数之和
题目链接:四数之和
思路
比三数之和多了一个数,那么就多一层循环就可以了,关键在于四数相加有可能会超过int的最大值,所以要转换成long值。
解题方法
同三数之和
复杂度
- 时间复杂度:
O(n3)
- 空间复杂度:
O(1)
Code
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> reuslt = new ArrayList<>();
Arrays.sort(nums);
for(int i = 0; i < nums.length-1; i++){
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(left < right){
long sum = (long)nums[i] + nums[j] + nums[left] + nums[right];
if(sum > target){
right--;
}else if(sum < target){
left++;
}else{
reuslt.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
while(left < right && nums[left] == nums[left+1]){
left++;
}
while(left < right && nums[right] == nums[right-1]){
right--;
}
right--;
left++;
}
}
}
}
return reuslt;
}
}