1291. Sequential Digits
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
From: LeetCode
Link: 1291. Sequential Digits
Solution:
Ideas:
Generate every sequential-digit number by choosing its length and starting digit, then keep those within low, high.
Code:
c
#include <stdlib.h>
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sequentialDigits(int low, int high, int* returnSize) {
int* result = (int*)malloc(36 * sizeof(int));
*returnSize = 0;
for (int length = 2; length <= 9; length++) {
for (int start = 1; start + length - 1 <= 9; start++) {
int number = 0;
for (int digit = start; digit < start + length; digit++) {
number = number * 10 + digit;
}
if (number >= low && number <= high) {
result[(*returnSize)++] = number;
}
}
}
return result;
}