3Sum Smaller -- LeetCode
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.
For example, given nums = [-2, 0, 1, 3], and target = 2.
Return 2. Because there are two triplets which sums are less than 2:
[-2, 0, 1]
[-, , ]
[-, , ]
思路:将数组排序。枚举第一个数,假设它为第i个数,则triplet中的第二个数和第三个数则在数组[i+1, n-1]中。我们用两个指针left和right来找这两个数,向中间搜索。
当nums[i] + nums[left] + nums[right] < target时,right指针向左移动仍然会符合,因此这时候满足条件的结果数有right - left个,记下这个数值,然后将left向右移动一位;否则将right向左移动一位。最后返回所有的结果数之和。时间复杂度为O(N^2)。
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int count = , len = nums.size();
sort(nums.begin(), nums.end(), less<int>());
for (int i = ; i < len - ; i++) {
int left = i + , right = len - ;
while (left < right) {
if (nums[i] + nums[left] + nums[right] < target) {
count += right - left;
left++;
} else {
right--;
}
}
}
return count;
}
};
3Sum Smaller -- LeetCode的更多相关文章
- 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, ...
- 3Sum Closest - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是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] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- [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 < ...
随机推荐
- 安徽师大附中%你赛day3T1 怜香惜玉 解题报告
怜香惜玉 题意: 已知 \(f(x)=\frac{2 \times \sum_{(i,x)=1}^x i}{φ(x)}\) 先给定数据组数\(t\)和\(k\) 每组数据给出\(n\),求\(\sum ...
- 12.25模拟赛T2
https://www.luogu.org/blog/a23333/post-xing-xuan-mu-ni-sai-path-ji-wang-zui-duan-lu 如果设f[i]表示从i到n的期望 ...
- BZOJ2097: [Usaco2010 Dec]Exercise 奶牛健美操 贪心+伪树dp+二分
//论全局变量的杀伤力....QAQ#include<cstdio> #include<iostream> #include<cstdlib> #include&l ...
- hadoop SecondNamenode 详解
SecondNamenode名字看起来很象是对第二个Namenode,要么与Namenode一样同时对外提供服务,要么相当于Namenode的HA. 真正的了解了SecondNamenode以后,才发 ...
- css属性选择器应用
代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...
- php函数-shuffle
Shuffle()函数说明: -随机乱序现有数组并不保留键值: -shuffle()函数把数组中的元素按随机顺序重新排列,该函数为数组中的元素分配新的键名,已有键名将被删除. 语法说明: shuffl ...
- node读取文件夹名
const fs = require('fs'); const join = require('path').join; /** * * @param startPath 起始目录文件夹路径 * @r ...
- mysql 基本操作练习
), sex ), age )); , '北京'); , '上海'); , '广州'); , '北京'); , '北京'); , '上海'); , '北京'); #(). 写出sql语句,查询所有年龄 ...
- Python学习笔记 - day7 - 类
类 面向对象最重要的概念就是类(Class)和实例(Instance),比如球类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可能不同.在Python中,定义类 ...
- android ARM 汇编学习—— 在 android 设备上编译c/cpp代码并用objdump/readelf等工具分析
学习 android 逆向分析过程中,需要学习 Arm 指令,不可避免要编写一些 test code 并分析其指令,这是这篇文档的背景. 在目前 android 提供的开发环境里,如果要编写 c / ...