当前位置:网站首页>剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列
剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列
2022-07-27 10:58:00 【无知小九】
T57 - II. 和为 s 的连续正数序列
输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数)。
序列内的数字由小到大排列,不同序列按照首个数字从小到大排列。
示例 1:
输入:target = 9
输出:[[2,3,4],[4,5]]
示例 2:
输入:target = 15
输出:[[1,2,3,4,5],[4,5,6],[7,8]]
限制:
1 <= target <= 10^5
解法 1
没做出来
解法 2
class Solution {
public int[][] findContinuousSequence(int target) {
int left = 1;
int right = 2;
List<int[]> res = new ArrayList<>();
while (left < right) {
int sum = (left + right) * (right - left + 1) / 2;
if (sum == target){
int[] arr = new int[right - left + 1];
for (int k = left; k <= right; k++) {
arr[k - left] = k;
}
res.add(arr);
left++;
}
else if (sum < target) {
right++;
}
else {
left++;
}
}
return res.toArray(new int[res.size()][]);
}
}
执行用时:4 ms, 在所有 Java 提交中击败了**56.28%**的用户
内存消耗:36.4 MB, 在所有 Java 提交中击败了**86.99%**的用户
时间复杂度:O(target)O(target)。滑动窗口最多移动 target/2 次
空间复杂度:O(1)O(1)。排除必要的存储结果数组之外,只需要保存左右指针
作者:画手大鹏
链接:https://leetcode-cn.com/leetbook/read/illustrate-lcof/5017gs/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- 82.(cesium之家)cesium点在3d模型上运动
- 求组合数 AcWing 885. 求组合数 I
- Digital triangle model acwing 1027. Grid retrieval
- Summary of C language knowledge involved in learning STM32F103 (link only)
- [special topic] summary of RMQ question brushing with ST table
- Kepserver configuration
- 数据包传输:应用层-内核-硬件
- 高斯消元 AcWing 884. 高斯消元解异或线性方程组
- Installation and use of GTEST and gmock
- 求组合数 AcWing 887. 求组合数 III
猜你喜欢

Longest ascending subsequence model acwing 1010. Interceptor missile

Redis simple to use

Longest ascending subsequence model acwing 1012. Sister Cities

数字三角形模型 AcWing 1018. 最低通行费

82. (cesium home) cesium points move on 3D models

2022 Niuke multi school (3) j.journey

Analysis of distributed database and cache double write consistency scheme (Reprint)

Longest ascending subsequence model acwing 482. Chorus formation

When std:: bind meets this

Gaussian elimination acwing 884. Gaussian elimination for solving XOR linear equations
随机推荐
Instructions for mock platform
The C programming language (2nd) -- Notes -- 1.8
求组合数 AcWing 888. 求组合数 IV
SQL statement learning and the use of pymysql
Lazy loading of lists and pictures
LeetCode-SQL练习题总结(MySQL实现)
VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景
Ask the big guys, is there transaction control for using flick sink data to MySQL? If at a checkpoint
The longest ascending subsequence model acwing 1017. The glider wing of the strange thief Kidd
局域网SDN技术硬核内幕 11 云网融合CP的关键——层次化端口绑定
开源flink有写holo的Datastream connector或者flink sql conn
Luogu p1441 weight weighing
数字三角形模型 AcWing 1027. 方格取数
Memory search acwing 901. Skiing
The longest ascending subsequence model acwing 1016. The sum of the largest ascending subsequence
ZABBIX custom monitoring items
Digital triangle model acwing 1027. Grid retrieval
Error while unzipping file in win10: unable to create symbolic link. You may need to run WinRAR with administrator privileges. The client does not have the required privileges
When std:: bind meets this
Maker Hongmeng application development training notes 03