/*
我们定义「顺次数」为:每一位上的数字都比前一位上的数字大 1 的整数。
请你返回由 [low, high] 范围内所有顺次数组成的 有序 列表(从小到大排序)。
示例 1:
输出:low = 100, high = 300
输出:[123,234]
示例 2:
输出:low = 1000, high = 13000
输出:[1234,2345,3456,4567,5678,6789,12345]
提示:
10 <= low <= high <= 10^9
*/
// 获取「顺次数」
function getReNum(num, len) {
// console.log('num, len: ', num, len);
let res = 0
while (len >= 0) {
res += num * (10 ** len)
num++
len--
}
return res
}
function getArr(lowFirstNum, lowLen, low, highLen, high) {
let res = []
let i = lowFirstNum
while (lowLen <= highLen) {
for (i; i<=9-lowLen; i++) {
const num = getReNum(i, lowLen)
if (num >= low && num <= high) {
res.push(num)
}
}
lowLen++
i = 1
}
return res
}
function getNum(low, high) {
if (high < low || typeof low !== 'number' || typeof high !== 'number') return '没有「顺次数」'
const lowLen = String(low).length
const highLen = String(high).length
const lowFirstNum = Number(Math.floor(low / (10 ** (lowLen-1))))
const highFirstNum = Number(Math.floor(high / (10 ** (highLen-1))))
let minNum = getReNum(lowFirstNum, lowLen-1)
let maxNum = getReNum(highFirstNum, highLen-1)
// 相等:直接判断是否是「顺次数」
if (high === low) {
return minNum === low ? [low] : '没有「顺次数」'
}
// 仅有一个「顺次数」
if (minNum === maxNum) return [minNum]
// 这里获取了合法的最小值、最大值
let res = []
// console.log('[minNum, maxNum]: ', [minNum, maxNum]);
res = getArr(lowFirstNum, lowLen-1, low, highLen-1, high)
return res
}
console.log('-------result: ', getNum(5555, 12345));
05-29
440
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-16
544
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交