forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
70 lines (55 loc) · 1.67 KB
/
main.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/// Source : https://leetcode.com/problems/string-compression/description/
/// Author : liuyubobobo
/// Time : 2017-10-28
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
int compress(vector<char>& chars) {
if(chars.size() <= 1)
return chars.size();
int start = 0;
int index = 0;
for(int i = start + 1 ; i <= chars.size() ; ){
if(i == chars.size() || chars[i] != chars[start]){
int len = i - start;
chars[index++] = chars[start];
if(len != 1){
string len_str = to_string(len);
for(char c: len_str)
chars[index++] = c;
}
start = i;
i = start + 1;
}
else
i ++;
}
return index;
}
};
int main() {
char str1[] = {'a','a','b','b','c','c','c'};
vector<char> vec1(str1, str1 + sizeof(str1)/sizeof(char));
int len1 = Solution().compress(vec1);
for(int i = 0 ; i < len1 ; i ++)
cout << vec1[i] << " ";
cout << endl;
// ---
char str2[] = {'a'};
vector<char> vec2(str2, str2 + sizeof(str2)/sizeof(char));
int len2 = Solution().compress(vec2);
for(int i = 0 ; i < len2 ; i ++)
cout << vec2[i] << " ";
cout << endl;
// ---
char str3[] = {'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b'};
vector<char> vec3(str3, str3 + sizeof(str3)/sizeof(char));
int len3 = Solution().compress(vec3);
for(int i = 0 ; i < len3 ; i ++)
cout << vec3[i] << " ";
cout << endl;
return 0;
}