Skip to content

Commit 27ad47e

Browse files
authored
Merge pull request gzc426#128 from lyfYangTing/master
压缩字符串
2 parents b698c23 + c18c29e commit 27ad47e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

2018.11.29-leetcode443/杨.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
```
2+
public int compress(char[] chars) {
3+
char data = chars[0];
4+
int index = 0;
5+
int length = -1;
6+
if(chars.length == 1){
7+
return 1;
8+
}else {
9+
for (int i = 1;i< chars.length;i++){
10+
if(chars[i]!=data || i == chars.length-1){
11+
chars[++length]=data;
12+
if(i == chars.length -1 && chars[i]==data){
13+
i++;
14+
}
15+
if(i-index>1){
16+
char[] num = String.valueOf((i-index)).toCharArray();
17+
for (int j=0;j<num.length;j++){
18+
chars[++length] = num[j];
19+
}
20+
}
21+
if(i < chars.length - 1){
22+
data = chars[i];
23+
index = i;
24+
}
25+
if(i == chars.length -1 && chars[i]!=data){
26+
chars[++length]=chars[i];
27+
}
28+
}
29+
}
30+
}
31+
return length + 1;
32+
}
33+
34+
```

0 commit comments

Comments
 (0)