【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的iupper_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 ...
随机推荐
- 毕业设计之LVS+keealive 负载均衡高可用实现
环境介绍 centos6.9最小化安装 主机名 IPADDR lvsA.load.quan.bbs 192.168.111.131 lvsB.load.quan.bbs 192.168.111.132 ...
- Python基础之流程控制for循环
目录 1. 语法 2. for+break 3. for+continue 4. for循环嵌套 1. 语法 while循环可以对任何内容循环,但循环次数不可控 for循环基于容器类型的长度,循环次数 ...
- pyyaml模块
pyyaml模块是一种文件数据处理格式的方法,常用与生成.解析或修改.yaml配置文件 1.常见.yaml文件格式内容如下 languages: - Ruby - Perl - Python webs ...
- 16. Linux find查找文件及文件夹命令
find的主要用来查找文件,查找文件的用法我们比较熟悉,也可用它来查找文件夹,用法跟查找文件类似,只要在最后面指明查找的文件类型 -type d,如果不指定type类型,会将包含查找内容的文件和文件夹 ...
- Flume(一)【概述】
目录 一.Flume定义 二.Flume基础架构 1.Agent 2.Source 3.Sink 4.Channel 5.Event 一.Flume定义 Flume是Cloudera公司提供的一个 ...
- Vue相关,Vue生命周期及对应的行为
先来一张经典图 生命钩子函数 使用vue的朋友们知道,生命周期函数长这样- mounted: function() { } // 或者 mounted() { } 注意点,Vue的所有生命周期函数都是 ...
- keil 生成 bin 文件 gd32为例
fromelf --bin --output .\update\GD32F4xZ.bin .\Output\GD32450Z_EVAL.axf代表使用的keil内的工具代表输出公式,..表示: 输出 ...
- ListView的item不能点击(焦点冲突问题)
一般这种问题就是item里面有checkbox或button之类抢占焦点的控件,解决方案有2种: 第一种:就是在checkbox或button添加android:focusable="fal ...
- 【编程思想】【设计模式】【行为模式Behavioral】registry
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/registry.py #!/usr/bin/env py ...
- linux系统目录初识
目录 今日内容概要 内容详细 系统目录结构介绍 目录结构知识描述 今日内容概要 系统目录结构介绍 目录结构详细描述 内容详细 系统目录结构介绍 # 1.linux系统中的目录 一切从根开始 结构拥有层 ...