Leetcode刷题记录(数组)两数之和、三数之和、四数之和

前言

该文仅作为作者记录学习的笔记,如有错误,敬请指出


一、两数之和

该题主要思路:
先通过循环遍历第一个数,再利用containsKet方法找到另一个数
然后以<值,下标>的形式存入HashMap中

为什么不一开始将所有数以这种方式存入HashMap?
因为,这道题每一个数只能选一遍,这样做是为了防止一个数被选两遍

代码:

class Solution {
    public int[] twoSum(int[] nums, int target) {
       Map<Integer,Integer> map = new HashMap<Integer,Integer>();
       for(int i = 0 ; i < nums.length; i++){
           if(map.containsKey(target - nums[i])){
               return new int[]{map.get(target - nums[i]),i};
           }
           map.put(nums[i],i);
       }
       return new int[0];
    }
}

二、三数之和

这里借鉴LeetCode的思路(几乎都是“借鉴”)

要想不重复,我们就可以先排序,保证三个数是:a<b<c
在这个基础上,将第二个数和第三个数统一处理,使得时间复杂度降为n2
因为我们的数组现在是递增的,且我们前两个数都是从头遍历的,且有唯一的a+b+c=0
当a或者b往后遍历成a1或b1时,有a1>a或b1>b,此时要满足三数之和还等于0,则需要c变小,所以我们的c要变小,所以需要从尾部开始往前遍历
当c的下标等于b时还没有满足条件时,说明前两个数还不够大,直接break跳出循环即可

注意:跳出循环后的上一层需要重新初始化第三个数c的下标为最后一个

代码:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        for(int i = 0;i<nums.length;i++){
            int k = nums.length-1;
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            int target = -nums[i];
            for(int j = i+1;j<nums.length;j++){
                if(j>i+1 && nums[j] == nums[j-1]){
                    continue;
                }
                while(k>j && nums[j]+nums[k]>target){
                    k--;
                }
                if(k == j){
                    break;
                }
                if(nums[j]+nums[k] == target){
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(nums[i]);
                    list.add(nums[j]);
                    list.add(nums[k]);
                    ans.add(list);
                }
            }
        }
        return ans;
    }
}

三、四数之和

与三数之和同理

代码:

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        int n = nums.length -1;
        Arrays.sort(nums);
        for(int first = 0 ; first<=n;first++){
            if(first >0 && nums[first] == nums[first-1]){
                continue;
            }
            for(int second = first + 1 ; second<=n; second++){
                int fourth = n;
                if(second > first+1 && nums[second] == nums[second-1]){
                    continue;
                }
                for(int third = second + 1 ; third<=n ; third++){
                if(third > second+1 && nums[third] == nums[third-1]){
                    continue;
                }    
                while(third<fourth && nums[first]+nums[second]+nums[third]+nums[fourth]>target){
                    fourth--;
                }
                if(third == fourth){
                    break;
                }
                if(nums[first]+nums[second]+nums[third]+nums[fourth] == target){
                     List<Integer> list = new ArrayList<Integer>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    list.add(nums[fourth]);
                    ans.add(list);
                }
                }
            }
        }
        return ans;
    }
}

总结

但愿吉祥

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值