File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ ```
You can’t perform that action at this time.
0 commit comments