题目:

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. 类似QQ的聊天工程

    首先建立一个html:<!DOCTYPE html><html lang="en"><head> <meta charset=" ...

  2. synchronized 控制并发(活动秒杀)

    1.首先我们新建一个Controller用于秒杀: package com.imooc.Controller; import com.imooc.service.impl.SeckillService ...

  3. iOS 获取蜂窝网络信号强度 包含iPhoneX XS XR XSMASX (最新)

    1.虽然各种直接获取信号强度的api都被封杀了.但是还有一个另类的黑魔法可以获取到.那就是遍历UIStatusBar了 网络上有的文章写的会崩溃 比如: - (int)getSignalStrengt ...

  4. stack permutation

    #include <iostream> #include <stack> #include <queue> using namespace std; bool ch ...

  5. HDU 5572--An Easy Physics Problem(射线和圆的交点)

    An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  6. Linux 实时查看进程网络的使用情况

    一行代码实现 linux 指定进程网络的使用情况 pid=4203;count=0;while true;do info2=`sed -n '4,100p' /proc/$pid/net/dev |a ...

  7. Java 8 – Map排序

    前提 Map是Java中最常用的集合类之一,这里整理了关于HashMap的排序 (关于List的排序,请查看Collections.sort()的doc或源码). 将无序的HashMap借助Strea ...

  8. FMX相关

    ListView的ItemAppearance的样式效果表: Navicat for 插入图片步骤: 如果最后一条记录的图片有问题,可以先插入下一条再导入图片.

  9. hadoop生态搭建(3节点)-13.mongodb配置

    # 13.mongodb配置_副本集_认证授权# ==================================================================安装 mongod ...

  10. Java线程状态图

    嘤,先盗图一张,后面再补充描述!