-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path443.cpp
More file actions
executable file
·45 lines (44 loc) · 1.2 KB
/
443.cpp
File metadata and controls
executable file
·45 lines (44 loc) · 1.2 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Solution {
public:
int compress(vector<char>& chars) {
if (chars.size() <= 1){
return chars.size();
}
int last = chars[0];
int lastCount = 1;
int index = 0;
for (int i = 1; i < chars.size(); i++){
if (chars[i] == last){
lastCount++;
}else{
if (lastCount == 1){
chars[index] = last;
index += 1;
}else{
chars[index] = last;
int start = 1;
for (auto c : to_string(lastCount)){
chars[index + start] = c;
start ++;
}
index += start;
}
lastCount = 1;
last = chars[i];
}
}
if (lastCount == 1){
chars[index] = last;
index += 1;
}else{
chars[index] = last;
int start = 1;
for (auto c : to_string(lastCount)){
chars[index + start] = c;
start ++;
}
index += start;
}
return index;
}
};