Skip to content

Commit 7c63e5b

Browse files
authored
Merge pull request gzc426#152 from 593903762/master
1
2 parents 6f66a0b + 25dc722 commit 7c63e5b

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2018.11.29-leetcode443/Mr.Q.md

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

0 commit comments

Comments
 (0)