当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

leetcode:241. 为运算表达式设计优先级

JCMsuite应用:倾斜平面波传播透过光阑的传输

九州云出席领航者线上论坛,共话5G MEC边缘计算现状、挑战和未来

16. Learn MySQL Regular Expressions

Technology sharing | Description of the electronic fence function in the integrated dispatching system

量化细胞内的信息流:机器学习时代下的研究进展

Almost all known protein structures in the world are open sourced by DeepMind

eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)

LeetCode_模拟_中等_498.对角线遍历

Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
随机推荐
eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
利用决策树找出最优特征组合
Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
vim 常用操作命令
AOSP built-in APP franchise rights white list
Hangzhou Electric School Competition (Counter Attack Index)
How to automatically renew the token after it expires?
16. Learn MySQL Regular Expressions
B.构造一个简单的数列(贪心)
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
Rust from entry to proficient 04-variables
leetcode: 253. How many meeting rooms are required at least
如何确定异步 I/O 瓶颈
杭电校赛(ACM组队安排)
[The Art of Hardware Architecture] Study Notes (1) The World of Metastability
MySQL【触发器】
CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
SQL语句的写法:Update、Case、 Select 一起的用法
1403. 非递增顺序的最小子序列
Problem solving-->Online OJ (18)