-
Notifications
You must be signed in to change notification settings - Fork 344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
顺次数-1291 #116
Comments
请问递归和回溯体现在了哪里呢 |
function f(low, high) {
} |
/**
* @param {number} low
* @param {number} high
* @return {number[]}
*/
var sequentialDigits = function(low, high) {
let res = []
let lowLen = low.toString().length
let highLen = high.toString().length
for(let i=lowLen;i<=highLen;i++){
for(let j=1;j<=10-i;j++){
let str = ''
let num = j
str += num
let k = i-1
while(k--){
num++
str += num
}
let ans = parseInt(str)
if(ans>=low && ans<=high){
res.push(ans)
}
}
}
return res
}; |
var sequentialDigits = function(low, high) {
}; |
|
|
|
我们定义「顺次数」为:每一位上的数字都比前一位上的数字大 1 的整数。
请你返回由 [low, high] 范围内所有顺次数组成的 有序 列表(从小到大排序)。
提示:
10 <= low <= high <= 10^9
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sequential-digits
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
先通过
low
和high
之间的长度对比,得到总共有几种数字长度。然后循环这些数字长度
len
作为目标,1
作为最小起点,10 - len
作为最大的起点,不断尝试构造出长度为len
的顺位数字即可。The text was updated successfully, but these errors were encountered: