当前位置:网站首页>leetcode:259. 较小的三数之和
leetcode:259. 较小的三数之和
2022-08-04 14:31:00 【OceanStar的学习笔记】
题目来源
题目描述

class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target){
}
};
题目解析
思路
- 先对数组排序
- 固定一个值,然后双指针碰撞。将所有符合条件的[l,r]区间都算到结果里面。
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;
}
};
边栏推荐
- vim common operation commands
- [机缘参悟-60]:《兵者,诡道也》-1-开篇:“死“与“生“都是天道
- Win11快速助手在哪里?Win11打开快速助手的方法
- 从理论到实践:MySQL性能优化和高可用架构,一次讲清
- 【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
- 本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋
- 2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
- The Internet of things application development trend
- 【剑指offer59】队列的最大值
- 一看就会的Chromedriver(谷歌浏览器驱动)安装教程
猜你喜欢
随机推荐
The Internet of things application development trend
Redis 复习计划 - Redis主从数据一致性和哨兵机制
考研上岸又转行软件测试,从5k到13k完美逆袭,杭州校区小哥哥拒绝平庸终圆梦!
ACL 2022 | 社会科学理论驱动的言论建模
编程思想_编程有必要给孩子学吗?
Problem solving-->Online OJ (18)
代码随想录笔记_动态规划_1049最后一块石头的重量II
vim common operation commands
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
Workaround without Project Facets
eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
杭电校赛(逆袭指数)
CCF GLCC正式开营|九州云开源专家携丰厚奖金,助力高校开源推广
郑轻新生校赛和中工选拔赛题解
[in-depth study of 4 g / 5 g / 6 g project - 50] : URLLC - 16 - the 3 GPP URLLC agreement, specification, technical principle of depth interpretation - 10 - high reliability technology - 1 - low codin
OAID是什么
16、学习MySQL 正则表达式
【模型部署与业务落地】基于量化芯片的损失分析
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
How to Identify Asynchronous I/O Bottlenecks









