259. 3Sum Smaller
题目:
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的更多相关文章
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- 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 < ...
- 【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 ...
- [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 < ...
- Leetcode 259. 3Sum Smaller
class Solution(object): def threeSumSmaller(self, nums, target): """ :type nums: List ...
- 259. 3Sum Smaller小于版3sum
[抄题]: Given an array of n integers nums and a target, find the number of index triplets i, j, k with ...
- [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 < ...
- 【LeetCode】259. 3Sum Smaller 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...
- 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 ...
随机推荐
- ref 和out的用法以及区别
在项目其实很少用ref和out,但是我们常用的工具resharep在帮我们重构的时候难免会给我们重构成带有ref或者是out的方法. 本人也是用的少所以难免忘记,留下简略笔记,以供后来自我参考: 为何 ...
- 说说iOS中的手势及触摸
一.响应链 在IOS开发中会遇到各种操作事件,通过程序可以对这些事件做出响应. 首先,当发生事件响应时,必须知道由谁来响应事件.在IOS中,由响应者链来对事件进行响应,所有事件响应的类都是UIResp ...
- 多路选择器(multiplexer)简介
1.多路器简介 简称:多路器 功能:多输入 单输出 组合逻辑电路 2.verilog代码实现: module Mux_8(addr,in1,in2,in3,in4,in5,in6,in7,in8 ...
- LintCode-Unique Path II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- Ubuntu多系统安装注意事项
1. 安装 选择分区时一定要全设置成逻辑分区,不能是主分区! 2.多系统引导向修复 利用LiveCD制作U盘启动进入Ubuntu系统,若挂载点为: /dev/sda9 swap ...
- Java 8 VM GC Tunning Guild Charter 9-b
第九章 G1 GC The Garbage-First (G1) garbage collector is a server-style garbage collector, targeted for ...
- HTML弹出窗口
1.最简单的 <script type="text/javascript"> <!-- window.open("http://cn.bing.com& ...
- c++ switch case
http://www.cnblogs.com/RealOnlyme/articles/2579628.html
- H5下拉刷新特效demo,动画流畅
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Maven搭建webService (一) 创建服务端---使用main函数发布服务
今天和大家分享下 使用maven 搭建 webService 服务端: 首先需要在你的IDE中集成Maven.集成办法此处略....... 1.创建一个web工程. 2.在pom文件中增加以下依赖: ...