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 < ...
随机推荐
- 停课day5
一转眼,已经停课五天了. 高二大佬们已经都走了,在机房里面呆着,有时感觉很孤寂. 但是为了能学好竞赛,这些都是在所不惜的. 好像多打打比赛啊,可是cf要FQ,洛谷之类的比赛还不勤. 哎,先去学一发SP ...
- yaf的安装
http://kenby.iteye.com/blog/1979899 yaf源码分析学习网站 # wget https://github.com/laruence/php-yaf/archive/m ...
- C++ 中 string, char*, int 类型的相互转换
一.int 1.int 转换成 string 1) to_string函数 —— c++11标准增加了全局函数std::to_string: string to_string (int val); s ...
- Google MapReduce中文版
英文原文链接: Google Map Reduce 译文原文链接: Google MapReduce中文版 Google MapReduce中文版 译者: alex 摘要 MapReduce是一个编程 ...
- idea xml 绿背景色 去掉拼写检查
去掉背景色 去掉拼写检查
- oracle查看字符集和修改字符集
oracle查看字符集和修改字符集 : 查看数据库服务器的字符集: select userenv('language') from dual ; 登陆用dba: 停掉数据库 : shutdown im ...
- L2-002. 链表去重---模拟
https://www.patest.cn/contests/gplt/L2-002 L2-002. 链表去重 时间限制 300 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 ...
- 【mysql优化】语句优化
1.int型子查询陷阱 如下两个表: mysql> desc user; +----------+-------------+------+-----+---------+-------+ | ...
- Linux 邮件服务器 之跟我一步一步来实现一个邮件系统【转】
转自:http://tchuairen.blog.51cto.com/3848118/1686875/ 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...
- 使用MXNet远程编写卷积神经网络用于多标签分类
最近试试深度学习能做点什么事情.MXNet是一个与Tensorflow类似的开源深度学习框架,在GPU显存利用率上效率高,比起Tensorflow显著节约显存,并且天生支持分布式深度学习,单机多卡.多 ...