java實現字元壓縮演算法
阿新 • • 發佈:2020-10-10
public class CompressionAlgorithm1 {
/**
* @param args
* 實現簡易字串壓縮演算法:一個長度最大為128的字串,
* 由字母a-z或者A-Z組成,將其中連續出現2次以上(含2次)的字母轉換為字母和出現次數,
* 以達到壓縮目的。
* 輸入:AAAABBBB
* 輸出:A4B4
*/
public static String outPutStr(String InputStr){
//首先先獲取字串的第一個字元
int i=0;
StringBuffer outputStr=new StringBuffer();;
int length=InputStr.length();
//依次獲取字元
char a=InputStr.charAt(i);
int number=1;
//然後判斷這個字元之後是否還有相同的字元
for(int j=i+1;j<length;j++){
char b=InputStr.charAt(j);
//判斷 a和b是否相等
if(a==b){
number++;
}else{
//跳出本次迴圈,進入下一個迴圈
break;
}
}
//將字元進行壓縮
if(number>1){
//如果number大於1那就表明可以壓縮
outputStr.append(a+new Integer(number).toString());
outputStr.append(a);
}
//將獲取number新增進i
i=i+number;
}
return outputStr.toString();
}
public static void main(String[] args) {
String inputStr=“ABccTTThhKKKL”;
System.out.println(outPutStr(inputStr));
}
}
U2FsdGVkX1/z87K/sb+CqRTYW++d2FraL/WhthKVu9nwpHXrcQnONCvQyCjCNOg4
0o/IIj+5BauG5Q4A4HuWiV9EKVZgBZUqtl/X6vEzk0OA9h/nOEY8gPa0Hn/hgJ4J
htvcKXF0jxu/iVAQmrE2x06uKlMgvyChO33+GRZk6tISSFen1j1JAki+TBen8eQ1
PRtKvZKtjlfDpje7UlD3H5UgcKzFQlcoxt72CM0ljOf3CRD6UAh3Pt6EuAaX26Fz
9rhlDAHLTVpXrQXZglRW0EVJAaMNCFiQatdx0ZXZGaNJuWUQFvv/ptECi5V9yC/L
xel0PcPrZsvwtXuCjOYK2Maa+tZUes1hXgZgTjtb7cA63I1i3mQojjo7OXRbv15/
UuFs7oYV8/qhfxboshq5Dym4K1Ohbh/MKenVz7StsJkUuhuSmnk+dEo+860gP+Xs
G170Xjmd7UEY6TOCz9nWAC5It9ati8TPk3RHx0wvRMydQuRqFW82N3zg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