当前位置:网站首页>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;
}
};
边栏推荐
- 数据链路层-------以太网协议
- Chinese valentine's day, of course, to learn SQL optimization better leave work early to find objects
- js深拷贝和浅拷贝具体使用区别_es6深拷贝和浅拷贝
- Phasecraft连下两城,助力英国量子技术商业化加速!
- Technology sharing | Mini program realizes audio and video calls
- 技术分享| 融合调度系统中的电子围栏功能说明
- 属于程序猿的浪漫
- 华为手机切换屏幕效果_华为p40页面切换效果怎么换
- 如何和程序员谈恋爱
- MySQL【触发器】
猜你喜欢
leetcode: 212. Word Search II
【历史上的今天】8 月 4 日:第一位图灵奖女性得主;NVIDIA 收购 MediaQ;首届网络安全挑战大赛完成
Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
leetcode:259. 较小的三数之和
量化细胞内的信息流:机器学习时代下的研究进展
leetcode:253. 至少需要多少间会议室
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source
代码随想录笔记_动态规划_1049最后一块石头的重量II
Theory 1: Deep Learning - Detailed Explanation of the LetNet Model
CCF GLCC officially opened | Kyushu Cloud open source experts bring generous bonuses to help universities promote open source
随机推荐
leetcode:255 验证前序遍历序列二叉搜索树
FRED应用:毛细管电泳系统
1375. 二进制字符串前缀一致的次数-前序遍历法
Qt的QItemDelegate使用
NPDP|作为产品经理,如何快速提升自身业务素养?
LeetCode_模拟_中等_498.对角线遍历
解题-->在线OJ(十八)
leetcode:250. 统计同值子树
小 P 周刊 Vol.13
leetcode: 253. How many meeting rooms are required at least
[The Art of Hardware Architecture] Study Notes (1) The World of Metastability
如何和程序员谈恋爱
OAID是什么
Crawler - basic use of selenium, no interface browser, other uses of selenium, cookies of selenium, crawler cases
Kyushu Cloud attended the Navigator Online Forum to discuss the current status, challenges and future of 5G MEC edge computing
Notes for xpath getting node with namespace
FRED Application: Capillary Electrophoresis System
Win10无法访问移动硬盘怎么解决
Centos7 install mysql version rapidly
【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed