Skip to content

Commit 94dd8e5

Browse files
authored
Merge pull request gzc426#12 from beddingearly/beddingearly-patch-7
Create Tony the Cyclist.md
2 parents 0eba83d + 1da2bf1 commit 94dd8e5

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
```
2+
class Solution {
3+
public int compress(char[] chars) {
4+
char base = chars[0];
5+
int count = 1;
6+
int pos = 0;
7+
for (int i = 1; i < chars.length; i++){
8+
if (base != chars[i]){
9+
base = chars[i];
10+
if (count == 1){
11+
chars[++pos] = base;
12+
continue;
13+
}
14+
// 先计数再写入下一个值
15+
for (int j = 0; j < String.valueOf(count).length(); j++){
16+
chars[++pos] = String.valueOf(count).charAt(j);
17+
}
18+
count = 1;
19+
chars[++pos] = base;
20+
}
21+
else {// base == chars[i]
22+
count ++;
23+
if (i == chars.length-1){
24+
if (count == 1){
25+
continue;
26+
}
27+
for (int j = 0; j < String.valueOf(count).length(); j++){
28+
chars[++pos] = String.valueOf(count).charAt(j);
29+
}
30+
31+
}
32+
33+
}
34+
35+
}
36+
pos++;
37+
//splitArray(chars, pos);
38+
if (pos == 0)
39+
return 1;
40+
return pos;
41+
}
42+
}
43+
```

0 commit comments

Comments
 (0)