题目:

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(n2) runtime?



方法一:

class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target) {
int res = ;
sort(nums.begin(), nums.end());
for (int i = ; i < int(nums.size() - ); ++i) {
int left = i + , right = nums.size() - , sum = target - nums[i];
for (int j = left; j <= right; ++j) {
for (int k = j + ; k <= right; ++k) {
if (nums[j] + nums[k] < sum) ++res;
}
}
}
return res;
}
};

方法二:

  • 双指针

    class Solution2 {
    public:
    int threeSumSmaller(vector<int>& nums, int target) {
    if (nums.size() < ) return ;
    int res = , n = nums.size();
    sort(nums.begin(), nums.end());
    for (int i = ; i < n - ; ++i) {
    int left = i + , right = n - ;
    while (left < right) {
    if (nums[i] + nums[left] + nums[right] < target) {
    res += right - left;
    ++left;
    } else {
    --right;
    }
    }
    }
    return res;
    }
    };

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

  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. [Swift]LeetCode259.三数之和较小值 $ 3Sum Smaller

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

  5. leetcode题目15.三数之和(中等)

    题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...

  6. 【LeetCode】15.三数之和

    题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...

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

  8. LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  9. 3sum 求三数之和等于0,不允许重复

    https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...

随机推荐

  1. 一点一点看JDK源码(三)java.util.ArrayList 前偏

    一点一点看JDK源码(三)java.util.ArrayList liuyuhang原创,未经允许禁止转载 本文举例使用的是JDK8的API 目录:一点一点看JDK源码(〇) 1.综述 ArrayLi ...

  2. IOS 文件名获取简洁方式

    //这里有一个模拟器沙盒路径(完整路径) NSString* index=@"/Users/junzoo/Library/Application Support/iPhone Simulat ...

  3. CentOS7 更换阿里云源

    搭建opensack时原生的源不好使就换了个阿里云的源试试 百度搜到的方法:https://blog.csdn.net/chavo0/article/details/51939362 1.备份 # m ...

  4. ubuntu下安装memcached和PHP的memcache扩展

    依赖包和软件包下载地址: Libevent:https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/li ...

  5. vscode vue 项目保存运行lint进行代码修正

    { "editor.tabSize": 2, "files.associations": { "*.vue": "vue" ...

  6. 常用的JavaScript设计模式(一)Constructor(构造器)模式

    在es6中,新增了一个语法糖--class,可以说是为JavaScript引入了类的概念.而在传统的JavaScript中,则是通过构造器生成实例对象的. JavaScript支持特殊的constru ...

  7. .Net Core使用Redis-从安装到使用

    一.安装 本文使用的操作系统是Centos7 在Redis中文网下载最新的Redis压缩包:http://www.redis.cn/ 把包上传到Liunx服务器上,cd 到包所在的目录执行以下命令 # ...

  8. day 22 内置的模块

    1.简单的理解模块 写的每一个py文件都是一个模块,还有一些是我们一直在使用的模块. buildins: 内置模块. print, input random 主要是和随机相关的内容:         ...

  9. python教程(三)·函数与模块

    函数,这和数学中的函数有点关联,但又不是完全等价 概念 不说的这么官方,我就已自己的理解来表达 ^_^ 在数学中,把一个或多个值(输入x)进行一定的计算或者映射,得到一个值(输出y),这个计算或者映射 ...

  10. java 代码块的执行顺序

    举一个实例程序: class HelloA { public HelloA(){ System.out.println("Hello A!父类构造方法"); } { System. ...