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 <= 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 三数之和较小值的更多相关文章
- [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] 259. 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- 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 < ...
- [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 < ...
- leetcode题目15.三数之和(中等)
题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...
- 【LeetCode】15.三数之和
题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...
- 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 ...
- 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 ...
- 3sum 求三数之和等于0,不允许重复
https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...
随机推荐
- OpenID Connect Core 1.0(二)ID Token
2.ID Token(ID Token) OpenID Connect主要是对OAuth 2.0 能够使得终端用户通过ID Token的数据结构进行验证.当客户端和潜在的其他请求声明,ID Token ...
- GPUImage源码解读之GPUImageFramebuffer
简介 OpenGL ES的FrameBuffer是渲染发生的地方,普通的2D图形的渲染默认发生在屏幕上:而三维的图形渲染则除了包括像素点的颜色,还有Depth Buffer,Stencil Buffe ...
- LeetCode 中级 - 救生艇(105)
第 i 个人的体重为 people[i],每艘船可以承载的最大重量为 limit. 每艘船最多可同时载两人,但条件是这些人的重量之和最多为 limit. 返回载到每一个人所需的最小船数.(保证每个人都 ...
- window系统mysql无法输入和无法显示中文的处理配置
第一步:使用记事本打开mysql安装目录下的"my.ini”文件. # MySQL client library initialization. [client] port= [mysql] ...
- linux命令sed与awk是干什么用的,怎么用?
非常强大的文本操纵工具,sed,awk,grep 这个三个命令 都是操作文本文件的unix系统有几个非常命令的特点:1. 对于内核而言,unix文件都是字节序列.io设备也是文件.2. 至于文件的含义 ...
- go加密算法:非对称加密(一)--RSA
椭圆曲线加密__http://blog.51cto.com/11821908/2057726 // MyRas.go package main import ( "crypto/rand&q ...
- Windows 聚焦(锁屏背景)不更新的解决方法
在 Windows Store 搜索 Dynamic theme 安装后可对桌面背景.锁屏界面等进行设置,非常好用!
- s3c2440串口详解
一.UART原理说明 通用异步收发器简称UART(Universal Asynchronous Receiver/Transmitter),它用来传输串行数据:发送数据时,CPU将并行数据写入UART ...
- Django之图书管理系统
出版社的增删改查 展示出版社列表: 1. 创建一个表结构: 2. 再配合那俩条命令即可创建一个press表 创建出版社函数,并在url中进行配置 创建HTML页面,展示出版社的表 for循环 {% ...
- 函数调用方法之__cdecl与_stdcall
在debug VS c工程文件时,碰到cannot convert from 'int (__cdecl *)(char *)' to 'xxx'这个问题,发现问题在于typedef函数指针类型时,将 ...