259. 3Sum Smaller小于版3sum
[抄题]:
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?
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
想到了化成2sum变形,但是不知道怎么改变第三个变量的值:for一遍就行了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
nsum指针对撞必须要排序,初始化时就写,别忘了!
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- right很大时,right-left都小于了,right变小时就更可以了。所以count+=right-left一段
- 必须要在left < right的while循环前提条件下进行
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
怎么改变第三个变量的值:for一遍就行了
[复杂度]:Time complexity: O(n2) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public int threeSumSmaller(int[] nums, int target) {
//corner case
if (nums == null || nums.length == 0) return 0;
//initialization: count = 0, sort
int count = 0;
Arrays.sort(nums);
//for loop and find i, left = i + 1, right = len - 1
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1; int right = nums.length - 1;
//while loop
while (left < right) {
//if and adjust left, right, add count
if (nums[i] + nums[left] + nums[right] < target) {
//add the whole period
count += right - left;
left++;
}
else {
right--;
}
}
}
//return
return count;
}
}
259. 3Sum Smaller小于版3sum的更多相关文章
- 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 ...
- 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 < ...
- [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 ...
- 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 ...
随机推荐
- PythonStudy——编程基础 Python Primary
1.什么是编程语言 语言: 一个事物与另外一个事物沟通的介质 .编程语言是程序员与计算机沟通的介质. 编程: 将人类内识别的语言转化为机器能识别的指令,这种过程就叫做编程. 注:最终这些指令会被转化 ...
- windows server 2008 R2之取消多余的安全配置
一:取消IE浏览器的安全配置(使IE浏览器可以正常上网) 管理员禁用即可 二.取消关机时强制输入关机备注 运行gpedit.msc,选择计算机配置->管理模板->系统->提示“关机时 ...
- MySQL Execution Plan--IN查询计划
对于IN查询,MySQL会根据当前表数据结构(索引)和数据分布(统计信息和预估)生成多种执行计划,并根据执行成本挑选出“最优执行计划”. 假设有查询 SELECT * FROM student ,,, ...
- 策略模式(Strategy )
为实现一个目的采用不同的方式都可实现,具体看要采取哪种方式. //接口 public interface Strategy { public void algorithmInterface(); ...
- PTA上Java问题自查与提问方法
自查 首先请一定先看这篇文章<PTA中提交Java程序的一些套路>.该文囊括了PTA中提交Java程序的几乎所有常见问题,请仔细阅读可以少踩很多坑 题目测试方法:复制样例输入,然后打开一个 ...
- mysql中的锁
MYSQL不同的存储引擎支持不同的锁的机制 MyISAM 支持表锁,InnoDB支持表锁和行锁 表锁,行锁比较 表锁:开销小,加锁快:不会出现死锁:锁定力度大,发生锁冲突概率高,并发度最低 行锁:开销 ...
- solr6.4.1 搜索引擎(1)启动eclipse启动
solr是一个java写的搜索引擎,所以支持java方式的eclipse调试. 本篇文章使用solr版本为6.4.1 一. 环境 solr 下载地址 http://archive.apache.org ...
- Python Pycharm 专题
http://www.themesmap.com/theme.html?t=time&page=3 一些好的主题地址 直接导入import settings就可以使用了
- ubuntu14.04 安装jdk1.8及以上
安装xmind1.8 碰到jvm版本太低的错误: Version 1.7.0_131 of the JVM is not suitable for this product. Version: 1.8 ...
- flutter学习地址
Flutter - 不一样的跨平台解决方案: 关于Flutter,你想知道的都在这里了!: Flutter 时间表 2015 年 4 月,Flutter(最初代号 Sky)在 Dart Devel ...