当前位置:网站首页>leetcode: 259. Smaller sum of three numbers
leetcode: 259. Smaller sum of three numbers
2022-08-04 14:37:00 【OceanStar's study notes】
题目来源
题目描述
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target){
}
};
题目解析
思路
- 先对数组排序
- 固定一个值,然后双指针碰撞.将所有符合条件的[l,r]The interval is counted into the result.
class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target){
int res = 0;
std::sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); ++i) {
int L = i + 1, R = nums.size() - 1;
while (L < R){
int sum = nums[i] + nums[L] + nums[R];
if(sum >= target){
R--;
}else{
res += (R - L);
L++;
}
}
}
return res;
}
};
边栏推荐
猜你喜欢
Centos7 install mysql version rapidly
【硬件架构的艺术】学习笔记(1)亚稳态的世界
1403. 非递增顺序的最小子序列
【剑指offer33】二叉搜索树的后序遍历序列
Problem solving-->Online OJ (18)
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
uni-app 从零开始-生命周期(二)
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
FRED应用:毛细管电泳系统
Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
随机推荐
OAID是什么
实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)
leetcode:250. 统计同值子树
Rust from entry to proficient 04-variables
Rust 从入门到精通04-变量
[Beiya data recovery] IBM System Storage storage lvm information lost data recovery solution
Go 语言快速入门指南: 变量和常量
爬虫——动作链、xpath、打码平台使用
数据链路层-------以太网协议
Oracle database user creation, restart, import and export
砺夏行动|九州云章津楠:开源不是少数人的运动,大众化才是源泉
Set partition minimum difference problem (01 knapsack)
G. Mountaineering Squad (violence & dfs)
RS|哨兵二号(.SAFE格式)转tif格式
JCMsuite应用:倾斜平面波传播透过光阑的传输
九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来
How to write SQL statements: the usage of Update, Case, and Select together
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
The Internet of things application development trend
uni-app 从零开始-生命周期(二)