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 <= 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 = ;
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;
}
};
方法二:
- 双指针
class Solution2 {
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;
}
};
259 [LeetCode] 3Sum Smaller 三数之和较小值的更多相关文章
- [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] 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 259. 3Sum Smaller (三数之和较小值) $
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [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 < ...
- leetcode题目15.三数之和(中等)
题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...
- 【LeetCode】15.三数之和
题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...
- 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 ...
- 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 ...
- 3sum 求三数之和等于0,不允许重复
https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...
随机推荐
- Sftp搭建与配置参考
Sftp搭建与配置参考 1. 介绍 sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一 ...
- 学习笔记 - Manacher算法
Manacher算法 - 学习笔记 是从最近Codeforces的一场比赛了解到这个算法的~ 非常新奇,毕竟是第一次听说 \(O(n)\) 的回文串算法 我在 vjudge 上开了一个[练习],有兴趣 ...
- Oracle->oracle单实例Shell脚本[20180122]
脚本主要用于redhat平台安装11g和12c软件 依赖包检查与安装 用户.组检查与安装 系统内核.用户限制 防火墙.selinux关闭 注意,linux组脚本 ...
- Linux 实时查看进程网络的使用情况
一行代码实现 linux 指定进程网络的使用情况 pid=4203;count=0;while true;do info2=`sed -n '4,100p' /proc/$pid/net/dev |a ...
- mysql 8.0.12 日常出错
最近不知道怎么回事,数据库老是会输出一个: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and co ...
- 微信小程序调用api接口
请求的第三方微信url大概有3种 1)$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&s ...
- Java并发编程:浅析几种线程安全模型 [转]
多线程编程一直是老生常谈的问题,在Java中,随着JDK的逐渐发展,JDK提供给我们的并发模型也越来越多,本文摘取三例使用不同原理的模型,分析其大致原理.目录如下: 1.COW之CopyOnWrite ...
- Java常用容器对比
ArrayList与Vector ArrayList和Vector内部都是由数组实现的,数组实现的优点就是支持元素的随机访问(O(1)),但是在对元素进行插入和删除操作时,需要向后或向前移动数组,这样 ...
- VSCode 配置 C++
每次换台电脑写c++,就要找配置,很是繁琐.这次自己写篇博客,记录下相关配置过程. 安装编译器 打开下面的网站 http://www.msys2.org ,下载 64 位的 MSYS2,按照主页上的步 ...
- JS中数组方法的封装之slice
slice方法的功能 // 1) : 数组的截取 // 2) :slice(m,n): 从数组索引m开始,截取到索引n,但是不包含n;[前包后不包] // slice(m) : 从索引m开始,截取到末 ...