当前位置:网站首页>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;
}
};
边栏推荐
- 【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
- Set partition minimum difference problem (01 knapsack)
- ICML 2022 | 图神经网络的局部增强
- Rust 从入门到精通04-变量
- 广告电商系统开发功能只订单处理
- Crawler - action chain, xpath, coding platform use
- 【模型部署与业务落地】基于量化芯片的损失分析
- 企业级优化
- 技术分享| 小程序实现音视频通话
- FRED应用:毛细管电泳系统
猜你喜欢
随机推荐
【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
F.金玉其外矩阵(构造)
Redis 复习计划 - Redis主从数据一致性和哨兵机制
手搓一个“七夕限定”,用3D Engine 5分钟实现烟花绽放效果
基于 Next.js实现在线Excel
技术分享| 融合调度系统中的电子围栏功能说明
零基础可以转行软件测试吗 ?这篇文章告诉你
杭电校赛(ACM组队安排)
MySQL【触发器】
Crawler - action chain, xpath, coding platform use
编译型与解释型编程语言的区别
并发程序的隐藏杀手——假共享(False Sharing)
How to install postgresql and configure remote access in ubuntu environment
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
1375. 二进制字符串前缀一致的次数-前序遍历法
Win11勒索软件防护怎么打开?Win11安全中心勒索软件防护如何设置
ACL 2022 | 社会科学理论驱动的言论建模
xampp安装包含的组件有(php,perl,apche,mysql)
【问题解决】QT更新组件出现 “要继续此操作,至少需要一个有效且已启用的储存库”
How to Identify Asynchronous I/O Bottlenecks