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 ...
随机推荐
- JDBC第二篇--【PreparedStatment、批处理、处理二进制、自动主键、调用存储过程、函数】
这是我JDBC的第一篇 http://blog.csdn.net/hon_3y/article/details/53535798 1.PreparedStatement对象 PreparedState ...
- websphere部署--web应用-以自己的项目为例
启动websphere: 1) 启动Manager: /home/wasadmin/IBM/WebSphere/AppServer/bin/startManager.sh 2) 启动Node: ...
- jstl-初步认知
JSTL是java提供的JSP标签库 1,在项目中加入 jsf-api.jar jsf-impl.jar jstl-1.2.jar 三个包 2, 如何在jsp页面引入标签库 使用 <@tagli ...
- Linux SSH 安装Tomcat
tomcat的安装 1. 下载tomcat 从tomcat官网(http://tomcat.apache.org/download-70.cgi)下载tomcat的压缩包apache-tomcat-7 ...
- [转]iOS 应用程序的生命周期
OS的应用程序的生命周期,还有程序是运行在前台还是后台,应用程序各个状态的变换,这些对于开发者来说都是很重要的. iOS系统的资源是有限的,应用程序在前台和在后台的状态是不一样的.在后台时,程序会受到 ...
- OpenStack Ocata 超详细搭建文档
前言 搭建前必须看我本文档搭建的是分布式O版openstack(controller+ N compute + 1 cinder)的文档.openstack版本为Ocata.搭建的时候,请严格按照文档 ...
- JS设计模式(一) 单例模式
命名空间 单例模式是JavaScript中最常见的一种模式,通过这种模式可以为我们提供一个命名空间,例如jQuery库的命名空间为jQuery或$.命名空间的使用是为了让代码更加整洁,在多人协作开发的 ...
- 调试 ASP.NET Core 2.0 源代码
在Visual Studio 2017中可以通过符号以及源链接,非常方便对 ASP.NET Core 2.0中源代码进行调试.在这篇文章中,我们将重点介绍如何使用源链接对ASP.NET Core源进行 ...
- 面向对象编程笔记--static
通过static方法,提供静态的不需要实例化即可访问的方法或属性.所有的调用者可以使用同一个类(不实例化)或对象(只实例化一次),可以应用的场景: 1)各个调用者共享数据,协同工作. 2)对象只可以实 ...
- Oracle的常用命令之备份和恢复数据库
1 将数据库TES完全导出,用户名system 密码manager 导出到D:\daochu.dmp中 exp system/manager@TEST file=d:\daochu.dmp 2 将数据 ...