题目:

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. jQuery实现分页

    转载地址 http://www.cnblogs.com/xiaoruoen/archive/2012/01/11/2318199.html ;( function($){ $.extend({ &qu ...

  2. online learning

    转自http://blog.csdn.net/sjkldjflakj/article/details/51886277 不同于以往的批量学习,即给了许多的已标记好的资料来学习出一个假设函数,onlin ...

  3. python学习第六天

    一. 模块介绍1. 模块的定义:用一堆代码实现了某个功能的代码集合     包的定义:本质就是一个目录(必须导游一个_init_.py文件),是用来从逻辑上组织模块的.2. 需要多个函数才能完成(函数 ...

  4. 命令行参数的处理函数getopt

    命令参数 在linux下, shell命令的参数分两种情况: a.参数需要附加信息, 如"wget http://www.abc.com/1.zip -o 1.zip" b.参数不 ...

  5. c 指针兼容性问题

    指针兼容性问题: const指针不能赋值给非const指针. 非const指针可以赋值给const 指针,但前提是只是一层间接运算 Example: int *pt1; const *pt2; con ...

  6. SlimDx绘制点图元的问题

    问题:点图元在自己创建的三维环境里渲染不出来,代码如下: GMCustomVertex.PositionNormalColored wellPart = new GMCustomVertex.Posi ...

  7. iOS - 指定视图的圆角个数-b

    平常设置视图的圆角最普遍的就是设置四个角的,方法也就是一句代码解决: view.layer.cornerRadius = 10; 四个圆角 但有时需求会是指定某个,或者特定哪几个角设置圆角,所以我们需 ...

  8. Android开发在使用第三方推送的时候出现INSTALL_FAILED_VERSION_DOWNGRADE

    [-- :: - push_getui_test] Uploading push_getui_test.apk onto device 'emulator-5554' [-- :: - push_ge ...

  9. Google history

    传说,硅谷的公司在和微软的竞争中一直处于下风,不论在市场,人才,还是在打官司上,直到婴儿巨人Baby Giant谷歌的出现,历史才出现前所未有的改变.Google以一个强大的挑战者的身份出现在人们的视 ...

  10. UVA 10720 Graph Construction 贪心+优先队列

    题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...