hwooo
LeetCode (C/C++) 1291. Sequential Digits 본문
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;
}
};
'Study > Algorithm' 카테고리의 다른 글
프로그래머스 (C/C++) 86971 : 전력망을 둘로 나누기 (0) | 2024.11.25 |
---|---|
LeetCode (C/C++) 34. Find First and Last Position of Element in Sorted Array (0) | 2024.11.22 |
프로그래머스 (C/C++) 64065 : 튜플 (0) | 2024.11.20 |
LeetCode (C/C++) 581. Shortest Unsorted Continuous Subarray (0) | 2024.11.07 |
프로그래머스 (C/C++) 118667 : 두 큐 합 같게 만들기 (0) | 2024.11.06 |