题目:

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.

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?

链接: http://leetcode.com/problems/3sum-smaller/

题解:

要求O(n2)求3sum smaller。这里我们依然用类似3Sum的方法,但由于只需要求count,而不用求出每个组合,我们可以作到O(n2)。方法还是用2个指针前后夹逼,当i, lo, hi这个组合满足条件时,在[lo, hi]这个闭合区间内的所有组合也应该满足条件,所以我们这里可以直接count += hi - lo, 然后lo++,增大三个值的和来继续尝试,假如不满足条件,则hi--来缩小三个值的和。

Time Complexity - O(n2), Space Complexity - O(1)

public class Solution {
public int threeSumSmaller(int[] nums, int target) {
if(nums == null || nums.length == 0)
return 0;
Arrays.sort(nums);
int count = 0; for(int i = 0; i < nums.length - 2; i++) {
int lo = i + 1, hi = nums.length - 1;
while(lo < hi) {
if(nums[i] + nums[lo] + nums[hi] < target) {
count += hi - lo;
lo++;
} else {
hi--;
}
}
} return count;
}
}

Reference:

https://leetcode.com/discuss/55602/just-another-pointer-direction-which-think-more-intuitive

https://leetcode.com/discuss/63016/accepted-and-simple-java-solution-with-detailed-explanation

https://leetcode.com/discuss/56164/simple-and-easy-understanding-o-n-2-java-solution

https://leetcode.com/discuss/52424/my-solutions-in-java-and-python

https://leetcode.com/discuss/52362/11-lines-o-n-2-python

259. 3Sum Smaller的更多相关文章

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

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

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

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

  4. [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 < ...

  5. Leetcode 259. 3Sum Smaller

    class Solution(object): def threeSumSmaller(self, nums, target): """ :type nums: List ...

  6. 259. 3Sum Smaller小于版3sum

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

  7. [LC] 259. 3Sum Smaller

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

  8. 【LeetCode】259. 3Sum Smaller 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...

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

随机推荐

  1. setEllipsize(TruncateAt where)

    void android.widget.TextView.setEllipsize(TruncateAt where) public void setEllipsize (TextUtils.Trun ...

  2. Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo

    1.新建Dll工程 2.Dll工程全部代码 library SubMain; { Important note about DLL memory management: ShareMem must b ...

  3. 单例模式C#

    首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...

  4. Python学习笔记1——人人都爱列表

    一些BIF函数在列表中的应用: Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64) ...

  5. 【转】常用插件和mvn test命令

    自定义构建Maven项目,需要包括额外的插件或者配置已存在的插件参数. 1. maven-compiler-plugin 指定JDK版本和编码方式 compiler插件能解决2个问题: 第一: mav ...

  6. R语言基础(一) 可视化基础

    ##数据获取 x1=round(runif(100,min=80,max=100)) x2=round(rnorm(100,mean=80, sd=7)) x3=round(rnorm(100,mea ...

  7. SVN四部曲之SVN命令精通

    SVN 常用命令一览表 命令 功能 使用格式 checkout 检出 svn  co  URL up 更新到当前URL的末端 svn  up switch 更新到某一tag/branch svn  s ...

  8. 【iis错误码】IIS 服务 这些年遇到的错误码

      II 发生错误,客户端似乎有问题.例如,客户端请求不存在的页面,客户端未提供有效的身份验证信息. 400 - 错误的请求. 401 - 访问被拒绝.   -- 暴力添加everyone用户,  i ...

  9. Thinkphp模板中使用自定义函数的方法

    注意:自定义函数要放在项目应用目录/common/common.php中. 这里是关键. 模板变量的函数调用格式:{$varname|function1|function2=arg1,arg2,### ...

  10. Eclipse常用功能

    功能(Functions):内置运行java程序的插件(JDT:java develop tools)工作集(WorkSet)(Task list)计划任务管理器(Mylyn)系统配置(Prefere ...