Notice
Recent Posts
Recent Comments
Link
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

hwooo

LeetCode (C/C++) 1291. Sequential Digits 본문

Study/Algorithm

LeetCode (C/C++) 1291. Sequential Digits

hwooo 2024. 11. 20. 20:15

https://leetcode.com/problems/sequential-digits/description/

 


풀이

주어진 숫자들을 모두 문자열 형태로 바꿔 숫자의 길이를 쟀다.

해당 길이에서 나올 수 있는 연속된 숫자의 모든 경우의 수를 구한 후, low < num < high인 경우에만 저장해 반환한다.


코드

class Solution {
public:
    vector<int> sequentialDigits(int low, int high) {
        string l = to_string(low);
        string h = to_string(high);

        vector<int> seq;

        // 주어진 숫자 사이의 길이 구함
        for (int i = l.size(); i <= h.size(); i++) {

            // 해당 길이의 연속된 숫자 구하기
            for (int j = 1; j <= 10 - i; j++) {
                string s = "";
                char start = j + '0';
                for (int k = 0; k < i; k++) 
                    s += start + k;

                // 해당 숫자가 low < num < high인지 확인 후 저장
                int num = stoi(s);
                if (low <= num && num <= high)
                    seq.push_back(num);
            }
        }
        return seq;
    }
};