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 ...
随机推荐
- IIS 集成模式 导致 AjaxPro 无法正常运行
web.config 配置如下: system.web/httphandlers <httpHandlers> <add verb="POST,GET" path ...
- PythonStudy——Python字典底层实现原理 The underlying implementation principle of Python dictionary
在Python中,字典是通过散列表或说哈希表实现的.字典也被称为关联数组,还称为哈希数组等.也就是说,字典也是一个数组,但数组的索引是键经过哈希函数处理后得到的散列值.哈希函数的目的是使键均匀地分布在 ...
- C++ Primer Plus (Stephen Prata 著)
第1章 预备知识 (已看) 第2章 开始学习C++ (已看) 第3章 处理数据 (已看) 第4章 复合类型 (已看) 第5章 循环和关系表达式 (已看) 第6章 分支语句和逻辑运算符 (已看) 第7章 ...
- Mysql环境搭建(及中文乱码解决)
卸载MySQL 电脑已经安装过mysql的 卸载电脑上的mysql方法: 我的电脑-->右键-->属性-->高级系统设置-->环境变量-->系统变量里面-->找到环 ...
- Spring Cloud(Dalston.SR5)--Zuul 网关-过滤器
Spring Cloud 为 HTTP 请求的各个阶段提供了多个过滤器,这些过滤器的执行顺序由各自提供的一个 int 值决定,提供的值越小则优先级越高,默认的过滤器及优先级如下: 自定义过滤器 在默认 ...
- 【Python】hasattr() getattr() setattr() 使用方法详解
本文转自 https://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回B ...
- Bootice1.34版本把grub4dos0.46a写入硬盘MBR失败一个例子
Bootice1.34版本把grub4dos0.46a写入硬盘MBR失败一个例子 一个同事的台式机,BIOS启动,500GB硬盘,分了四个MBR分区,C盘是激活的主分区,第二个分区50 ...
- 算法实践--最长递增子序列(Longest Increasing Subsquence)
什么是最长递增子序列(Longest Increasing Subsquence) 对于一个序列{3, 2, 6, 4, 5, 1},它包含很多递增子序列{3, 6}, {2,6}, {2, 4, 5 ...
- flutter学习地址
Flutter - 不一样的跨平台解决方案: 关于Flutter,你想知道的都在这里了!: Flutter 时间表 2015 年 4 月,Flutter(最初代号 Sky)在 Dart Devel ...
- NIOS II With uCOSII
1.如果使用uCOS,那么Qsys中Nios II核就不能使用外部中断控制器(EIC). 2.遇到很迷惑的问题,运行uCOSII的实例代码,总是在第二个OSTimeDlyHMSM(0, 0, 3, 0 ...