[抄题]:

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指针对撞必须要排序,初始化时就写,别忘了!

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. right很大时,right-left都小于了,right变小时就更可以了。所以count+=right-left一段
  2. 必须要在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的更多相关文章

  1. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

  2. 259. 3Sum Smaller

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  3. 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 ...

  4. [Locked] 3Sum Smaller

    3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...

  5. 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 < ...

  6. [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 < ...

  7. 【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 ...

  8. Leetcode 259. 3Sum Smaller

    class Solution(object): def threeSumSmaller(self, nums, target): """ :type nums: List ...

  9. 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 ...

随机推荐

  1. PythonStudy——函数的返回值 The return value of the function

    # 在函数体中,通过return关键词返回函数的内部数据给外部 """# 一.作用# return作用:1.结束函数:2.将函数的内部数据返回给外部 def fn(): ...

  2. Vue-项目之免费课和购物车实现

    调整首页细节 固定头部 App.vue中代码 <style> body{ padding: 0; margin:0; margin-top: 80px; } </style> ...

  3. Python神坑:sum和numpy.sum

    同样的一段代码,在两个python文件里面执行的结果不一样,一个是按照列单位进行sum一个是所有元素进行sum: def distCal(vecA, vecB): return sqrt(sum(po ...

  4. [ZZ] 基于Matlab的标记分水岭分割算法

    基于Matlab的标记分水岭分割算法 http://blog.sina.com.cn/s/blog_725866260100rz7x.html 1 综述 Separating touching obj ...

  5. c# 动态绘制直线和曲线

    c# 动态绘制直线和曲线   在本案例中利用Graphics对象动态地绘制直线和曲线.程序运行后,选择“直线”单选按钮,然后按下鼠标左键拖动鼠标就可以绘制直线,选择“曲线”单选按钮,然后移动鼠标就可以 ...

  6. 初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器

    第一次接触nginx的时候,那时候公司还是用的一些不知名的小技术,后来公司发展问题,重新招了人,然后接触到nginx,公司 使用nginx用来做代理服务器,所有请求 都先经过nginx服务器,然后交由 ...

  7. 在WINDOWS下安装MYSQL8.0

    1:创建文件夹D:\data\service\mysql-8.0.11-winx64\data 2:进到D:\data\service\mysql-8.0.11-winx64\bin 第三步:初始化. ...

  8. HTML/CSS基础知识(二)

    Q:HTML5是什么?为什么要用HTML5?有什么新特性?移除了哪些元素? HTML5是HTML的最新版本,2014年10月由万维网联盟(W3C)完成标准制定.目标是替换1999年制定的HTML4.0 ...

  9. Springboot Download file

    @RequestMapping(value = "/downloadSvt") public ResponseEntity<FileSystemResource> ex ...

  10. [UGUI]Image源码分析

    unity版本5.3.5 一.属性 1.overrideSprite 脚本对精灵的访问均使用overrideSprite,如果m_OverrideSprite存在就使用m_OverrideSprite ...