当前位置:网站首页>Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
Sword finger offer notes: t57 - ii Continuous positive sequence with sum s
2022-07-27 11:48:00 【Ignorant little nine】
T57 - II. And for s Continuous positive sequence of
Enter a positive integer target , Output all and as target A sequence of continuous positive integers ( Contains at least two numbers ).
The numbers in the sequence are arranged from small to large , The different sequences are arranged in descending order according to the first number .
Example 1:
Input :target = 9
Output :[[2,3,4],[4,5]]
Example 2:
Input :target = 15
Output :[[1,2,3,4,5],[4,5,6],[7,8]]
Limit :
1 <= target <= 10^5
solution 1
Didn't do it
solution 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()][]);
}
}
Execution time :4 ms, In all Java Defeated in submission **56.28%** Users of
Memory consumption :36.4 MB, In all Java Defeated in submission **86.99%** Users of
Time complexity :O(target)O(target). Sliding window moves at most target/2 Time
Spatial complexity :O(1)O(1). Exclude the necessary storage result array , Just save the left and right pointers
author : Painter Dapeng
link :https://leetcode-cn.com/leetbook/read/illustrate-lcof/5017gs/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .
边栏推荐
- N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei
- 意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
- 暂用 Solo,博客选择困难
- LAN SDN hard core technology insider 23 looking forward to the future - RDMA (Part 1)
- LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
- Source code compilation and installation lamp
- Matlab draws Bode diagram with time delay system
- CH340模块无法识别/烧写不进的一种可能性
- Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
- Analysis of the use of JUC framework from runnable to callable to futuretask
猜你喜欢

Why is ack=seq+1 when TCP shakes hands three times

Detailed explanation of hash table
Synchronous use reference of the new version of data warehouse (for beginners)
![[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis](/img/54/dc232f737da99c7097c395a326e74f.png)
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis

iptables防火墙

Adobe audit prompts that the sampling rate of audio input does not match the output device - problem solving

【机器学习-白板推导系列】学习笔记---支持向量机和主成分分析法

Shell脚本文本三剑客之sed
![[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution](/img/cc/a27db6c5a380102029b85255df79c9.png)
[machine learning whiteboard derivation series] learning notes - probability graph model and exponential family distribution

82.(cesium之家)cesium点在3d模型上运动
随机推荐
Why is ack=seq+1 when TCP shakes hands three times
Difference quotient approximation of wechat quotient
LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
Some commonly used shortcut keys for MathType
Maker Hongmeng application development training 04
Principle of control system based on feedback rate
Stm32f10x -- C Language-1
Newton-Raphson迭代法
Codeforces round #664C
STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
The C programming language (2nd) -- Notes -- 1.10
LeetCode 01: T1. 两数之和 ; T1108. IP 地址无效化 ; T344. 反转字符串
Why choose smart TV?
Matlab draws Bode diagram with time delay system
局域网SDN硬核技术内幕 24 展望未来——RDMA(中)
Regular expression of shell programming (grep of shell script text three swordsmen)
基于反馈率的控制系统原理
zabbix自定义监控项
[special topic] summary of RMQ question brushing with ST table