반응형
https://leetcode.com/problems/string-compression/description/
String Compression - LeetCode
Can you solve this real interview question? String Compression - Given an array of characters chars, compress it using the following algorithm: Begin with an empty string s. For each group of consecutive repeating characters in chars: * If the group's leng
leetcode.com
투포인터를 두고 풀었습니다.
하나의 포인터는 chars를 순차적으로 탐색하면서 연속된 동일 문자 개수를 구해주고,
또 다른 포인터는 새로운 문자가 나오면 chars에 압축 내역을 반영해주었습니다.
class Solution {
public:
int compress(vector<char>& chars) {
chars.push_back('?'); // dummy
char ch = chars[0];
int cnt = 1;
int j = 0;
for(int i=1; i<chars.size(); i++) {
if(chars[i] != ch) {
chars[j++]= ch;
if(cnt >= 10) {
string cntStr = to_string(cnt);
for(char cntCh : cntStr) {
chars[j++] = cntCh;
}
} else if(cnt > 1) {
chars[j++] = cnt + '0';
}
cnt = 0;
ch = chars[i];
}
cnt++;
}
return j;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 2444. Count Subarrays With Fixed Bounds (0) | 2023.03.04 |
---|---|
LeetCode 28. Find the Index of the First Occurrence in a String (0) | 2023.03.03 |
LeetCode 912. Sort an Array (0) | 2023.03.01 |
LeetCode 652. Find Duplicate Subtrees (0) | 2023.02.28 |
LeetCode 427. Construct Quad Tree (0) | 2023.02.27 |