File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ ## Leetcode 443
2
+
3
+ ```
4
+ class Solution {
5
+ public:
6
+ int compress(vector<char>& chars) {
7
+
8
+ if (chars.size() <= 1) return chars.size();
9
+
10
+ vector<char>::iterator it = chars.begin();
11
+ ++it;
12
+ int count = 1;
13
+ char tmp = chars[0];
14
+
15
+ char ccc = *(chars.end() - 1);
16
+ while (it < chars.end())
17
+ {
18
+ if (*it == tmp)
19
+ {
20
+
21
+ ++count;
22
+ if (it == chars.end() - 1) {
23
+ if (count != 1) {
24
+
25
+ string s = to_string(count);
26
+ for (char c : s) {
27
+ it = chars.insert(it, c);
28
+ ++it;
29
+ }
30
+ it = chars.erase(it);
31
+ }
32
+ //++it;
33
+ }
34
+ else {
35
+ it = chars.erase(it);
36
+ }
37
+ }
38
+ else
39
+ {
40
+ if (count != 1)
41
+ {
42
+ string s = to_string(count);
43
+ for (char c : s)
44
+ {
45
+ it = chars.insert(it, c);
46
+ ++it;
47
+ }
48
+ }
49
+
50
+ tmp = *it;
51
+ count = 1;
52
+ ++it;
53
+ }
54
+ }
55
+ return chars.size();
56
+ }
57
+ };
58
+
59
+ ```
You can’t perform that action at this time.
0 commit comments