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 = 2.

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

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

Solution

由于这道题题目并不要求去重,所以我们就不考虑重复。

题目虽然提到了index,但我们发现返回的是个数。因此还是可以先将数组排序,用2Sum的方法。

注意到对于nums[l] + nums[r]

如果已经小于target,那么nums[l] + nums[r - 1], nums[l] + nums[r - 2], nums[l] + nums[r - 3], ...一定也满足条件。所以count += r - l。之后l++,看后一个左指针指向的数。

Time complexity O(n2)

 1 public class Solution {
 2     public int threeSumSmaller(int[] nums, int target) {
 3         Arrays.sort(nums);
 4         int count = 0;
 5         for (int i = 0; i < nums.length; i++) {
 6             int tmpTarget = target - nums[i];
 7             int start = i + 1, end = nums.length - 1;
 8             while (start < end) {
 9                 int sum = nums[start] + nums[end];
10                 if (sum >= tmpTarget) {
11                     end--;
12                 } else {
13                     count += end - start;
14                     start++;
15                 }
16             }
17         }
18         return count;
19     }
20 }
时间: 2024-10-08 15:46:30

3Sum Smaller 解答的相关文章

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

3Sum Closest 解答

Question Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S

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

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

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

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

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 Outpu

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.

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre