题目:

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-利用session完成用户登陆

    /***********************************************login.html*****************************************/ ...

  2. php 字符串内容是数组格式 转换成数组

    一个简单的应用.. 例, $str    =    "array( 'USD'=>'1', 'GBP'=>'0.6494', 'EUR'=>'0.7668' ,'JPY'= ...

  3. Zend API:深入 PHP 内核

    Introduction Those who know don't talk. Those who talk don't know. Sometimes, PHP "as is" ...

  4. 17 redis -key设计原则

    书签系统 create table book ( bookid int, title char(20) )engine myisam charset utf8; insert into book va ...

  5. Python 进程、线程、协程、锁机制,你知多少?

    1.python的多线程到底有没有用? 2. 为什么在python里推荐使用多进程而不是多线程 3.进程.线程.协程.各种锁 4.Python多进程编程

  6. iOS 流布局 UICollectionView使用(简单使用)

    简介 UICollectionView是iOS6之后引入的一个新的UI控件,它和UITableView有着诸多的相似之处,其中许多代理方法都十分类似.简单来说,UICollectionView是比UI ...

  7. Python爬虫-- BeautifulSoup库

    BeautifulSoup库 beautifulsoup就是一个非常强大的工具,爬虫利器.一个灵活又方便的网页解析库,处理高效,支持多种解析器.利用它就不用编写正则表达式也能方便的实现网页信息的抓取 ...

  8. synchronized同步关键字

    参考:http://blog.csdn.net/luoweifu/article/details/46613015 synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: ...

  9. 【题解】HNOI2013比赛

    [题解][P3230 HNOI2013]比赛 将得分的序列化成样例给的那种表格,发现一行和一列是同时确定的.这个表格之前是正方形的,后来长宽都减去一,还是正方形.问题形式是递归的.这就启示我们可以把这 ...

  10. 题解 P1387 【最大正方形】

    传送门 搞不清楚为什么这一题要DP . . . . . . 思路: \(n\le100\),考虑暴力. 要求一大块区间内都是1,考虑前缀和. 在矩阵中求一个符合条件的子矩阵,考虑\(n^3\)的&qu ...