题目:

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]
[-2, 0, 3]

Follow up:

Could you solve it in O(n^2) runtime?

题解:

  (我没舍得花钱。。。咳咳,这样不太好QAQ,代码都没有提交过,所以以后来看得注意一下代码的正确性。)

  老规矩,第一个想法是啥?必须的暴力解啊,哈哈,虽然时间复杂度是O(n^3);这里

Solution 1 ()

 class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int cnt = ;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
if(i> && nums[i] == nums[i-]) continue;
for(int j=i+; j<n-; j++) {
if(j>i+ && nums[j] == nums[j-]) continue;
for(int k=j+; k<n; k++) {
if(k>j+ && nums[k] == nums[k-]) continue;
if(nums[i] + nums[j] + nums[k] < target) cnt++;
            else break;
}
}
}
}
};

  若想时间复杂度为O(n^2),那么就需要和之前的3Sum题目一样,两指针为剩余数组的头尾指针,搜索遍历。这里需要注意的是,当sum<target时,cnt需要加上k-j,而不是cnt++,因为数组已经排好序,若尾指针当处于位置k时满足条件,则j<position<=k的pos都满足这个条件,故cnt需加上k-j.

Solution 2 ()

 class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int cnt = ;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
if(i> && nums[i] == nums[i-]) continue;
int j = i + , k = n - ;
while(j<k) {
int sum = nums[i] + nums[j] + nums[k];
if(sum < target) {
cnt += k - j;
j++;
}
else k--;
}
}
return cnt;
}
};

【LeetCode】259 3Sum Smaller的更多相关文章

  1. 【LeetCode】259. 3Sum Smaller 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...

  2. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  3. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  4. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

  5. 【LeetCode】16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  6. 【LeetCode】15. 3Sum 三个数和为0

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  7. 【leetcode】15. 3Sum

    题目描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  8. 【LeetCode】015 3Sum

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  9. 【LeetCode】016 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

随机推荐

  1. 09-redis事务及锁应用

    Redis 中的事务 Redis支持简单的事务 Redis与 mysql事务的对比 ------------------------------------------------------- My ...

  2. 不错的iOS相关的主页或站点 (更新于14-06-22)

    近期一直没事在翻一些站点看看资料学习下. 推荐几个不错的站点: http://www.raywenderlich.com/   这个站点有各种各样的教程,可惜是大部分都是英文教程,只是阅读起来还好.每 ...

  3. l两张图片轮播

    在head里面加 <script language="javascript"> function scroll(spanlevel) { if (spanlevel.s ...

  4. 两个DataGridEHToExcel

    procedure TForm1.N1Click(Sender: TObject); var    GridtoExcel: TDBGridEhToExcel; begin    try    Gri ...

  5. CentOS6.5安装MySQL5.6 过程记录

    刚开始,还不太懂,直接上了MySQL5.7版本的二进制安装,结果遇到了各种问题,从5.6到5.7还是做了很大改变的,比如mysql_install_db的文件位置变更到了/bin文件下等等,觉得现在用 ...

  6. 关于EF输出sql的执行日志

    sqlserver中可以使用sql profiler:但是mysql当中无法查看:只能借助于组件: ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用 ...

  7. AWS:4.VPC

    主要介绍 1.Amazon混合云 2.将EC2加入VPC 3.VPC经典场景 4.VPC安全保障 Amazon混合云 : 在公有云的基础上创建私有云 VPC概念 VPC(VPC Virtual Pri ...

  8. going

  9. 列举你了解的Python较其他语言的优势

    1.简单易学 2.开发速度快 3.拥有最成熟的程序包资源库(第三方库)

  10. 流畅的python学习笔记:第十一章:抽象基类

    __getitem__实现可迭代对象.要将一个对象变成一个可迭代的对象,通常都要实现__iter__.但是如果没有__iter__的话,实现了__getitem__也可以实现迭代.我们还是用第一章扑克 ...