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 ...
随机推荐
- 策略模式(Strategy )
为实现一个目的采用不同的方式都可实现,具体看要采取哪种方式. //接口 public interface Strategy { public void algorithmInterface(); ...
- 1.3 Linux分区类型
1.主分区最多只能有4个. 2.扩展分区: 最多只能有一个: 主分区加扩展分区最多只能有4个: 扩展分区只能包含逻辑分区,不能写数据. 3.格式化目的: 写入文件系统:清除数据:划出文件分配表(i n ...
- Jquery 样式选择器,查找包含两种样式以上的元素
可以连写 $(".样式一.样式二") 中间不要留空格,id也一样. $(".modalDishMsgTitleWrap").find(".standa ...
- Redis 总结精讲 看一篇成高手系统-4
本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...
- Linux报错:bash: pip: command not found
$ wget https://bootstrap.pypa.io/get-pip.py$ python get-pip.py$ pip -V #查看pip版本 接下来就可以随便pip安装东西了
- POJ3259 Wormholes
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- 学习 MeteoInfo二次开发教程(六)
在教程(五)的基础上加了Faded,Grid_Fill,Grid_Point,Raster,Vector,Barb,Streamline 1.同样注意修改LegendStyleEnum改为Legend ...
- doris 0.9.0版本docker镜像制作与使用
1. 安装docker 详情请参见本人博客 2. 编译doris 详情请参见doris官网文档 3. 在编译好的doris output文件夹下编写两个Dockerfile 3.1 Dockerfi ...
- Windows和Linux查看端口占用
Windows方法 TCP netstat -aon|findstr "TCP"|findstr "LISTENING"|findstr ":135[ ...
- [sharepoint]修改Item或者File的Author和Editor
写在前面 最近项目中调用sharepoint rest api方式获取文件或者Item列表,而用的方式是通过证书请求,在上传文件,或者新建item的时候,默认的用户是在sharepoint端注册的用户 ...