题目:

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. MAC 升级到10.10(OS X Yosemite)下apache+php的配置问题

    MAC升级到最新系统后 本地测试的站点不能正常运行,其原因是mac系统升级后 他的apache的版本也随之升级了版本 Server version: Apache/2.4.9 (Unix) Serve ...

  2. 一个Java对象到底占用多大内存

    在网上搜到了一篇博客讲的非常好,里面提供的这个类也非常实用: import java.lang.instrument.Instrumentation; import java.lang.reflect ...

  3. android support Percent支持库开发

    Android的布局支持百分比的设置进行开发,来学习如何去实现它,不过看起来会像网页的设置,比如宽度的设置属性是`layout_widthPercent`.在此之前,我们一般都会设置Linearlay ...

  4. java集合类(五)About Map

    接上篇“java集合类(四)About Set” 这次学完Map之后,就剩队列的知识,之后有关java集合类的学习就将告一段落,之后可能会有java连接数据库,I/O,多线程,网络编程或Android ...

  5. Wireshark技巧-过滤规则和显示规则

    Wireshark是一个强大的网络协议分析软件,最重要的它是免费软件. 过滤规则 只抓取符合条件的包,在Wireshark通过winpacp抓包时可以过滤掉不符合条件的包,提高我们的分析效率. 如果要 ...

  6. 【BZOJ】【1965】SHUFFLE 洗牌

    扩展欧几里德+快速幂 每次转换位置:第x位的转移到2*x %(n+1)这个位置上 那么m次后就到了(2^m)*x %(n+1)这个位置上 那么找洗牌m次后在 l 位置上的牌就相当于解线性模方程: (2 ...

  7. NET Framework 4 中的新 C# 功能

    http://msdn.microsoft.com/zh-cn/magazine/ff796223.aspx C# 编程语言自 2002 年初次发布以来已经有了极大的改善,可以帮助程序员编写更清晰易懂 ...

  8. hadoop浅尝 第一个hadoop程序

    hadoop编程程序员需要完成三个类. map类,reduce类和主类. map和reduce类自然是分别完成map和reduce.而主类则负责对这两个类设置job.完成这三个类之后,我们生成一个ja ...

  9. 【设计模式六大原则4】接口隔离原则(Interface Segregation Principle)

      定义:客户端不应该依赖它不需要的接口:一个类对另一个类的依赖应该建立在最小的接口上. 问题由来:类A通过接口I依赖类B,类C通过接口I依赖类D,如果接口I对于类A和类B来说不是最小接口,则类B和类 ...

  10. hdoj 2112 HDU Today

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=2112 分析:多了一个地方的条件,用map来映射地点编号,Dijkstra求解即可 //2013-10- ...