1.5---字符串压缩(CC150)

import java.util.*;

public class Zipper {
public String zipString(String str) {
// write code here
if(str.length() == 0 ) return str;
int flag = 0;
int num = 1;
StringBuffer sb = new StringBuffer();
char tmp = str.charAt(0);
for(int i = 1; i < str.length(); i++){
if(str.charAt(i) == tmp){
num++;
flag = 1;
}
else{
sb.append(tmp);
sb.append(num);
tmp = str.charAt(i);
num = 1;
}
}
sb.append(tmp);
sb.append(num);
if(flag == 0 || sb.length() >= str.length()) return str;
else return sb.toString();
}
}
上一篇:pandas 之 多层索引


下一篇:如何获取tinyeditor编辑器里面的值