259. 3Sum Smaller小于版3sum

[抄题]:

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.

Example:

Input: nums = [-2,0,1,3], and target = 2
Output: 2
Explanation: 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?

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

想到了化成2sum变形,但是不知道怎么改变第三个变量的值:for一遍就行了

[英文数据结构或算法,为什么不用别的数据结构或算法]:

nsum指针对撞必须要排序,初始化时就写,别忘了!

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. right很大时,right-left都小于了,right变小时就更可以了。所以count+=right-left一段
  2. 必须要在left < right的while循环前提条件下进行

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

怎么改变第三个变量的值:for一遍就行了

[复杂度]:Time complexity: O(n2) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
    public int threeSumSmaller(int[] nums, int target) {
        //corner case
        if (nums == null || nums.length == 0) return 0;

        //initialization: count = 0, sort
        int count = 0;
        Arrays.sort(nums);

        //for loop and find i, left = i + 1, right = len - 1
        for (int i = 0; i < nums.length - 2; i++) {
            int left = i + 1; int right = nums.length - 1;
            //while loop
            while (left < right) {
                //if and adjust left, right, add count
                if (nums[i] + nums[left] + nums[right] < target) {
                //add the whole period
                count += right - left;
                left++;
            }
            else {
                right--;
            }
        }

    }
        //return
        return count;
}
}

原文地址:https://www.cnblogs.com/immiao0319/p/9465127.html

时间: 2024-11-05 17:21:51

259. 3Sum Smaller小于版3sum的相关文章

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. Retu

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 conditionnums[i] + nums[j] + nums[k] < target. For example, given nums = [-2, 0, 1, 3], and target = 2. Retur

Leetcode 259. 3Sum Smaller

1 class Solution(object): 2 def threeSumSmaller(self, nums, target): 3 """ 4 :type nums: List[int] 5 :type target: int 6 :rtype: int 7 """ 8 nums.sort() 9 n = len(nums) 10 ans = 0 11 12 for k in range(n): 13 i, j = k+1, n-1 1

259 [LeetCode] 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.

3Sum Smaller 解答

Question 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 

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. Retu

3Sum Smaller -- LeetCode

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. Retu

LeetCode 3Sum Closest 最近似的3sum(2sum方法)

题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). 1 int threeSumClosest(vector<int>& nums, int target) { 2 int sum=nums[0]+nums[1]+nums[2]; 3 sort(nums.begin(), nums.end()); 4 for(int i=0; i<nums.size()-2; i++) 5 { 6 int p=i+1, q=nums.size()

[Swift]LeetCode923.3Sum具有多重性 | 3Sum With Multiplicity

Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i < j < kand A[i] + A[j] + A[k] == target. As the answer can be very large, return it modulo 10^9 + 7. Example 1: Input: A = [1,1,2,2,3,3,4,4,5,5], targe