zoukankan      html  css  js  c++  java
  • LeetCode 15. 3Sum

    原题链接在这里:https://leetcode.com/problems/3sum/

    题目:

    Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

    Note:

    • Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
    • The solution set must not contain duplicate triplets.
        For example, given array S = {-1 0 1 2 -1 -4},
    
        A solution set is:
        (-1, 0, 1)
        (-1, -1, 2)

    题解:

    Two Sum是这道题的子问题,但是对于3Sum来说又不方便用HashMap, 因为不方便存入相同的key, e.g -1,-1,2.

    这道题需要先进行排序.

    对于0 到 length-3 的每一个数nums[i]后面生成两个指针 j, k.

    j 指向当前元素的下一元素 j = i+1; k指向最后一个元素k = nums.length-1.

    判断三个数的和 nums[i] + nums[j] + nums[k] 是否是target,若比target小,j 指针后移; 若比target大, k指针前移。

    Note:1. for loop 是注意时候会有溢出,因为会用到至少后面两个数,所以终止条件应该是 i<nums.length - 2;

    2. res中不能有duplicates, 所依若是nums[i] == nums[i-1], 直接continue.

    3. 最后一个else 中记得加入 j++;k--; 否则就是infinite loop, 并且注意j++;k--时是否会遇到相同元素,遇到需跳过。

    否则会加入重复的结果,e.g. -2, 0, 0, 2, 2 会加两个 -2,0,2 的 subList.

    Time Complexity: O(n^2) 因为对于每一个i 后面while loop用了n的时间. Space: O(1), regardless res.

    AC  Java: 

     1 class Solution {
     2     public List<List<Integer>> threeSum(int[] nums) {
     3         List<List<Integer>> res = new ArrayList<List<Integer>>();
     4         if(nums == null || nums.length == 0){
     5             return res;
     6         }
     7         
     8         Arrays.sort(nums);
     9         for(int i = 0; i<nums.length-2; i++){
    10             if(i>0 && nums[i]==nums[i-1]){
    11                 continue;
    12             }
    13             
    14             int j = i+1;
    15             int k = nums.length-1;
    16             while(j<k){
    17                 int sum = nums[i] + nums[j] + nums[k];
    18                 if(sum > 0){
    19                     k--;
    20                 }else if(sum < 0){
    21                     j++;
    22                 }else{
    23                     res.add(Arrays.asList(nums[i], nums[j], nums[k]));
    24                     j++;
    25                     k--;
    26                     
    27                     while(j<k && nums[j]==nums[j-1]){
    28                         j++;
    29                     }
    30                     
    31                     while(j<k && nums[k]==nums[k+1]){
    32                         k--;
    33                     }
    34                 }
    35             }
    36         }
    37         
    38         return res;
    39     }
    40 }

     跟上3Sum Smaller3Sum Closest4Sum3Sum With Multiplicity.

  • 相关阅读:
    如何让nacos中服务注册的ip是自定义的网段ip ( docker swarm )
    docker swarm集群节点打标签
    mysql 8 设置大小写不敏感
    mysql 8 安装&账户权限控制
    I/O(输入/输出)---序列化与反序列化
    I/O(输入/输出)---字节流与字符流
    I/O(输入/输出)---File类
    转发与重定向的区别
    application的使用(实现计数器)
    HTML的相关路径与绝对路径的问题---通过网络搜索整理
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4825052.html
Copyright © 2011-2022 走看看