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 ...
随机推荐
- js 功能
---IE wps excelApp =ActiveXObject("Excel.Application") App.DisplayAlerts = false 不显示警告 App ...
- poj2279——Mr. Young's Picture Permutations
Description Mr. Young wishes to take a picture of his class. The students will stand in rows with ea ...
- 创建一个dynamics 365 CRM online plugin (二) - fields检查
Golden Rules 1. Platform only passes Entity attributes to Plugin that has change of data. 2. If the ...
- Linux-入门配置jdk,tomcat,mysql
Mysql安装 大家可以在这里下 http://mirrors.163.com/mysql/Downloads/MySQL-5.7/ 1)查看CentOS自带的mysql rpm -qa | grep ...
- 第二章 JavaScript文档(上)
JavaScript 1.JavaScript简介 起源 在1995年时,由Netscape公司的Brendan Eich,在网景导航者浏览器上首次设计实现而成.Netscape在最初将其脚本语言命名 ...
- HTTP进阶学习笔记
代理 HTTP的代理服务器既是Web服务器,又是Web客户端.使用代理可以"接触"到所有流过的HTTP流量,代理可以对其进行监视和修改.常见的就是对儿童过滤一些"成人&q ...
- Laravel 5.5处理 Emoji 表情不顯示問題
服务器环境:PHP7 + MySQL5.6 + Laravel 5.5 項目有個玩樂日誌功能,添加玩樂日誌有富文本輸入,富文本輸入的內容在emoji表情之後被截斷了,沒保存到數據表,排查是對應字段字符 ...
- golang cache--go-cache
go-cache是一款类似于memached 的key/value 缓存软件.它比较适用于单机执行的应用程序. go-cache实质上就是拥有过期时间并且线程安全的map,可以被多个goroutine ...
- java工程师-面试知识点总结
目录(转载) [x] 一.Java基础(语言.集合框架.OOP.设计模式等) [x] 二.Java高级(JavaEE.框架.服务器.工具等) [x] 三.多线程和并发 [x] 四.Java虚拟机 [x ...
- python虚拟环境virtualenv简介
参考网站: https://realpython.com/python-virtual-environments-a-primer/ 一. 创建一个新的虚拟环境 # Python 2: $ virtu ...