Skip to content

Commit 09dd74a

Browse files
authored
Merge pull request gzc426#131 from MMzhe/patch-4
Create sourcema.md
2 parents 87e0cbd + c5aef92 commit 09dd74a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

2018.11.29-leetcode443/sourcema.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# LeetCode 443
2+
class Solution {
3+
public int compress(char[] chars) {
4+
if (chars == null || chars.length == 0) {
5+
return 0;
6+
}
7+
StringBuilder sb = new StringBuilder();
8+
int num=1;
9+
for (int i = 0,j=0; i <chars.length ; ) {
10+
j=i+1;
11+
while(j<chars.length&&chars[i] == chars[j]) {
12+
j++;
13+
num++;
14+
}
15+
if (num == 1) {
16+
sb.append(chars[i]);
17+
} else {
18+
sb.append(chars[i]).append(num);
19+
num=1;
20+
}
21+
i=j;
22+
}
23+
String s = sb.toString();
24+
for (int i = 0; i <chars.length ; i++) {
25+
chars[i] = i>=s.length()?0:s.charAt(i);
26+
}
27+
return sb.toString().length();
28+
}
29+
}

0 commit comments

Comments
 (0)