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 ...
随机推荐
- 接口测试入门(3)--使用httpClient进行登录用例操作/set-cookies验证/ List<NameValuePair>设置post参数/json解析
(最近学的都是很基础的接口测试,都是基于UI界面可见的接口,就是发请求,接收响应,分析返回的结果,校验,对共通模块进行封装,仅此而已,其实做自动化的思路基本都是如此,UI也是.) 现在开始用httpC ...
- CacheConcurrencyStrategy五种缓存方式
CacheConcurrencyStrategy有五种缓存方式: CacheConcurrencyStrategy.NONE,不适用,默认 CacheConcurrencyStrategy.REA ...
- python3中的编码与解码用法
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = '人生入戏' #python3在编码时会把str编码成utf-8的bytes类型 ...
- PHP命令注入笔记
一.PHP命令注入介绍 在学习php相关的攻击时,遇到了Command Injection,即命令注入攻击,是指这样一种攻击手段,黑客通过把HTML代码输入一个输入机制(例如缺乏有效验证限制的表格域) ...
- hadoop2.x的变化
HDFS Federation(HDFS联邦) HDFS有两个主要层: Namespace 由目录.文件和块组成:支持所有命名空间对文件和目录的操作. Block Storage Service 由B ...
- Linux Ubuntu从零开始部署web环境及项目 -----tomcat+jdk+mysql (二)
上一篇介绍如何在linux系统下搭建ssh环境 这篇开始将如何搭建web服务器 1,下载文件 在官网下载好 tomcat.jdk.mysql的linux压缩包 后缀名为.tar.gz 并通过xftp上 ...
- SpringAop详解
近几天学习了一下SpringAop在网上找了一些资料,此链接为原文链接http://www.cnblogs.com/xrq730/p/4919025.html AOP AOP(Aspect Orien ...
- 外设位宽为8、16、32时,CPU与外设之间地址线的连接方法
有不少人问到:flash连接CPU时,根据不同的数据宽度,比如16位的NOR FLASH (A0-A19),处理器的地址线要(A1-A20)左移偏1位.为什么要偏1位? (全文有点晦涩,建议收藏本文对 ...
- 第4章 同步控制 Synchronization ----信号量(Semaphore)
许多文件中都会提到 semaphores(信号量),因为在电脑科学中它是最具历史的同步机制.它可以让你陷入理论的泥淖之中,教授们则喜欢问你一些有关于信号量的疑难杂 症.你可能不容易找到一些关于 sem ...
- struts jar包
这些错误很让我摸不着头脑,经多方查阅资料后,在Struts 2.2.x中应该导入如下7个JAR文件 1) commons-fileupload-1.2.1.jar 2) commons-io- ...