[Locked] 3Sum Smaller
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?
分析:
1、暴力做法O(n^3)
2、如果题目是等于target,那么一种很直观的想法,对于nums中每个数,用target去减,然后问题就转换成了O(n)复杂度解决两数之和为target。
3、这题是小于target,用hash的方法复杂度为O(n^2*logn),若先排序,然后用夹逼的方法做复杂度可为O(n^2);
代码:
int tripletsNumber(vector<int> &nums, int target) {
int count = ;
sort(nums.begin(), nums.end());
for(int i = ; i < nums.size(); i++) {
//从后面两个数的取值界限定为i,这样保证triplets不重复
int j = i + , k = int(nums.size()) - , newt = target - nums[i];
while(j < k) {
if(nums[j] + nums[k] < newt) {
count += k - j;
j++;
}
else
k--;
}
}
return count;
}
[Locked] 3Sum Smaller的更多相关文章
- 3Sum Closest & 3Sum Smaller
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- 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 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- 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] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.com/problems/3sum-smaller/ 题目: Given an array of n integers nums and a targ ...
- 【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 ...
随机推荐
- Android手机修改Hosts的方法
Android手机是和Google帐号紧密联系的,由于中国的操蛋情况,很多时候Google帐号无法登录,导致Android市场无法使用. 在电脑上我们通过修改Hosts方法可以解决Google帐号的登 ...
- power desinger 学习笔记<一>
如果一张表有 很多字段(多于30个),那么一个一个复制粘贴,耗时耗力.可以偷懒,事先编辑好 sql脚本,然后把sql脚本导入 power designer,是不是很方便? 看下面的 1. 打开Pow ...
- 【转】 ios开发之倒计时实现的两种方法
原文:http://blog.csdn.net/kylinbl/article/details/8972261 方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTime ...
- const和volatile的区别
一.关键字const有什么含意? 只要一听到说:“const意味着常数”,就知道我正在和一个业余者打交道.去年Dan Saks已经在他的文章里完全概括了const的所有用法,因此ESP(译者:Embe ...
- hdoj 1060
代码: #include <stdio.h>#include <math.h> int main(){ int t; while(scanf("%d&qu ...
- SGU 190.Dominoes(二分图匹配)
时间限制:0.25s 空间限制:4M 题意: 给定一个N*N的棋盘,一些格子被移除,在棋盘上放置一些1*2的骨牌,判定能否放满,并且输出任意方案. Solution: 首先考虑对棋盘的一个格子黑白染色 ...
- HDU 4010.Query on The Trees 解题报告
题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...
- linux安装时提示发生不正常错误问题
跳过md5系统较检(每个系统版本都有一个md5编码唯一) 在安装CentOS时 提示 找不到磁盘,是否安装程序,选择安装程序进行"下一步" 提示: 发生不规则,不正常错误 原因:没 ...
- getJSON回调函数不执行问题?
利用getJSON异步请求时,回调函数不执行,不知道是什么问题? php 返回数据 header("Content-type:text/json"); echo json_enco ...
- Linux系统上使用php获取apk信息
最近在做一个apk商城,需要在用户上传了apk之后系统自动读取apk信息(包名,版本号等),后台语言使用的是php,需要php去调用系统的aapt命令去读取apk信息,在Linux系统上安装aapt的 ...