当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
开放麒麟 openKylin 版本规划敲定:10 月发布 0.9 版并开启公测,12 月发布 1.0 版
小 P 周刊 Vol.13
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
【HMS core】【Media】【视频编辑服务】 在线素材无法展示,一直Loading状态或是网络异常
【剑指offer33】二叉搜索树的后序遍历序列
字符串类的设计与实现_C语言字符串编程题
AlphaFold 如何实现 AI 在结构生物学中的全部潜力
Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
在腾讯,我的试用期总结!
信创是什么意思?涉及哪些行业?为什么要发展信创?
centos7安装mysql急速版
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
用了TCP协议,就一定不会丢包吗?
相似文本聚类与调参
爬虫——动作链、xpath、打码平台使用
基于 Next.js实现在线Excel
G.登山小分队(暴力&dfs)
四平方和,激光炸弹
How to write SQL statements: the usage of Update, Case, and Select together
Rust 从入门到精通04-变量