Sequential Digits
Description
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high]
inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300 Output: [123,234]
Example 2:
Input: low = 1000, high = 13000 Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
10 <= low <= high <= 10^9
Solution(javascript)
/**
* @param {number} low
* @param {number} high
* @return {number[]}
*/
const sequentialDigits = function (low, high) {
const getCount = (num) => {
let count = 0
while (num > 0) {
count += 1
num = Math.floor(num / 10)
}
return count
}
const composeNum = (count, start) => {
let result = start
while (count > 1 && start < 9) {
result = result * 10 + start + 1
start += 1
count -= 1
}
if (count > 1) {
return 0
}
return result
}
const count1 = getCount(low)
const count2 = getCount(high)
const result = []
for (let i = count1; i <= count2; i++) {
for (let start = 1; start <= 8; start++) {
const num = composeNum(i, start)
if (num >= low && num <= high) {
result.push(num)
}
}
}
return result
}