[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 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.
Example:
Input: nums =[-2,0,1,3], and target = 2
Output: 2
Explanation: 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?
class Solution {
public int threeSumSmaller(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
Arrays.sort(nums);
int res = 0;
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1, end = nums.length - 1;
while (start < end) {
if (nums[i] + nums[start] + nums[end] < target) {
// assume the end - 1... met
res += end - start;
start += 1;
} else {
end -= 1;
}
}
}
return res;
}
}
[LC] 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 ...
- 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 < ...
- 259. 3Sum Smaller小于版3sum
[抄题]: Given an array of n integers nums and a target, find the number of index triplets i, j, k with ...
- 【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 ...
- 【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 ...
随机推荐
- git子模块使用
如下项目有多个标红的子模块 1.首先进入每个子模块目录,init初始化子模块仓库,然后提交远程. 2.在每个子目录都初始化好仓库后,进入lv-qggz主目录,只初始化该仓库,然后依次添加子模块的仓库地 ...
- Go语言-并发模式-资源池实例(pool)
Go语言并发模式 利用goroutine和channel进行go的并发模式,实现一个资源池实例(<Go语言实战>书中实例稍作修改) 资源池可以存储一定数量的资源,用户程序从资源池获取资源进 ...
- B. Odd Sum Segments CF(分割数组)
题目地址 http://codeforces.com/contest/1196/problem/B B. Odd Sum Segments time limit per test 3 seconds ...
- Dp(NOIp级)全解
2018年(你还真以为我会讲保卫王国2333 LuoguP5020 货币系统 这道题就相当于求{A}的线性基大小 证明: 反证法,设该解为B,那么B定能表示出{A}的线性基,即{A}的线性基中所有数都 ...
- awk grep sed 的一些问题
条件 匹配 打印含关键字的行 ps aux | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' z ...
- mysql的show status和show global status区别在哪
show status 本次会话的参数状态show global status 本次MYSQL服务开启(或重置)到现在总请求数
- Python说文解字_main
1. main函数: 我们知道很多的编程语言都要写一个main函数,比如在C# 中Main函数还需要大写.很多人疑惑为什么要写这么一个Main函数.其实这就是好比我们在建了一排房子,你从哪个门都可以进 ...
- c2000 N2A1 设置 KonNaD Settings & User Manual
KonNaD Settings & User Manual c2000 N2A1 两个开关都推到左边,都设置成off
- WINSCP 使用笔记
前期准备: 1.官网下载:http://winscp.net/eng/docs/lang:chs 官网C#示例:http://winscp.net/eng/docs/library#csharp 当然 ...
- 复杂分布式架构下的计算治理之路:计算中间件 Linkis
前言 在当前的复杂分布式架构环境下,服务治理已经大行其道.但目光往下一层,从上层 APP.Service,到底层计算引擎这一层面,却还是各个引擎各自为政,Client-Server 模式紧耦合满天飞的 ...