LeetCode-1291. Sequential Digits

网友投稿 259 2022-08-29

LeetCode-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 = 300Output: [123,234]

Example 2:

Input: low = 1000, high = 13000Output: [1234,2345,3456,4567,5678,6789,12345]

Constraints:

​​10 <= low <= high <= 10^9​​

题解:

暴力枚举即可。

class Solution {public: vector sequentialDigits(int low, int high) { string s = "123456789"; vector res; for (int begin = 0; begin < 9; begin++) { for (int len = 1; len <= 9 - begin; len++) { string sub = s.substr(begin, len); int num = stoi(sub); if (num >= low && num <= high) { res.push_back(num); } } } sort(res.begin(), res.end()); return res; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-1261. Find Elements in a Contaminated Binary Tree
下一篇:央视网揭秘网红店营销套路,中脉抨击骗局虚假宣传,纠正行业风气!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~