zoukankan      html  css  js  c++  java
  • leetcode15

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

    Note:

    The solution set must not contain duplicate triplets.

    Example:

    Given array nums = [-1, 0, 1, 2, -1, -4],
    
    A solution set is:
    [
      [-1, 0, 1],
      [-1, -1, 2]
    ]
    
    Accepted
    522,004
    Submissions
    2,197,404
     
    解答:
    /*
    首先对原数组进行排序,然后开始遍历排序后的数组,这里注意不是遍历到最后一个停止,
    而是到倒数第三个就可以了,中间如果遇到跟前一个数相同的数字就直接跳过。
    对于遍历到的数,如果大于0则跳到下一个数,因为三个大于0的数相加不可能等于0;
    否则用0减去这个数得到一个sum,我们只需要再之后找到两个数之和等于sum即可,
    这样一来问题又转化为了求two sum,
    这时候我们一次扫描,找到了等于sum的两数后,加上当前遍历到的数字,
    按顺序存入结果中即可,然后还要注意跳过重复数字。时间复杂度为 O(n2)
    */
    
    class Solution {
    
        public static void main(String[] args) {
            int[] nums = {-2, -3, 4, -5, 1};
            List<List<Integer>> res = threeSum(nums);
        }
    
        public static List<List<Integer>> threeSum(int[] nums) {
            List<List<Integer>> result = new ArrayList<>();
            
            if(nums == null || nums.length < 3) {
                return result;
            }
            
            // 对数组进行排序
            Arrays.sort(nums);
            // 从前向后进行遍历,
            for(int i = 0; i < nums.length-2; i++) {
                if(nums[i] > 0) {
                    break;
                }
                
                if(i > 0 && nums[i-1] == nums[i]) {
                    continue;
                }
                
                int sum = -nums[i];
                int left = i + 1;
                int right = nums.length-1;
                
                while(left < right) {
                    if(nums[left] + nums[right] == sum) {
                        ArrayList<Integer> t = new ArrayList<>();
                        t.add(nums[i]);
                        t.add(nums[left]);
                        t.add(nums[right]);
                        
                        result.add(t);
                        
                        while(left < right && nums[left++] == nums[left]) {
                            
                        }
                        
                        while(left < right && nums[right--] == nums[right]) {
                            
                        } 
                        
                    } else if(nums[left] + nums[right] < sum) {
                        while(left < right && nums[left++] == nums[left]) {
                            
                        }
                    } else {
                        while(left < right && nums[right--] == nums[right]) {
                            
                        }  
                    }
                } // end of while
            }// end of for
            
            return result;
            
        }
    }
    

      

  • 相关阅读:
    ASP.NET的内置对象 —— Response 对象
    dd命令测试硬盘IO
    Linux netstat命令详解
    tpcc-mysql安装、使用、结果解读
    【MySQL案例】error.log的Warning:If a crash happens thisconfiguration does not guarantee that the relay lo(转)
    mysql5.6主从
    无法远程访问Mysql
    pythonMD5加密
    python随机验证码函数
    log buffer space事件(转)
  • 原文地址:https://www.cnblogs.com/wylwyl/p/10726416.html
Copyright © 2011-2022 走看看