[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 ...
随机推荐
- http与socket
http是一个应用层的协议 socket不是一个协议,而是一个编程(API)接口
- VUE- iView组件框架的使用
VUE- iView组件框架的使用 1. 下载iView 工程. 引用:https://www.iviewui.com/
- 十五、Numpy-科学计算基础库
Numpy: NumPy(Numerical Python) 是科学计算基础库,提供大量科学计算相关功能,比如数据统计,随机数生成等.其提供最核心类型为多维数组类型(ndarray) ...
- RNA分类|技术策略|终极目标
如何在转录水平分类所有RNA分子?可以罗列所有的可能性.技术策略和终极目标. 可能性:见纸 技术策略:RNA单细胞直测技术 终极目标:单细胞水平RNA直测技术决定新的人类RNA组和人类表观组学两个核心 ...
- Linux-线程引入
1.使用进程技术的优势 (1).CPU分时复用,单核心CPU可以实现宏观上的并行 (2).实现多任务系统需求(多任务的系统是客观的) 2.进程技术的劣势 (1).进程间切换开销大 (2).进程间通信麻 ...
- python开源库——h5py快速指南
1. 核心概念 一个HDF5文件是一种存放两类对象的容器:dataset和group. Dataset是类似于数组的数据集,而group是类似文件夹一样的容器,存放dataset和其他group.在使 ...
- 一文彻底搞懂Cookie、Session、Token到底是什么
> 笔者文笔功力尚浅,如有不妥,请慷慨指出,必定感激不尽 Cookie 洛:大爷,楼上322住的是马冬梅家吧? 大爷:马都什么? 夏洛:马冬梅. 大爷:什么都没啊? 夏洛:马冬梅啊. 大爷:马什 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL简介
MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用 ...
- 05 SpringMVC:02.参数绑定及自定义类型转换&&04.SpringMVC返回值类型及响应数据类型&&05.文件上传&&06.异常处理及拦截器
springMVC共三天 第一天: 01.SpringMVC概述及入门案例 02.参数绑定及自定义类型转换 03.SpringMVC常用注解 第二天: 04.SpringMVC返回值类型及响应数据类型 ...
- 寒假day05-spring框架
1.回顾事务 l 事务:一组业务操作ABCD,要么全部成功,要么全部不成功. l 特性:ACID 原子性:整体 一致性:完成 隔离性:并发 持久性:结果 l 隔离问题: 脏读:一个事务读到另一个事务没 ...