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?

class Solution {
public int threeSumSmaller(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
Arrays.sort(nums);
int res = 0;
for (int i = 0; i < nums.length - 2; i++) {
int start = i + 1, end = nums.length - 1;
while (start < end) {
if (nums[i] + nums[start] + nums[end] < target) {
// assume the end - 1... met
res += end - start;
start += 1;
} else {
end -= 1;
}
}
}
return res;
}
}

[LC] 259. 3Sum Smaller的更多相关文章

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

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

  2. 259. 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 259. 3Sum Smaller (三数之和较小值) $

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

  4. 259. 3Sum Smaller小于版3sum

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

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

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

  7. Leetcode 259. 3Sum Smaller

    class Solution(object): def threeSumSmaller(self, nums, target): """ :type nums: List ...

  8. 【LeetCode】259. 3Sum Smaller 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 双指针 日期 题目地址:https://le ...

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

随机推荐

  1. Java学习——代理模式

    Java中的三种代理模式 一,什么是代理模式? 代理模式是一种设计模式,简单的来说就是在不改变源码的情况下,实现对目标对象的功能扩展. 比如有个歌手对象叫Singer,这个对象有一个唱歌方法叫sing ...

  2. Java--HashMap排序

    package connection; import java.util.Collections; import java.util.Comparator; import java.util.Hash ...

  3. Servlet基本概念及其部署

    什么servlet? Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动 ...

  4. 关于WIN7系统,在运行pycharm时,老出现问题

    今天在pycharm中写python代码的时候,一直跳出一个窗口: 后来经过上网查询,得出针对此类问题的解决办法如下: (1)在运行中输入“Regedit” (2)HKEY_CURRENT_USER— ...

  5. vue中报错Props with type Object/Array must use a factory function to return the default value

    Invalid default value for prop "value": Props with type Object/Array must use a factory fu ...

  6. liunx搭建配置

    预安装准备工具 yum -y install gcc gcc-c++ wget cmake 安装 软件存放目录: /lnmp/ 软件安装目录: /usr/local/ 1.下载安装PCRE wget ...

  7. F5负载均衡综合实例详解(转)

    转载自:https://blog.csdn.net/weixin_43089453/article/details/87937994  女程序员就不脱发了吗来源于:<网络运维与管理>201 ...

  8. Spring Boot 中集成 Shiro

    https://blog.csdn.net/taojin12/article/details/88343990

  9. 第1章 分布式系统概念与ZooKeeper简介

    ZooKeeper分布式专题与Dubbo微服务入门 第1章 分布式系统概念与ZooKeeper简介 1-1 zookeeper简介 1-2 什么是分布式系统 略 1-3 分布式系统的瓶颈以及zk的相关 ...

  10. Single设计模式

    JavaSE基础中学习的single设计模式复习: * 单列设计模式概念理解:用程序实现在存储中只能有一个对象. *  * 恶汉式实现思路分析: * 1.如何实现类不能被其他人多次创建呢? * 实现: ...