【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 ...
随机推荐
- PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组
PHP 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 <?php /** * 获取两个日期相差多少年,多少月,多少天,多少小时,并填充数组 * @param [type] $st ...
- PHP-FPM运行状态的实时查看及监控详解
https://www.jb51.net/article/97640.htm https://blog.csdn.net/Dr_cokiy/article/details/105580758
- Java 读取txt文件生成Word文档
本文将以Java程序代码为例介绍如何读取txt文件中的内容,生成Word文档.在编辑代码前,可参考如下代码环境进行配置: IntelliJ IDEA Free Spire.Doc for Java T ...
- 重新整理 .net core 实践篇——— endpoint[四十七]
前言 简单整理一些endpoint的一些东西,主要是介绍一个这个endpoint是什么. 正文 endpoint 从表面意思是端点的意思,也就是说比如客户端的某一个action 是一个点,那么服务端的 ...
- Vue3项目搭建规范
Vue3项目搭建规范 一. 代码规范 1.1 集成editorconfig配置 EditorConfig有助于为不同IDE编辑器上维护一致的编码风格 安装插件:EditorConfig for VS ...
- 学习java第十九天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- API 管理在云原生场景下的机遇与挑战
作者 | 张添翼 来源 | 尔达Erda公众号 云原生下的机遇和挑战 标准和生态的意义 自从 Kubernetes v1.0 于 2015 年 7 月 21 日发布,CNCF 组织随后建立以来,其 ...
- 疯了吧!这帮人居然用 Go 写“前端”?(一)
作者 | 郑嘉涛(羣青) 来源 | 尔达 Erda 公众号 无一例外,谈到前后端分离"必定"是 RESTful API,算是定式了.但我们知道 REST 在资源划分上的设计总是 ...
- 多人协作解决方案,git flow的使用
简介 Gitflow工作流程围绕项目发布定义了严格的分支模型. 为不同的分支分配了非常明确的角色,并且定义了使用场景和用法.除了用于功能开发的分支,它还使用独立的分支进行发布前的准备.记录以及后期维护 ...
- Java对象的创建过程:类的初始化与实例化
一.Java对象创建时机 我们知道,一个对象在可以被使用之前必须要被正确地实例化.在Java代码中,有很多行为可以引起对象的创建,最为直观的一种就是使用new关键字来调用一个类的构造函数显式地创建对象 ...