Skip to content

Commit e700164

Browse files
Merge pull request gzc426#7 from EngineerZhang/EngineerZhang-patch-4
Create 神秘的火柴人.md
2 parents 1b62fa7 + 0492c68 commit e700164

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
...

0 commit comments

Comments
 (0)