zoukankan      html  css  js  c++  java
  • LeetCode 15. 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: 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]
    ]
    

    题目标签:Array
      这道题目给了我们一个nums array, 让我们找到所有任意三个数字加起来等于0的可能性,但是不能重复。这道题目我们要运用Two Sum(Two pointers)方法来帮助完成,那么我们首先就要sort array。这样才可以利用Two Sum。也可以帮助避免重复的数字。然后遍历nums array,这里只需要遍历到最后倒数第三个就可以了,因为一共需要三个数字,后面只有两个数字的时候,没必要。对于每一个数字,把它* -1,比如说题目中的例子: -4 -1 -1 0 1 2, 把-4 * -1 = 4, 然后需要在后面的array 里找到两个数字之和等于4的。这样就是在做Two Sum题目了。如果遇到-1 -1这种情况,第一个-1 我们需要,在后面array 里找两数之和等于1的。当遇到第二个重复的,直接skip。同样的,在后面的array里找两数之和的话,遇到第二个重复的也跳过。
     
     

    Java Solution:

    Runtime beats 82.41% 

    完成日期:07/11/2017

    关键词:Array

    关键点:利用TwoSum(two pointers)来辅助

     1 public class Solution 
     2 {
     3     public List<List<Integer>> threeSum(int[] nums) 
     4     {
     5         Arrays.sort(nums);
     6         List<List<Integer>> res = new ArrayList<>();
     7         
     8         for(int i=0; i<nums.length-2; i++) // no need to find twoSum if rest array size is only 1 or 0
     9         {
    10             if(i > 0 && nums[i] == nums[i-1]) // if previous num is same, skip this num
    11                 continue;
    12             
    13             // for each num, find the twoSum which equal -num in the rest array
    14             int left = i + 1;
    15             int right = nums.length-1;
    16             int sum = nums[i] * -1;
    17             
    18             while(left < right)
    19             {
    20                 if(nums[left] + nums[right] == sum) // find the two 
    21                 {
    22                     res.add(Arrays.asList(nums[i], nums[left], nums[right])); // ascending order
    23                     left++;
    24                     right--;
    25                     
    26                     while(left < right && nums[left] == nums[left-1]) // if next num is same as this, skip this
    27                         left++;
    28                     while(left < right && nums[right] == nums[right+1])
    29                         right--;
    30                 }
    31                 else if(nums[left] + nums[right] > sum) // meaning need smaller sum
    32                     right--;
    33                 else // nums[left] + nums[right] < sum, meaning need larger sum
    34                     left++;
    35             }
    36             
    37         }
    38         
    39         return res;
    40     }
    41 }

    参考资料:

    https://discuss.leetcode.com/topic/8125/concise-o-n-2-java-solution

    LeetCode 算法题目列表 - LeetCode Algorithms Questions List

     
  • 相关阅读:
    网站开发感悟
    jQuery之字体大小的设置
    jQuery之load方法
    jQuery之get方法
    MongoVUE的使用
    jQuery之ajax删除
    jQuery之选项卡的实现
    jQuery之简单的表单验证
    jQuery滑动效果实例
    jQuery之下拉框左右选择
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7153565.html
Copyright © 2011-2022 走看看