LeetCode 两数之和 + 三数之和

两数之和 简单题

思路:一个Map,key是数值,value是该数值对应的下标,遍历的时候判断一下当前数组下标对应的值在map里有没有可组合成target的(具体体现为在map里找target-nums【i】),如果有,直接返回,没有的话就加进去,继续找。

需要掌握的方法:map的get和containsKey

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

三数之和 双指针 中等题

思路:严格上来说算三个指针,一个i是维护当前遍历到的数字下标,之后的l和r指针是从i所处位置开始,在i + 1 到len - 1这个区间中寻找nums[i]的相反数。

class Solution {
    // -4 -1 -1 0 1 2 
    // 
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(nums);
        int len = nums.length;
        if(len < 2){
            return res;
        }
        for(int i = 0;i < len;++i){
            int l = i + 1;
            int r = len - 1;
            if( i != 0 && nums[i] == nums[i-1]){//与前一个数重复需要调过,不然会出现重复的三元组
                continue;
            }
            while(l < r){
                if(nums[i] + nums[l] + nums[r] == 0){
                    if(l > i + 1 && nums[l] == nums[l-1] ){//这里也是同理
                        l ++ ;
                        continue;
                    }
                    List<Integer> tmp = new ArrayList<>();
                    tmp.add(nums[i]);
                    tmp.add(nums[l]);
                    tmp.add(nums[r]);
                    l++;
                    r--;
                    //左右指针都要变,因为l变大的话,还希望结果有可能为0,r必须变小(意味着nums[r]变小)
                    res.add(tmp);
                }
                else if(nums[i] + nums[l] + nums[r] < 0){
                    //nums[l] 太小了,把它变大一点
                     l ++;
                }   
                else{
                    r --;
                }
            }
        }
        return res;
    }
}

相关推荐

  1. leetcode-之和

    2024-06-07 19:38:04       46 阅读
  2. LeetCode之和

    2024-06-07 19:38:04       30 阅读
  3. LeetCode 之和

    2024-06-07 19:38:04       25 阅读
  4. LeetCode之和

    2024-06-07 19:38:04       23 阅读
  5. leetcode 之和

    2024-06-07 19:38:04       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 19:38:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 19:38:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 19:38:04       87 阅读
  4. Python语言-面向对象

    2024-06-07 19:38:04       96 阅读

热门阅读

  1. OpenCV 4.X 使用CvxText在图片显示汉字

    2024-06-07 19:38:04       19 阅读
  2. Less is more VS 精一 [生活感悟]

    2024-06-07 19:38:04       28 阅读
  3. 【学习笔记】Redis-AOF日志重写的机制

    2024-06-07 19:38:04       32 阅读
  4. 【路径规划】三维深度矩阵寻路算法

    2024-06-07 19:38:04       32 阅读
  5. Python做Web:深度剖析与多维评价

    2024-06-07 19:38:04       28 阅读
  6. amis源码 onEvent事件动作 和 Action行为按钮解析

    2024-06-07 19:38:04       36 阅读
  7. 1348:【例4-9】城市公交网建设问题

    2024-06-07 19:38:04       30 阅读
  8. Ant-Design-Vue动态表头并填充数据

    2024-06-07 19:38:04       31 阅读
  9. System-Verilog

    2024-06-07 19:38:04       31 阅读
  10. 重新学习stm32(序)stm32简介

    2024-06-07 19:38:04       26 阅读
  11. Redux 入门+面试题

    2024-06-07 19:38:04       31 阅读