当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

从理论到实践:MySQL性能优化和高可用架构,一次讲清

广告电商系统开发功能只订单处理

eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)

【模型部署与业务落地】基于量化芯片的损失分析

基于 Next.js实现在线Excel

Phasecraft连下两城,助力英国量子技术商业化加速!

技术分享| 融合调度系统中的电子围栏功能说明

Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source

MySQL【窗口函数】【共用表表达式】

量化细胞内的信息流:机器学习时代下的研究进展
随机推荐
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
Lecture 4 SVN
xampp安装包含的组件有(php,perl,apche,mysql)
eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
MySQL【窗口函数】【共用表表达式】
开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版
idea去掉spark的日志
实际工作中的高级技术(训练加速、推理加速、深度学习自适应、对抗神经网络)
C# 复制列表
SLAM 04.视觉里程计-1-相机模型
华为手机切换屏幕效果_华为p40页面切换效果怎么换
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
CF1527D MEX Tree (mex & tree & inclusive)
【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
How to install postgresql and configure remote access in ubuntu environment
Notes for xpath getting node with namespace
阿里老鸟终于把测试用例怎么写说的明明白白了,小鸟必看
How to automatically renew the token after it expires?
How to write SQL statements: the usage of Update, Case, and Select together