当前位置:网站首页>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;
}
};
边栏推荐
- The Internet of things application development trend
- Technology sharing | Description of the electronic fence function in the integrated dispatching system
- 杭电校赛(ACM组队安排)
- 如何在ubuntu环境下安装postgresql并配置远程访问
- 蓝牙技术|上半年全国新增 130 万台充电桩,蓝牙充电桩将成为市场主流
- 如何和程序员谈恋爱
- Workaround without Project Facets
- Lecture 4 SVN
- [LeetCode] 38. Appearance sequence
- Rust from entry to proficient 04-variables
猜你喜欢
随机推荐
[in-depth study of 4 g / 5 g / 6 g project - 50] : URLLC - 16 - the 3 GPP URLLC agreement, specification, technical principle of depth interpretation - 10 - high reliability technology - 1 - low codin
MySQL【窗口函数】【共用表表达式】
LCP 06. 拿硬币-遍历
代码随想录笔记_动态规划_1049最后一块石头的重量II
解题-->在线OJ(十八)
异步编程概览
零基础可以转行软件测试吗 ?这篇文章告诉你
Technology sharing | Mini program realizes audio and video calls
RS|哨兵二号(.SAFE格式)转tif格式
CF1527D MEX Tree(mex&树&容斥)
Unity插件:使用PopulationSystem制作行走交流的路人
属于程序猿的浪漫
How to install postgresql and configure remote access in ubuntu environment
【硬件架构的艺术】学习笔记(1)亚稳态的世界
License server system does not support this version of this feature
2042. 检查句子中的数字是否递增-力扣双百代码-设置前置数据
AlphaFold 如何实现 AI 在结构生物学中的全部潜力
G. Mountaineering Squad (violence & dfs)
idea removes spark logs
Lixia Action | Kyushu Yunzhang Jinnan: Open source is not a movement for a few people, popularization is the source









