【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 <= 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(n^2) runtime?
题解:
(我没舍得花钱。。。咳咳,这样不太好QAQ,代码都没有提交过,所以以后来看得注意一下代码的正确性。)
老规矩,第一个想法是啥?必须的暴力解啊,哈哈,虽然时间复杂度是O(n^3);这里
Solution 1 ()
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int cnt = ;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
if(i> && nums[i] == nums[i-]) continue;
for(int j=i+; j<n-; j++) {
if(j>i+ && nums[j] == nums[j-]) continue;
for(int k=j+; k<n; k++) {
if(k>j+ && nums[k] == nums[k-]) continue;
if(nums[i] + nums[j] + nums[k] < target) cnt++;
else break;
}
}
}
}
};
若想时间复杂度为O(n^2),那么就需要和之前的3Sum题目一样,两指针为剩余数组的头尾指针,搜索遍历。这里需要注意的是,当sum<target时,cnt需要加上k-j,而不是cnt++,因为数组已经排好序,若尾指针当处于位置k时满足条件,则j<position<=k的pos都满足这个条件,故cnt需加上k-j.
Solution 2 ()
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int cnt = ;
sort(nums.begin(), nums.end());
int n = nums.size();
for(int i=; i<n-; i++) {
if(i> && nums[i] == nums[i-]) continue;
int j = i + , k = n - ;
while(j<k) {
int sum = nums[i] + nums[j] + nums[k];
if(sum < target) {
cnt += k - j;
j++;
}
else k--;
}
}
return cnt;
}
};
【LeetCode】259 3Sum Smaller的更多相关文章
- 【LeetCode】259. 3Sum Smaller 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【LeetCode】16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- 【LeetCode】15. 3Sum 三个数和为0
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 【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 ...
- 【LeetCode】015 3Sum
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 【LeetCode】016 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
随机推荐
- Bootstrap学习速查表(四) 栅格系统
Bootstrap框架的网格系统工作原理如下: 1.数据行(.row)必须包含在容器(.container)中,以便为其赋予合适的对齐方式和内距(padding).如: 2.在行(.row)中可以添加 ...
- Java的Executor框架和线程池实现原理
Java的Executor框架 1,Executor接口 public interface Executor { void execute(Runnable command); } Executor接 ...
- servletResponse 请求重定向
package response;/* * 重定向特点: * 1,浏览器会向服务器发送两次请求,意味着就有两个request\response * 2,用重定向技术,浏览器地址栏会发生变化 * * ...
- android 用webView作为编辑器 各种问题
1.首先我要说明一下为什么要写这个博客,因为公司最近需要一个自定义的编辑器,苦于没有思路在网上找了好久,看到了好多android实现的编辑器(其实也就那么几个并不多),公司需求和网页端同步共享创建的文 ...
- linux环境tomcat配置及hadoop 2.6伪分布模式安装配置
一.ubuntu 15.04.openjdk1.7.tomcat7环境配置 1. 配置openjdk1.7,输入命令: -jdk 2. 查看java是否安装成功,输入命令: envjava -vers ...
- 相机标准之onvif---开放型网络视频接口论坛onvif 简介
什么是ONVIF ? ONVIF:原意为 开放型网络视频接口论坛,即 Open Network Video Interface Forum ,是安讯士.博世.索尼等三家公司在2008年共同成立的一个国 ...
- android 导入项目 项目中文字乱码问题
乱码问题出现了几次,一直没有在意,今天又出现了,现总结如下: eclipse之所以会出现乱码问题是因为eclipse编辑器选择的编码规则是可变的.一般默认都是UTF-8或者GBK,当从外部导入的一个工 ...
- 【BZOJ4605】崂山白花蛇草水 权值线段树+kd-tree
[BZOJ4605]崂山白花蛇草水 Description 神犇Aleph在SDOI Round2前立了一个flag:如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇Aleph的实力,他轻松地进了 ...
- 转 EBP ESP 的理解
PS:EBP是当前函数的存取指针,即存储或者读取数时的指针基地址:ESP就是当前函数的栈顶指针.每一次发生函数的调用(主函数调用子函数)时,在被调用函数初始时,都会把当前函数(主函数)的EBP压栈,以 ...
- Java基础教程:HashTable与HashMap比较
Java基础教程:HashTable与HashMap比较 1. 关于HashMap的一些说法: a) HashMap实际上是一个“链表散列”的数据结构,即数组和链表的结合体.HashMap的底层结 ...