Skip to content

Commit da01a07

Browse files
authored
Merge pull request gzc426#6 from gityjx/gityjx-patch-4
Create halfofwater.md
2 parents 101ec62 + 666f446 commit da01a07

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

2018.11.29-leetcode443/halfofwater.md

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

0 commit comments

Comments
 (0)