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)

 public class Solution {
public int threeSumSmaller(int[] nums, int target) {
Arrays.sort(nums);
int count = 0;
for (int i = 0; i < nums.length; i++) {
int tmpTarget = target - nums[i];
int start = i + 1, end = nums.length - 1;
while (start < end) {
int sum = nums[start] + nums[end];
if (sum >= tmpTarget) {
end--;
} else {
count += end - start;
start++;
}
}
}
return count;
}
}

3Sum Smaller 解答的更多相关文章

  1. 3Sum Closest & 3Sum Smaller

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  2. 259. 3Sum Smaller

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  3. [Locked] 3Sum Smaller

    3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...

  4. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

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

  6. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  7. LeetCode 3Sum Smaller

    原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...

  8. 3Sum Closest 解答

    Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  9. 【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 ...

随机推荐

  1. 哲学家用餐问题的几个解法(c语言实现)

    参考资料: 1.维基百科:哲学家用餐问题 2.Windows的多线程编程

  2. python3-day4(yield)

    1.yield 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一 ...

  3. javaweb文件下载

    最近搞了一下struts文件上传下载了,一个是通过struts自带的类实现的下载方法,一个是通用的下载方法: struts实现: FileDownloadAction.java package com ...

  4. JAVA模拟表单提交

    这是我网上搜的,自己使用也蛮方便,所以上传供大家分享. package wzh.Http;   import java.io.BufferedReader; import java.io.IOExce ...

  5. [原创作品] Express 4.x 接收表单数据

    好久没有写博客,从现在开始,将介绍用nodejs进行web开发的介绍.欢迎加群讨论:164858883. 之前的express版本在接收表单数据时,可以统一用res.params['参数名'],但在4 ...

  6. My way on Linux - [Shell基础] - Bash Shell中判断文件、目录是否存在或者判断其是否具有某类属性(权限)的常用方法

    Conditional Logic on Files # 判断文件是否存在及文件类型 -a file exists. #文件存在 -b file exists and is a block speci ...

  7. 关于asp.net的<%#%>的一些总结

    一.说明 asp特有的控件在前台绑定数据的语法,且必须要调用该控件的DataBind()方法才执行,也可以整个页面数据绑定. 二.注意 1.并不是只有服务器控件才可以使用该语法,当整个页面调用this ...

  8. LNMP优化

        LNMP优化 LNMP优化从系统安全,系统资源占用率,及web服务并发负载这三个方面体现,并   且主要体现在web服务并发负载这一方面.     1:首先进行linux优化加固  Linux ...

  9. Oracle 批量操作

    背景: 前两天由于工作需要做个业务单据接口(支持批量处理),一般有接口发布为批量,但访问数据库时还是一张张单据处理,本次访问数据库也是批量操作.   内容: 研究发现Oracle批量操作有两种:1)B ...

  10. ios--图片处理(修改、保存)UIGraphicsBeginImageContext

    http://www.th7.cn/Program/IOS/201407/250904.shtml