当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢
随机推荐
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
《中国综合算力指数》《中国算力白皮书》《中国存力白皮书》《中国运力白皮书》在首届算力大会上重磅发出
职场漫谈:为什么越是内卷的行业越有人争着抢着往里冲?好奇怪的说...
快解析结合友加畅捷U+
[The Art of Hardware Architecture] Study Notes (1) The World of Metastability
Android Sqlite3基本命令
Set partition minimum difference problem (01 knapsack)
量化细胞内的信息流:机器学习时代下的研究进展
JCMsuite应用:倾斜平面波传播透过光阑的传输
[Problem solving] QT update component appears "To continue this operation, at least one valid and enabled repository is required"
License server system does not support this version of this feature
输入输出流总结
相似文本聚类与调参
Crawler - action chain, xpath, coding platform use
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
爬虫——动作链、xpath、打码平台使用
第十六章 源代码文件 REST API 教程(一)
OAID是什么
浙江大学团队使用基于知识图谱的新方法,从空间分辨转录组数据中推断细胞间通信状况








