Skip to content

Commit 5f37829

Browse files
authored
Create Ostrichcrab.md
1 parent a09bcc7 commit 5f37829

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

2018.11.29-leetcode443/Ostrichcrab.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
```
2+
class Solution {
3+
public:
4+
int compress(vector<char>& chars) {
5+
int len = chars.size();
6+
if(len == 1) return chars.size();
7+
chars.push_back(' ');//统一一下
8+
len++;
9+
int cnt = 1;
10+
char pre = ' ';
11+
pre = chars[0];
12+
for(int i = 1; i < len; i++){
13+
if(chars[i] == pre){
14+
chars.erase(chars.begin()+i);
15+
i--;
16+
len--;
17+
cnt++;
18+
} else if(cnt>1){
19+
stringstream ss;
20+
string s;
21+
ss<<cnt;
22+
ss>>s;
23+
pre=chars[i];
24+
for(int j = 0; j < s.size(); j++){
25+
chars.insert(chars.begin()+i,s[j]);
26+
i++;
27+
len++;
28+
}
29+
cnt=1;
30+
}
31+
pre = chars[i];
32+
}
33+
chars.pop_back();//最后去除
34+
return chars.size();
35+
}
36+
};
37+
```

0 commit comments

Comments
 (0)