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 <= 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?
题目标签:Array
题目给了我们一个 nums array 和一个 target, 要我们找到 任何三个数字之和 小于 target。题目要求了要 O(n^2) 的runtime, 所以三个 for loop 就不行了,虽然也能通过。
对于这种3Sum,要在O(n^2) 条件下的,都是要一个遍历,然后剩下的n*n 用 two pointers 来代替。换句话说,就是第一个遍历来选定第一个数字,剩下的就是2Sum的题目了,而且要用 two pointers 来做。
来分析一下这题的情况:
首先排序,从小到大。
选好第一个数字,然后在剩下的数字里,选好left 和 right:
case 1:如果当left 数字 + right 数字 < target - 第一个数字 的话, 意味着符合条件,并且left 和 所有在right 左边的数字搭配 也都符合,因为排序从小到大,所以直接把所有的可能性都加上, right - left;然后left++ 到下一个数字继续。
case 2:如果当left 数字 + right 数字 >= target - 第一个数字 的话, 意味着目前数字组合太大,需要更小的数字,所以right--。
Java Solution:
Runtime beats 79.74%
完成日期:09/08/2017
关键词:Array
关键点:一个遍历(选中第一个数字):two pointers 代替 n*n
class Solution
{
public int threeSumSmaller(int[] nums, int target)
{
Arrays.sort(nums);
int res = 0; for(int i=0; i<nums.length-2; i++) // no need to iterate last two numbers
{
int left = i+1;
int right = nums.length-1;
int tempTarget = target - nums[i]; while(left < right)
{ // if find two numbers < tempTarget
if(nums[left] + nums[right] < tempTarget)
{
res += (right - left); // all the left side numbers are also < tempTarget
left++; // left one with each number from right rest are done, move to next left one
}
else // meaning current two numbers sum is too big, need smaller one
right--;
} } return res;
}
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 259. 3Sum Smaller (三数之和较小值) $的更多相关文章
- [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] 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 15. 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 ...
- [Swift]LeetCode259.三数之和较小值 $ 3Sum Smaller
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- LeetCode 第15题-三数之和
1. 题目 2.题目分析与思路 3.思路 1. 题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且 ...
- LeetCode#15 | Three Sum 三数之和
一.题目 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意:答案中不可以包含 ...
- LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
随机推荐
- eclipse: eclipse导入工程出现大红叹号
总结: 问题原因:工程中classpath中指向的包路径错误 解决办法:到BUILDPATH CONFIG````中,liberaies中 出现红色叉号的包为路径错误的包.到classpath中修改相 ...
- Eclipse rap 富客户端开发总结(1) :rap简单介绍和开发环境搭建
一.rap简单介绍 1 基本概念 RAP可以让开发人员使用JAVA API和按照Eclipse 插件的开发模式构建基于AJAX的Web 2.0应用程序, RAP的工作原理是采用交叉编译的方式将 ...
- input type="hidden" js获取不到值(document.getelementbyid OR $(#).val())
<head> <input type="hidden" name="aplStatus" id="aplStatus" v ...
- Android 之http编程
HTTP-GET和HTTP-POST定义: 都是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语义. 每个HTTP-GET和HTTP-POST都由一系列HTTP请求 ...
- Hadoop的safeMode
当集群启动的时候,会首先进入到安全模式.系统在安全模式下,会检查数据块的完整性.假设我们设置的副本数(即参数dfs.replication)是5,那么在dataNode上就应该有5个副本存在,假设只存 ...
- thinkphp的select和find的区别
hinkphp是比较好的php开发框架,能比较快速的开发MVC架构的管理系统,我们需要用到 select()和find()方法,两个方法都能返回数据集数组,但有什么不同呢?先看一下我的代码对比:$te ...
- 记XDCTF的misc之旅---base64隐写
bWFpbigpe2ludCBpLG5bXT17KCgoMSA8PDEpPDwgKDE8PDEpPDwoMTw8Cm==ICAgICAgIDEpPDwoMTw8KDE+PjEpKSkrKCgxPDwx ...
- JFrame的层次结构以及背景颜色设置问题
JFrame的层次结构: JFrame:窗体,也就是窗口的框架.默认为不可见.不透明的(可以使用isVisible和isOpaque来验证).创建窗口时,最后一步需要调用setVisible(true ...
- C#实现断点续传
断点续传的原理在了解HTTP断点续传的原理之前,先来说说HTTP协议,HTTP协议是一种基于tcp的简单协议,分为请求和回复两种.请求协议是由客户机(浏览器)向服务器(WEB SERVER)提交请求时 ...
- Lucene介绍与入门使用
Lucene简介 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整 ...