当前位置:网站首页>【剑指 Offer 】57 - II. 和为s的连续正数序列
【剑指 Offer 】57 - II. 和为s的连续正数序列
2022-07-03 16:29:00 【LuZhouShiLi】
剑指 Offer 57 - II. 和为s的连续正数序列
题目
输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
思路
- 当窗口的和小于 target 的时候,窗口的和需要增加,所以要扩大窗口,窗口的右边界向右移动
- 当窗口的和大于 target 的时候,窗口的和需要减少,所以要缩小窗口,窗口的左边界向右移动
- 当窗口的和恰好等于 target 的时候,我们需要记录此时的结果。设此时的窗口为 [i, j)[i,j),那么我们已经找到了一个 ii 开头的序列,也是唯一一个 ii 开头的序列,接下来需要找 i+1i+1 开头的序列,所以窗口的左边界要向右移动
代码
class Solution {
public:
vector<vector<int>> findContinuousSequence(int target) {
int i = 1;// 滑动窗口的左边界
int j = 1; // 滑动窗口的右边界
int sum = 0; // 滑动窗口中数字的和
vector<vector<int>> res;// 存储若干滑动窗口的结果
while(i <= target / 2)
{
// 扩大滑动窗口
if(sum < target)
{
// 右边界向右移动
sum += j;
j++;
}
else if(sum > target)
{
// 缩小滑动窗口
sum -= i;
i++;
}
else{
// 记录结果
vector<int> arr;
// 将滑动窗口中的数字全部写入数组arr中 左闭右开区间
for(int k = i; k < j; k++)
{
arr.push_back(k);
}
res.push_back(arr);
// 写入之后 继续寻找下一组结果 滑动窗口向右移动 减去i即可
sum -= i;
i++;
}
}
return res;
}
};
边栏推荐
- Golang 匿名函数使用
- 线程池执行定时任务
- [combinatorics] combinatorial identity (sum of variable upper terms 1 combinatorial identity | summary of three combinatorial identity proof methods | proof of sum of variable upper terms 1 combinator
- From the 18th line to the first line, the new story of the network security industry
- Caching mechanism of Hibernate / session level caching mechanism
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- [list to map] collectors Tomap syntax sharing (case practice)
- Unity项目优化案例一
- Using optimistic lock and pessimistic lock in MySQL to realize distributed lock
猜你喜欢

Explore Cassandra's decentralized distributed architecture

线程池执行定时任务

探索Cassandra的去中心化分布式架构

How to use AAB to APK and APK to AAB of Google play apps on the shelves

爱可可AI前沿推介(7.3)

Data driving of appium framework for mobile terminal automated testing

Deep understanding of grouping sets statements in SQL

Unreal_ Datatable implements ID self increment and sets rowname

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
随机推荐
Hibernate的缓存机制/会话级缓存机制
Chinese translation of Tagore's floating birds (1~10)
斑马识别成狗,AI犯错的原因被斯坦福找到了
近视:摘镜or配镜?这些问题必须先了解清楚
Mixlab编辑团队招募队友啦~~
0214-27100 a day with little fluctuation
拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
"Everyday Mathematics" serial 56: February 25
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
First knowledge of database
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
Learn from me about the enterprise flutter project: simplified framework demo reference
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
"The NTP socket is in use, exiting" appears when ntpdate synchronizes the time
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
Mysql 将逗号隔开的属性字段数据由列转行
Pychart error updating package list: connect timed out
PHP CI(CodeIgniter)log级别设置
Record a jar package conflict resolution process