当前位置:网站首页>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;
}
};
边栏推荐
- metaRTC5.0新版本支持mbedtls(PolarSSL)
- 如何才能有效、高效阅读?猿辅导建议“因材因时施教”
- 如何通过使用“缓存”相关技术,解决“高并发”的业务场景案例?
- Almost all known protein structures in the world are open sourced by DeepMind
- NPDP|作为产品经理,如何快速提升自身业务素养?
- 华为手机切换屏幕效果_华为p40页面切换效果怎么换
- Rust from entry to proficient 04-variables
- 中大型商业银行堡垒机升级改造就用行云管家!必看!
- CF1527D MEX Tree (mex & tree & inclusive)
- [The Art of Hardware Architecture] Study Notes (1) The World of Metastability
猜你喜欢
随机推荐
G.登山小分队(暴力&dfs)
零基础可以转行软件测试吗 ?这篇文章告诉你
MySQL【触发器】
How to write SQL statements: the usage of Update, Case, and Select together
四平方和,激光炸弹
爬虫——动作链、xpath、打码平台使用
企业级优化
G. Mountaineering Squad (violence & dfs)
华为手机切换屏幕效果_华为p40页面切换效果怎么换
世间几乎所有已知蛋白质结构,都被DeepMind开源了
[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
Problem solving-->Online OJ (18)
文盘Rust -- 配置文件解析
Centos7 install mysql version rapidly
Makefile 语法及使用笔记
C# 动态加载卸载 DLL
基于 Next.js实现在线Excel
[深入研究4G/5G/6G专题-50]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-10-高可靠性技术-1-低编码率编码调制方案MCS与高可靠性DRB
Android Sqlite3基本命令
七夕邂逅爱,那人一定在