Skip to content

Commit c7a319d

Browse files
authored
Merge pull request gzc426#161 from ZongYangL/master
2018-11-29
2 parents f852924 + 0495d7c commit c7a319d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2018.11.28

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
/**
3+
* Created by zongjianwei
4+
* DATE: 21:32 2018/11/28
5+
* Desc: 压缩字符数组
6+
*/
7+
public class StringCompression {
8+
public int compress(char[] chars){
9+
int m = 0;
10+
int n = 0;
11+
for(int i=0;i<chars.length;i++){
12+
if(i+1 == chars.length || chars[i+1]!=chars[i]){
13+
chars[m++] = chars[n];
14+
if(i>n){
15+
for(int j=0;j<(""+(i-n+1)).toCharArray().length;j++){
16+
chars[m++] = chars[j];
17+
}
18+
}
19+
n = i +1;
20+
}
21+
}
22+
return m;
23+
}
24+
25+
public static void main(String[] args) {
26+
char[] chars = {'a','a','b','b','c','c','c'};
27+
StringCompression sc = new StringCompression();
28+
int length = sc.compress(chars);
29+
System.out.println(length);
30+
31+
}
32+
}

0 commit comments

Comments
 (0)