class Solution(object):
def threeSumSmaller(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort()
n = len(nums)
ans = 0 for k in range(n):
i, j = k+1, n-1
while i<j:
sum3 = nums[k] + nums[i] + nums[j]
if sum3 < target:
ans += j - i
i += 1
else:
j -= 1
return ans

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]

Follow up:
Could you solve it in O(n2) runtime?

思路: 相遇型two pointers。注意L17, 这一句的意思是,如果 nums[k] + nums[i] + nums[j] < target,那么i和j之间的所有nums[p]+nums[k]+nums[i]也都满足条件。

 
 

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

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

  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 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

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

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

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

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

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

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

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

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

  9. [LeetCode] 923. 3Sum With Multiplicity 三数之和的多种情况

    Given an integer array A, and an integer target, return the number of tuples i, j, k  such that i &l ...

随机推荐

  1. 9.6 MongoDB一

    目录:ASP.NET MVC企业级实战目录 9.6.1 MongoDB简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSql数据库中比较热门的一种.它在许多场景下可用于替代传统 ...

  2. [连载]《C#通讯(串口和网络)框架的设计与实现》- 11.调试器的设计

    目       录 第十一章     调试器设计... 2 11.1         调试接口... 2 11.2         界面方式调试... 3 11.3         命令行方式调试.. ...

  3. 大朋展翅 html5上传图片(三)一解决部分手机拍相册批量上传图片转向问题

    在经过前面的改进之后本来以为已经没有问题了,但经过我们神通广大的测试的测试,发现相册中的图片在上传时也会发生转向问题.既然前面都解决了拍照转向的问题,那么相册中图片的上传也容易解决.修改一下需要旋转图 ...

  4. iOS:GCD组

    组内异步会与组外顺序执行的事件争抢资源 1).创建一个组 dispatch_group_t group = dispatch_group_create(); 2).组内异步ST1,DISPATCH_Q ...

  5. Ext JS - 问答

    Ext JS - 问答 在下面你将可以找到关于Ext JS 的最常见问题的答复.如果没有找到您所需的答复,请访问 Ext JS 论坛或者提交一个支持申请. 如果你确信你的问题可以对本页有补充,请让我们 ...

  6. 基于UDP的网络编程

    与TCP编程相比较,UDP缺少了connect().listen()及accept()函数,这是由于UDP协议无连接的特性,不用维护TCP的连接.断开等状态. UDP编程框图 API函数 socket ...

  7. User mode Linux

    一.简介 用户模式Linux(User ModeLinux,UML)不同于其他Linux虚拟化项目,UML尽量将它自己作为一个普通的程序.从Linux2.6.9版本起,用户模式Linux(User m ...

  8. 【Pyrosim案例】02:简单燃烧

    1 案例说明 本案例介绍一个简单的燃烧模拟. 本案例通过指定热释放率(Heat Release Rate,HRR)来定义一个500kW的燃烧火焰.利用热释放率来定义燃烧火焰在火灾安全工程中描述火焰的一 ...

  9. plain framework 1 网络流 缓存数据详解

    网络流是什么?为什么网络流中需要存在缓存数据?为什么PF中要采用缓存网络数据的机制?带着这几个疑问,让我们好好详细的了解一下在网络数据交互中我们容易忽视以及薄弱的一块.该部分为PF现有的网络流模型,但 ...

  10. 图像处理中任意核卷积(matlab中conv2函数)的快速实现。

    卷积其实是图像处理中最基本的操作,我们常见的一些算法比如:均值模糊.高斯模糊.锐化.Sobel.拉普拉斯.prewitt边缘检测等等一些和领域相关的算法,都可以通过卷积算法实现.只不过由于这些算法的卷 ...