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?

 
 
这道题是 3Sum 问题的一个变形,让我们求三数之和小于一个目标值,那么最简单的方法就是穷举法,将所有的可能的三个数字的组合都遍历一遍,比较三数之和跟目标值之间的大小,小于的话则结果自增1,参见代码如下:
 
解法一:
// O(n^3)
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int res = ;
sort(nums.begin(), nums.end());
for (int i = ; i < int(nums.size() - ); ++i) {
int left = i + , right = nums.size() - , sum = target - nums[i];
for (int j = left; j <= right; ++j) {
for (int k = j + ; k <= right; ++k) {
if (nums[j] + nums[k] < sum) ++res;
}
}
}
return res;
}
};

题目中的 Follow up 让我们在 O(n^2) 的时间复杂度内实现,那么借鉴之前那两道题 3Sum Closest 和 3Sum 中的方法,采用双指针来做,这里面有个 trick 就是当判断三个数之和小于目标值时,此时结果应该加上 right-left,因为数组排序了以后,如果加上 num[right] 小于目标值的话,那么加上一个更小的数必定也会小于目标值,然后将左指针右移一位,否则将右指针左移一位,参见代码如下:

解法二:

// O(n^2)
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
if (nums.size() < ) return ;
int res = , n = nums.size();
sort(nums.begin(), nums.end());
for (int i = ; i < n - ; ++i) {
int left = i + , right = n - ;
while (left < right) {
if (nums[i] + nums[left] + nums[right] < target) {
res += right - left;
++left;
} else {
--right;
}
}
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/259

类似题目:

3Sum Closest

3Sum

Valid Triangle Number

Two Sum Less Than K

参考资料:

https://leetcode.com/problems/3sum-smaller/

https://leetcode.com/problems/3sum-smaller/discuss/68817/Simple-and-easy-understanding-O(n2)-JAVA-solution

https://leetcode.com/problems/3sum-smaller/discuss/68820/Accepted-and-Simple-Java-O(n2)-solution-with-detailed-explanation

[LeetCode] 3Sum Smaller 三数之和较小值的更多相关文章

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

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

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

  4. [Swift]LeetCode259.三数之和较小值 $ 3Sum Smaller

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

  5. leetcode题目15.三数之和(中等)

    题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...

  6. 【LeetCode】15.三数之和

    题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...

  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 all un ...

  8. LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  9. 3sum 求三数之和等于0,不允许重复

    https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...

随机推荐

  1. go语言赋值

    使用赋值语句可以更新一个变量的值,最简单的赋值语句是将要被赋值的变量放在=的左边,新值的表达式放在=的右边. x = // 命名变量的赋值 *p = true // 通过指针间接赋值 person.n ...

  2. LinqToDB 源码分析——轻谈Linq查询

    LinqToDB框架最大的优势应该是实现了对Linq的支持.如果少了这一个功能相信他在使用上的快感会少了一个层次.本来笔者想要直接讲解LinqToDB框架是如何实现对Linq的支持.写到一半的时候却发 ...

  3. [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合

    [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合 Datasets can often contain components of that require differe ...

  4. WinForm操作隐藏的小知识点

    这次的项目需要使用Winform来实现,我们使用的是CSkin的皮肤.文章主要记录使用过程中遇到的小问题,也是楼主网上查找很久才找到的,这里总结下. 插件地址: 使用SKinDataGridView出 ...

  5. [Winform] DataGridView 中 DataGridViewComboBox 的可编辑

    在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑: 故作如下处 ...

  6. Windows进程间通信—命名管道

    命名管道是通过网络来完成进程间的通信,它屏蔽了底层的网络协议细节.我们在不了解网络协议的情况下,也可以利用命名管道来实现进程间的通信.与Socket网络通信相比,命名管道不再需要编写身份验证的代码.将 ...

  7. Servlet3.0的可插拔功能

    如果说 3.0 版本新增的注解支持是为了简化 Servlet/ 过滤器 / 监听器的声明,从而使得 web.xml 变为可选配置, 那么新增的可插性 (pluggability) 支持则将 Servl ...

  8. java基础思维导图

    如果图片看不清楚的可以把图片另存为桌面放大看哈

  9. 类型转换和类型相关函数.png

  10. AlloyTouch Button插件-不再愁click延迟和点击态

    移动端不能使用click,因为click会有300ms.所有有了fastclick这样的解决方案.然后fastclick并没有解决点击态(用户点击的瞬间要有及时的外观变化反馈)的问题.hover会有不 ...