英文原文
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
中文题目
我们定义「顺次数」为:每一位上的数字都比前一位上的数字大 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
通过代码
高赞题解
class Solution {
public:
vector<int> sequentialDigits(int low, int high) {
vector<int> t = {
12, 23, 34, 45, 56, 67, 78, 89,
123, 234, 345, 456, 567, 678, 789,
1234, 2345, 3456, 4567, 5678, 6789,
12345, 23456, 34567, 45678, 56789,
123456, 234567, 345678, 456789,
1234567, 2345678, 3456789,
12345678, 23456789,
123456789
};
int a = 0, b = t.size() - 1;
while(a < t.size() && t[a] < low)
a++;
while(b >=0 && t[b] > high)
b--;
if(a <= b)
return vector<int>(t.begin() + a, t.begin() + b + 1);
else
return vector<int>();
}
};
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
7377 | 14205 | 51.9% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|