当前位置:网站首页>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;
}
};
边栏推荐
- How to install postgresql and configure remote access in ubuntu environment
- F.金玉其外矩阵(构造)
- Bluetooth Technology|In the first half of the year, 1.3 million charging piles were added nationwide, and Bluetooth charging piles will become the mainstream of the market
- idea去掉spark的日志
- 输入输出流总结
- CF1527D MEX Tree(mex&树&容斥)
- Go 语言快速入门指南: 变量和常量
- 兆骑科创创新创业大赛活动举办,线上直播路演,投融资对接
- 集合划分差最小问题(01背包)
- [LeetCode] 38. Appearance sequence
猜你喜欢
随机推荐
兆骑科创创新创业大赛活动举办,线上直播路演,投融资对接
leetcode:212. 单词搜索 II
利用决策树找出最优特征组合
字符串类的设计与实现_C语言字符串编程题
【剑指offer59】队列的最大值
xampp安装包含的组件有(php,perl,apche,mysql)
[LeetCode] 38. Appearance sequence
Cisco-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
如何在ubuntu环境下安装postgresql并配置远程访问
[Problem solving] QT update component appears "To continue this operation, at least one valid and enabled repository is required"
1401 - Web technology 】 【 introduction to graphical Canvas
Qt的QItemDelegate使用
Technology sharing | Mini program realizes audio and video calls
四平方和,激光炸弹
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
一看就会的Chromedriver(谷歌浏览器驱动)安装教程
token 过期后,如何自动续期?
华为手机切换屏幕效果_华为p40页面切换效果怎么换
RS|哨兵二号(.SAFE格式)转tif格式
并发程序的隐藏杀手——假共享(False Sharing)









