【LeetCode】259. 3Sum Smaller 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/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
.
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?
题目大意
给定一个长度为 n 的整数数组和一个目标值 target,寻找能够使条件 nums[i] + nums[j] + nums[k] < target
成立的三元组 i, j, k
个数(0 <= i < j < k < n)
。
解题方法
二分查找
先对数组进行排序。
固定i, j
找出k
,使得nums[k] >= target - nums[i] - nums[j]
。
此时满足nums[i] + nums[j] + nums[k] < target
成立的三元组 i, j, k
个数是 k - j - 1
个。
lower_bound()
找出A[i] >= target
的i
upper_bound()
找出A[i] > target
的i
所以使用lower_bound()找出大于等于target - nums[i] - nums[j]
的k位置,此位置的k是第一个不满足题设的位置。因此满足条件的k在左边,共有k - j - 1
个。
另外二分查找的时候,并不是从头开始查找,而是从nums.begin() + j + 1
查找,即j的下一个元素位置开始。
时间复杂度O(N^2 * log(N)).
C++代码如下:
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
const int N = nums.size();
if (N <= 2) return 0;
sort(nums.begin(), nums.end());
int res = 0;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
int numsk = target - nums[i] - nums[j];
int k = lower_bound(nums.begin() + j + 1, nums.end(), numsk) - nums.begin();
res += k - j - 1;
}
}
return res;
}
};
双指针
j
指向起始,k
指向结束,找到nums[j] + nums[k] < target - nums[i]
的区间长度,里面的元素全都符合。
- 如果
nums[j] + nums[k] >= target - nums[i]
,说明k太大,需要k–; - 如果
nums[j] + nums[k] < target - nums[i]
,说明满足条件,又由于j比较小,需要j++;
时间复杂度O(N^2).
C++代码如下:
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
const int N = nums.size();
if (N <= 2) return 0;
sort(nums.begin(), nums.end());
int res = 0;
for (int i = 0; i < N; ++i) {
int j = i + 1;
int k = N - 1;
while (j < N && k > i && j != k) {
if (nums[j] + nums[k] >= target - nums[i]) {
k --;
} else {
res += k - j;
j ++;
}
}
}
return res;
}
};
日期
2019 年 9 月 22 日 —— 熬夜废掉半条命
【LeetCode】259. 3Sum Smaller 解题报告 (C++)的更多相关文章
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
- [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 < ...
- Leetcode 259. 3Sum Smaller
class Solution(object): def threeSumSmaller(self, nums, target): """ :type nums: List ...
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
随机推荐
- Demo01无重复数字
package 习题集2;//有1,2,3,4四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?public class Demo1 { public static void main(S ...
- 学习java 7.10
学习内容: List 集合:有序集合,用户可以精确控制列表中每个元素的插入位置 List 集合特点:有序:存储和取出的元素顺序一致 可重复:存储的元素可以重复 增强for循环:简化数组和 Collec ...
- A Child's History of England.3
So, Julius Caesar came sailing over to this Island of ours, with eighty vessels and twelve thousand ...
- flink02------1.自定义source 2. StreamingSink 3 Time 4窗口 5 watermark
1.自定义sink 在flink中,sink负责最终数据的输出.使用DataStream实例中的addSink方法,传入自定义的sink类 定义一个printSink(),使得其打印显示的是真正的ta ...
- json模块中函数的用法
json模块中主要使用四个函数:json.load(),json.dump(),json.loads(),json.dumps() json.loads()是将一个json编码的字符串转换成pytho ...
- github单独下载某一个文件夹
可以借助svn工具进行下载,实现只下载repo下的指定文件夹内容 背景 需要下载这个文件夹下所有内容https://github.com/rabbitmq/rabbitmq-tutorials/tre ...
- Mysql的行级锁
我们首先需要知道的一个大前提是:mysql的锁是由具体的存储引擎实现的.所以像Mysql的默认引擎MyISAM和第三方插件引擎 InnoDB的锁实现机制是有区别的. Mysql有三种级别的锁定:表级锁 ...
- struct vs class in C++
在C++中,除了以下几点外,struct和class是相同的. (1)class的成员的默认访问控制是private,而struct的成员的默认访问权限是public. 例如,program 1会编译 ...
- PL/SQL实例1
declare --定义游标 cursor cemp is select to_char(hiredate,'yyyy') from emp; phiredate varchar2(4); ...
- Linkerd Service Mesh 服务配置文件规范
服务配置文件 为 Linkerd 提供有关服务的附加信息. 以下是可以使用服务配置文件完成的所有操作的参考. 系列 中文手册(https://linkerd.hacker-linner.com) Sp ...