-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
40 lines (35 loc) · 971 Bytes
/
main.cpp
File metadata and controls
40 lines (35 loc) · 971 Bytes
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
#include <iostream>
#include <vector>
#include <string>
int compress(std::vector<char>& chars);
void write(int sum, int& w, std::vector<char>& chars, char c);
int main() {
std::vector<char> vec {'a', 'a', 'a', 'b', 'b'};
std::cout << compress(vec) << std::endl;
return 0;
}
int compress(std::vector<char>& chars) {
int l = 0; // left
int w = 0; // write
int r = 0; // right
for (r = 0; r <= chars.size(); r++) {
if (r == chars.size()) {
int sum = r - l;
write(sum, w, chars, chars[l]);
} else if (chars[l] != chars[r]) {
int sum = r - l;
write(sum, w, chars, chars[l]);
l = r;
}
}
return w;
}
void write(int sum, int& w, std::vector<char>& chars, char c) {
chars[w++] = c;
if (sum >= 10) {
chars[w++] = '0' + (sum / 10);
chars[w++] = '0' + (sum % 10);
} else if (sum > 1) {
chars[w++] = '0' + sum;
}
}