k++;
if (array[j] == array[j + 1]) {
count++;
} else if (j != 0 && (array[j - 1] == array[j])) {
zipStr = zipStr + array[k - 1] + count;
count = 1;
}
if (j != 0 &&(array[j-1] != array[j])
&& (array[j] != array[j + 1])
) {
count = 1;
zipStr = zipStr + array[k-1] + count;
continue;
}
if (j == array.length - 2) {
zipStr = zipStr + array[k] + count;
}
}
// System.out.println(zipStr);
if (zipStr.length() >= iniString.length()) {
System.out.println(iniString);
} else {
System.out.println(zipStr);
}
}
/**
网友的高效解法
*/
public static String zipStr(String iniString){
if(iniString == null || iniString.length()==0) {
return null;
}
StringBuilder sb = new StringBuilder();
int len = iniString.length();
for(int i=0;i int count = 1;
while((i+1) count++;
i++;
}
if(count>=1){
sb.append(iniString.charAt(i));
sb.append(count);
}else{
sb.append(iniString.charAt(i));
}
}
if(sb.length() return sb.toString();
}
return iniString;
}
对字符串进行压缩