LeetCode 259. 3Sum Smaller (三数之和较小值) $

Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

For example, given nums = [-2, 0, 1, 3], and target = 2.

Return 2. Because there are two triplets which sums are less than 2:

[-2, 0, 1]
[-2, 0, 3]

Follow up:
Could you solve it in O(n2) runtime?


 

题目标签:Array

  题目给了我们一个 nums array 和一个 target, 要我们找到 任何三个数字之和 小于 target。题目要求了要 O(n^2) 的runtime, 所以三个 for loop 就不行了,虽然也能通过。

  对于这种3Sum,要在O(n^2) 条件下的,都是要一个遍历,然后剩下的n*n 用 two pointers 来代替。换句话说,就是第一个遍历来选定第一个数字,剩下的就是2Sum的题目了,而且要用 two pointers 来做。

  来分析一下这题的情况:

  首先排序,从小到大。

  选好第一个数字,然后在剩下的数字里,选好left 和 right:

          case 1:如果当left 数字 + right 数字 < target - 第一个数字  的话, 意味着符合条件,并且left 和 所有在right 左边的数字搭配 也都符合,因为排序从小到大,所以直接把所有的可能性都加上, right - left;然后left++ 到下一个数字继续。

          case 2:如果当left 数字 + right 数字 >= target - 第一个数字  的话, 意味着目前数字组合太大,需要更小的数字,所以right--。

 

Java Solution:

Runtime beats 79.74% 

完成日期:09/08/2017

关键词:Array

关键点:一个遍历(选中第一个数字):two pointers 代替 n*n

 

 1 class Solution 
 2 {
 3     public int threeSumSmaller(int[] nums, int target) 
 4     {
 5         Arrays.sort(nums);
 6         int res = 0;
 7         
 8         for(int i=0; i<nums.length-2; i++) // no need to iterate last two numbers 
 9         {
10             int left = i+1;
11             int right = nums.length-1;
12             int tempTarget = target - nums[i];
13             
14             while(left < right)
15             {    // if find two numbers < tempTarget
16                 if(nums[left] + nums[right] < tempTarget)
17                 {
18                     res += (right - left); // all the left side numbers are also < tempTarget
19                     left++; // left one with each number from right rest are done, move to next left one
20                 }
21                 else // meaning current two numbers sum is too big, need smaller one
22                     right--;
23             }
24             
25         }
26         
27         return res;
28     }
29 }

参考资料:N/A

 

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

 

posted @ 2017-09-09 07:33  Jimmy_Cheng  阅读(434)  评论(0编辑  收藏  举报