LeetCode 3Sum Smaller
原题链接在这里:https://leetcode.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
.
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]
题解:
与3Sum类似. 计算和小于target的组合个数,重复的也要算. 若是nums[i] + nums[j] + nums[k] 符合要求,那么count += k-j, j++. 举例{-2, 0, 1, 3}. target = 4. {-2, 0, 3}符合要求, {-2, 0, 1}也符合要求,一次加上两个.
若是不符合要求说明等于或者大于target了, k--.
Time Complexity: O(n^2). Space: O(1).
AC Java:
public class Solution {
public int threeSumSmaller(int[] nums, int target) {
if(nums == null || nums.length == 0){
return 0;
}
Arrays.sort(nums);
int count = 0;
for(int i = 0; i<nums.length-2; i++){
int j = i+1;
int k = nums.length-1;
while(j<k){
if(nums[i] + nums[j] + nums[k] >= target){
k--;
}else{
count += k-j;
j++;
}
}
}
return count;
}
}
类似Triangle Count, Valid Triangle Number.
LeetCode 3Sum Smaller的更多相关文章
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- 259 [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 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 259. 3Sum Smaller
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
- 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 ...
- [Locked] 3Sum Smaller
3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...
- 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 < ...
随机推荐
- mysql建表建索引
建表: CREATE TABLE `sj_projects` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL ...
- POJ 3276 (开关问题)
题目链接: http://poj.org/problem?id=3276 题目大意:有一些牛,头要么朝前要么朝后,现在要求确定一个连续反转牛头的区间K,使得所有牛都朝前,且反转次数m尽可能小. 解题思 ...
- into outfile 生成sql脚本
select concat('insert into t_dm_stage(STAGE_ID,STAGE_NAME) values(',STAGE_ID,',','\'',STAGE_NAME,'\' ...
- ACM 寻找最大数
寻找最大数 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 请在整数 n 中删除m个数字, 使得余下的数字按原次序组成的新数最大, 比如当n=920813467185 ...
- ACM Minimum Inversion Number 解题报告 -线段树
C - Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...
- cdoj 1334 郭大侠与Rabi-Ribi Label:贪心+数据结构
郭大侠与Rabi-Ribi Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) 最近 ...
- hbases索引技术:Lily HBase Indexer介绍
Lily HBase Indexer 为hbase提供快速查询,他允许不写代码,快速容易的把hbase行索引到solr.Lily HBase Indexer drives HBase indexing ...
- 纪念逝去的岁月——C++实现一个栈
1.代码 2.运行结果 1.代码 stack.cpp #include <stdio.h> #include <string.h> class ClsStack { priva ...
- 使用RESTClient插件进行数据模拟(GET,POST)提交
1:在Firefox中下载RESTClient插件安装 2:安装好后的界面 (chrome://restclient/content/restclient.html) 3:选择GET/POST,输入U ...
- mysql 存储过程 删除重复
DELIMITER $$ CREATE PROCEDURE `delRepeatCA`() BEGIN DECLARE tally INT DEFAULT 0; SELECT COUNT(rs.c_C ...