力扣labuladong——一刷day13

本文详细介绍了力扣平台上三个经典问题的Java解决方案:两数之和、三数之和和四数之和,分别涉及哈希映射、数组操作和排序技巧。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


一、力扣1. 两数之和

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

        
    }
}

二、力扣15. 三数之和

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

三、力扣18. 四数之和

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        for(int i = 0; i < nums.length - 3; i ++){
            if(target < 0 && nums[i] > 0)return res;
            for(int j = i + 1; j < nums.length - 2; j ++){
                for(int left = j + 1, right = nums.length-1; left < right;){
                    int cur = nums[left] + nums[right] + nums[i] + nums[j];
                    if(cur > target){
                        right --;
                    }else if(cur < target){
                        left ++;
                    }else{
                        List<Integer> li = new ArrayList<>();
                        li.add(nums[i]);
                        li.add(nums[j]);
                        li.add(nums[left]);
                        li.add(nums[right]);
                        res.add(li);
                        while(left < right && nums[left] == nums[left+1])left++;
                        while(left < right && nums[right] == nums[right-1])right--;
                        left ++;
                        right --;
                    }
                }
                while(j < nums.length-2 && nums[j] == nums[j+1])j ++;
            }
            while(i < nums.length-1 && nums[i] == nums[i+1])i ++;
        }
        return res;
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值