原题链接在这里: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 CountValid Triangle Number.

LeetCode 3Sum Smaller的更多相关文章

  1. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  2. 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 ...

  3. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

  4. [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 ...

  5. [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 ...

  6. 259. 3Sum Smaller

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  7. 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 ...

  8. [Locked] 3Sum Smaller

    3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...

  9. 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 < ...

随机推荐

  1. Posterior visual bounds retrieval for the Plato framework

    Plato is a MVVM compliant 2D on-canvas graphics framework I've been designing and implementing for d ...

  2. 这次,雅虎真的撤销QA团队了

    在一个软件开发过程中取消了质量保证团队会发生什么?更少,而不是更多的错误,以及一个大大加快的开发周期. 至少,根据雅虎的经验,确实如此.该公司的首席设计师Amotz Maimon,以及科学与技术高级副 ...

  3. 【BZOJ2438】 [中山市选2011]杀人游戏 tarjan强连通分量+缩点

    Description 一位冷血的杀手潜入 Na-wiat,并假装成平民.警察希望能在 N 个人里面,查出谁是杀手. 警察能够对每一个人进行查证,假如查证的对象是平民,他会告诉警察,他认识的人, 谁是 ...

  4. Android -- ImageSwitch和Gallery 混合使用

    1. 实现效果

  5. Linux 下找出超過某些容量的檔案

    找目前所在位置下,所有檔案大小超過3M的file,並列出檔名:大小 find . -type f -size +3M -exec ls -alh {} \; | awk '{print$9 " ...

  6. Json数据格式事例查看

    最简单的一种: [{"ProvinceID":1,"ProvinceName":"北京市","DateCreated": ...

  7. 用c#实现$.now()(1437813924915)的时间效果

    Convert.ToInt64((DateTime.UtcNow - Convert.ToDateTime("1970-01-01")).TotalMilliseconds)

  8. Jenkins 2.7.3 LTS 发布

    更新如下: Stop A/B testing of the remoting JNLP3 protocol due to the known issues. The protocol can be e ...

  9. Apple Special Event, October 2013 (1080p)(苹果发布会)

    Apple iPhone 5C(16G) 3G智能手机(粉色 北京联通惊喜合约价3899,购机送费含200话费,率先上市,限量抢购)

  10. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...