LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/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.
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]
题解:
与3Sum类似. 计算和小于target的组合个数,重复的也要算. 若是nums[i] + nums[j] + nums[k] 符合要求,那么count += k-j, j++. 举例{-2, 0, 1, 3}. target = 4. {-2, 0, 3}符合要求, {-2, 0, 1}也符合要求,一次加上两个.
若是不符合要求说明等于或者大于target了, k--.
Time Complexity: O(n^2). Space: O(1).
AC Java:
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 j = i+1;
int k = nums.length-1;
while(j<k){
if(nums[i] + nums[j] + nums[k] >= target){
k--;
}else{
count += k-j;
j++;
}
}
}
return count;
}
}
类似Triangle Count, Valid Triangle Number.
LeetCode 3Sum Smaller的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- 259 [LeetCode] 3Sum Smaller 三数之和较小值
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 259. 3Sum Smaller
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
- 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 ...
- [Locked] 3Sum Smaller
3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...
- 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 < ...
随机推荐
- BZOJ4383 : [POI2015]Pustynia
设$a$到$b$的边权为$c$的有向边的含义为$b\geq a+c$,则可以根据题意构造出一张有向图. 设$f[x]$为$x$点可行的最小值,$a[x]$为$x$位置已知的值,则$f[x]=\max( ...
- 比较好用的php函数
eval(); $b = 2;$c = "+";$d = 3;eval("\$a=$b$c$d;"); //字符串相加,取值 (加减乘除都行) str_repl ...
- awk 学习
1. awk用例 今天用awk来统计一个字符出现的次数,总是比实际多一个.查了半天才发现问题所在. 文本tt.txt如下: <lst name="responseHeader" ...
- NOIP欢乐模拟赛 T2 解题报告
小澳的坐标系 (coordinate.cpp/c/pas) [题目描述] 小澳者表也,数学者景也,表动则景随矣. 小澳不喜欢数学,可数学却待小澳如初恋,小澳睡觉的时候也不放过. 小澳的梦境中出现了一个 ...
- Selenium_获取相对坐标
<html> <head> <title>位置</title> <style> .test { background: url(" ...
- Linux-CentOS 6.5 mini 中没有curses.h的问题
1.直接贴过程 [fengbo@CentOS: jigsaw]$ rpm -q ncursesncurses-5.7-3.20090208.el6.i686[fengbo@CentOS: jigsaw ...
- Sumsets
题目连接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=91209#problem/A 题意:给定一个整数几何S,找出一个最大的d,使得a ...
- 高性能MySQL第1章知识点梳理
1. MySQL的逻辑架构 最上面不是MySQL特有的,所有基于网络的C/S的网络应用程序都应该包括连接处理.认证.安全管理等. 中间层是MySQL的核心,包括查询解析.分析.优化和缓存等.同时它还提 ...
- Linux下配置Lamp
linux下配置lamp步骤: 一.快速安装Apache+PHP5+MySql 先更新: # yum update 然后安装LAMP环境:(163的yum源上只有php5.1.6 mysql 5.0. ...
- 【HDU1914 The Stable Marriage Problem】稳定婚姻问题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1914 题目大意:问题大概是这样:有一个社团里有n个女生和n个男生,每位女生按照她的偏爱程度将男生排序, ...