题目:

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(n2) runtime?



方法一:

class Solution {
public:int threeSumSmaller(vector<int>& nums, int target) {int res = 0;sort(nums.begin(), nums.end());for (int i = 0; i < int(nums.size() - 2); ++i) {int left = i + 1, right = nums.size() - 1, sum = target - nums[i];for (int j = left; j <= right; ++j) {for (int k = j + 1; k <= right; ++k) {if (nums[j] + nums[k] < sum) ++res;}}}return res;}
};

方法二:

  • 双指针

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

转载于:https://www.cnblogs.com/250101249-sxy/p/10421748.html

259 [LeetCode] 3Sum Smaller 三数之和较小值相关推荐

  1. leetcode No.15-16 三数之和相关问题

    leetcode 15. 三数之和 题目 链接:https://leetcode-cn.com/problems/3sum 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 ...

  2. LeetCode实战:三数之和

    题目英文 Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...

  3. leetcode系列--15.三数之和

    leetcode 第15题 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有和为 0 且不重复的三 ...

  4. 刻意练习:LeetCode实战 -- Task04. 三数之和

    背景 本篇图文是LSGO软件技术团队组织的 第二期基础算法(Leetcode)刻意练习训练营 的打卡任务.本期训练营采用分类别练习的模式,即选择了五个知识点(数组.链表.字符串.树.贪心算法),每个知 ...

  5. leetcode算法题--三数之和

    原题链接:https://leetcode-cn.com/problems/3sum/ 排序+双指针法 先将数列从小到大排序 先选择一个数,这一步时间复杂度为O(n) 在这个数后面的数中用双指针分别从 ...

  6. Leetcode:NO.15 三数之和 夹逼

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

  7. LeetCode实战:最接近的三数之和

    题目英文 Given an array nums of n integers and an integer target, find three integers in nums such that ...

  8. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  9. leetcode 16. 3Sum Closest | 16. 最接近的三数之和(双指针)

    题目 https://leetcode.com/problems/3sum-closest/ 题解 方法1:固定 L,双指针找 M.R 时间复杂度 O(n^2),推荐此方法. 证明不会有元素遗漏,详见 ...

最新文章

  1. nginx特定的 404页面利于seo
  2. 2019如何学Python?这里有你需要的答案
  3. Linux读写锁释放,Linux读写锁的使用
  4. QPS、TPS、RT、并发量、 吞吐量
  5. SNF开发平台WinForm之五-高级查询使用说明-SNF快速开发平台3.3-Spring.Net.Framework
  6. lambda 函数式编程_Java 8 Lambda表达式的函数式编程– Monads
  7. Linux 安装 nginx注意
  8. 微信小程序云开发教程-微信小程序的API入门-API的类型和语法结构
  9. 使用 concurrently 并行地运行多个命令(同时跑前端和后端的服务)
  10. Spring Cloud Alibaba之服务治理Nacos
  11. oracle裁员原因_Oracle中国良心裁员:首批900人,赔偿N+6
  12. HTML的id选择器类选择器
  13. python等额本息和等额本金_用Python解读房贷利率,要不要看随你
  14. NUC的型号说明及其他的一些信息
  15. AES 轮密钥(子密钥如何生成).md
  16. 游戏开发计划——数据元素设计(人物)
  17. 基于python3在windows下安装gmpy2
  18. 方法的调用,构造方法,方法的重载
  19. 关于价值链分析法在企业成本管理中应用 (转载)
  20. OpenCV基于Python霍夫圆检测—标准霍夫圆检测

热门文章

  1. Window 的版本号管理策略
  2. Eclipse: Difference between clean, build and publish
  3. Eclipse如何不使用alt+/来实现自动提示
  4. 购物车demo(内含bug)
  5. math java 计算_Java中的数学计算函数汇总
  6. ironpython不想要可以卸载吗_IronPython的致命弱点
  7. 你应该需要知道的前端小技巧
  8. el table 固定表头和首行_vue表格实现固定表头首列
  9. 在Qt界面中显示OpenCV图像
  10. 在python中sqrt是什么意思_python中sqrt是什么意思