File tree Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Expand file tree Collapse file tree 1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ ...
2
+ class Solution {
3
+ public:
4
+ int compress(vector<char >& chars) {
5
+ int size = chars.size();
6
+ int ret = 0;
7
+
8
+ int num = 1;
9
+ int cur = 0;
10
+ while(cur+1<size)
11
+ {
12
+ if(chars[ cur] == chars[ cur+1] )
13
+ num++;
14
+ else
15
+ {
16
+ chars[ ret] = chars[ cur] ;
17
+ ret++;
18
+ if(num > 1)
19
+ {
20
+ stack<char > s;
21
+ while(num != 0)
22
+ {
23
+ s.push(num%10+'0');
24
+ num /= 10;
25
+ }
26
+
27
+ while(!s.empty())
28
+ {
29
+ chars[ ret++] = s.top();
30
+ s.pop();
31
+ }
32
+
33
+ }
34
+ num = 1;
35
+ }
36
+
37
+ cur++;
38
+ }
39
+
40
+ chars[ ret++] = chars[ cur] ;
41
+ if(num > 1)
42
+ {
43
+ stack<char > s;
44
+ while(num != 0)
45
+ {
46
+ s.push(num%10+'0');
47
+ num /= 10;
48
+ }
49
+
50
+ while(!s.empty())
51
+ {
52
+ chars[ret++] = s.top();
53
+ s.pop();
54
+ }
55
+
56
+ }
57
+
58
+
59
+ chars.resize(ret);
60
+ return ret;
61
+ }
62
+ };
63
+ ...
You can’t perform that action at this time.
0 commit comments