代码随想录算法训练营Day7|454.四数相加II、15. 三数之和 、18. 四数之和

454.四数相加II

        基本思路:先把前两个数组的元素用两个for循环全排列相加,记录在map表中。然后后两个数组仍然用全排列相加,取相反数,在map表中查询有没有对应的数,有则count增加对应的value值。

class Solution {
    public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        Map<Integer, Integer> map1 = new HashMap<>();
        for(int i: nums1){
            for(int j :nums2){
                int temp1 = i + j;
                map1.put(temp1,map1.getOrDefault(temp1,0) + 1);
                
            }
        }
        int count = 0;
        for(int i :nums3){
            for(int j :nums4){
                int temp2 = -i - j;                
                count += map1.getOrDefault(temp2,0);
                
            }
        }
        return count;
    }
}

15. 三数之和

        基本思路:先把数组排序,然后用i指针遍历数组。每次遍历的时候,i指针的下一位是left指针,数组末尾是right指针。如果三个指针指向的数之和大于0,就将right指针左移一位,减小和;反之将left指针右移;如果恰好等于0,将三元组添加到结果列表当中,并且判断左右指针即将移向的下一位是否和当前位置相等,如果相等,则跳过下一位(因为三元组不能重复,该步骤为了去重,此处用while不用if),最后将左右指针继续移向下一位,判断还有不同的三元组等于0。

        去重步骤:注意对a的去重。以及b和c的去重。

class Solution {
	public List<List<Integer>> threeSum(int[] nums) {
		Arrays.sort(nums);
		List<List<Integer>> res = new ArrayList<>();
		if(nums.length<2){
			return res;
		}
		for(int i= 0 ; i < nums.length;i++){
			int left = i+1;
			int right = nums.length -1;
			if(i >0 &&nums[i] == nums[i - 1]){
				continue;
			}
			if(nums[i]>0){
				return res;
			}
			while(left <right){
				if(nums[i] + nums[left] + nums[right]>0){
					right--;
					continue;
				}else if(nums[i] + nums[left] + nums[right] < 0){
					left++;
					continue;
				}else{
					res.add(Arrays.asList(nums[i],nums[left],nums[right]));
					while(left < right && nums[left] == nums[left +1]){
						left++;
					}
					while(right > left && nums[right] == nums[right -1]){
						right--;
					}
					left++;
					right--;
				}
			}
		}

		return res;

	}
}

18. 四数之和

        在三数之和基础上套一层for循环,注意两个事情:一个是排序完之后,如果当前元素大于零,并且当前元素大于目标值,则说明后续不可能相加小于目标数字。对于下一层循环:判断当前数字是否大于目标值以及当前值是否大于零,需要以宏观的角度看待,也就是两个数字之和。

        还有如果下一个数字和当前数字一样需要跳过,不要用while,因为continue会跳进下一次循环,陷入死循环。

        while在判断时候会先执行判断条件的语句(),i++要改成i+1;不然会执行自增操作。

class Solution {
	public List<List<Integer>> fourSum(int[] nums, int target) {
		Arrays.sort(nums);
		List<List<Integer>> res = new ArrayList<>();
		for(int i = 0; i < nums.length; i++){
			if(nums[i] > target && nums[i] > 0){
				return res;
			}
			if(i > 0 && nums[i] == nums[i-1]){
				continue;
			}
			for(int j = i + 1; j < nums.length;j++){
				if(nums[i] + nums [j] > target && nums[j] > 0){
					return res;
				}
				if(j > i+1 && nums[j] == nums[j-1]){
					continue;
				}
				int l = j + 1;
				int r = nums.length - 1;
				while(l < r){
					if(nums[i] + nums[j] + nums[l] + nums[r] < target){
						l++;
					}else if(nums[i] + nums[j] + nums[l] + nums[r] > target){
						r--;
					}else{
						res.add(Arrays.asList(nums[i], nums[j], nums[l], nums[r]));
						while( l < r && nums[l] == nums[l+1]){
							l++;
						}
						while( l < r && nums[r] == nums[r-1]){
							r--;
						}
						l++;
						r--;
					}
				}
			}
		}
		return res;
	}
}

       

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值