Skip to content

Commit 178b211

Browse files
authored
Create 湛江甲鸟.md
1 parent 81321b1 commit 178b211

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
```
2+
public int compress(char[] chars) {
3+
int index = 0;
4+
if (chars.length < 2) {
5+
return chars.length;
6+
}
7+
8+
for (int i = 0; i < chars.length; ) {
9+
int times = 1;
10+
chars[index++] = chars[i++];
11+
while (i < chars.length && chars[i] == chars[i - 1]) {
12+
times++;
13+
i++;
14+
}
15+
if (times > 1) {
16+
char[] chs = String.valueOf(times).toCharArray();
17+
for (int j = 0; j < chs.length; j++) {
18+
chars[index++] = chs[j];
19+
}
20+
}
21+
}
22+
return index;
23+
}

0 commit comments

Comments
 (0)