当前位置:网站首页>leetcode: 259. Smaller sum of three numbers
leetcode: 259. Smaller sum of three numbers
2022-08-04 14:37:00 【OceanStar's study notes】
题目来源
题目描述

class Solution {
public:
int threeSumSmaller(vector<int>& nums, int target){
}
};
题目解析
思路
- 先对数组排序
- 固定一个值,然后双指针碰撞.将所有符合条件的[l,r]The interval is counted into the result.
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;
}
};
边栏推荐
猜你喜欢

本周讨论用户体验:Daedalus 的 Nemo 加入 Ambire,探索加密海洋

NPDP|作为产品经理,如何快速提升自身业务素养?

物联网应用发展趋势

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

How to automatically renew the token after it expires?

Centos7 install mysql version rapidly

FRED Application: Capillary Electrophoresis System

MySQL【触发器】

The Internet of things application development trend

leetcode:215无序数组中找第k大的元素
随机推荐
leetcode:253. 至少需要多少间会议室
【北亚数据恢复】IBM System Storage存储lvm信息丢失数据恢复方案
Google plug-in. Download contents file is automatically deleted after solution
Makefile 语法及使用笔记
Workaround without Project Facets
1403. Minimum Subsequence in Non-Increasing Order
如何确定异步 I/O 瓶颈
Qt的QItemDelegate使用
异步编程概览
X射线掠入射聚焦反射镜
【 HMS core 】 【 Media 】 online video editing service 】 【 material can't show, or network anomalies have been Loading state
vim 常用操作命令
Phasecraft连下两城,助力英国量子技术商业化加速!
[机缘参悟-60]:《兵者,诡道也》-1-开篇:“死“与“生“都是天道
Oracle database user creation, restart, import and export
Sum of four squares, laser bombs
【剑指offer33】二叉搜索树的后序遍历序列
F.金玉其外矩阵(构造)
基于数据库实现分布式锁
Rust from entry to proficient 04-variables