Arrays_Strings 压缩字符串 @CareerCup

来源:互联网 发布:淘宝助理天猫版 编辑:程序博客网 时间:2024/04/28 07:05

Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the “compressed” string would not become smaller than the original string, your method should return the original string.


思路:

基本上处理字符串的问题都不好用String,主要是出于效率考虑,而且String在递归传值时也不好用。

所以一律用StringBuilder或者StringBuffer。

思路很简单,就是每个字符和它的前一个比较,同时维护一个count变量,记录重复了多少次。


假如不让用StringBuilder,则可以用一个char array来代替StringBuilder


package Arrays_Strings;public class S1_5 {// 计算压缩过后的字符串长度,若是比没压过的还大,则果然返回没压过的public static int countCompression(String str) {if(str==null || str.isEmpty()) {return 0;}char last = str.charAt(0);int size = 0;// compress后的字符长度int count = 1;// 有多少个重复字符for(int i=1; i<str.length(); i++) {if(str.charAt(i) == last) {// 与之前字符相同count++;} else{// 与之前字符不同last = str.charAt(i);// 1为字符长度,然后还要加上count的字符串表示长度size += 1 + String.valueOf(count).length();count = 1;// 重置count为1}}size += 1 + String.valueOf(count).length();return size;}// 直接用String操作不好public static String compressBad(String str) {int size = countCompression(str);if(str.length() <= size) {return str;}String mystr = "";char last = str.charAt(0);int count = 1;for(int i=1; i<str.length(); i++) {if(str.charAt(i) == last) {count++;} else{mystr += last + "" + count;last = str.charAt(i);count = 1;}}return mystr + last + count;}// 应该改用StringBuilder操作public static String compressBetter(String str) {int size = countCompression(str);if(str.length() <= size) {return str;}StringBuilder sb = new StringBuilder();char last = str.charAt(0);int count = 1;for(int i=1; i<str.length(); i++) {if(str.charAt(i) == last) {count++;} else{sb.append(last);sb.append(count);last = str.charAt(i);count = 1;}}sb.append(last);sb.append(count);return sb.toString();}// 不用StringBuilder而用array的方法public static String compressAlternate(String str) {int size = countCompression(str);if(str.length() <= size) {return str;}char[] array = new char[size];int index = 0;char last = str.charAt(0);int count = 1;for(int i=1; i<str.length(); i++) {if(str.charAt(i) == last) {count++;} else{index = setChar(array, last, index, count);last = str.charAt(i);count = 1;}}index = setChar(array, last, index, count);return String.valueOf(array);}// index:标记当前位置,把字符c和有几个c(count)添加到array数组中// 最后返回更新过的indexpublic static int setChar(char[] array, char c, int index, int count) {array[index] = c;index++;char[] cnt = String.valueOf(count).toCharArray();for(char x : cnt) {array[index] = x;index++;}return index;}public static void main(String[] args) {String str = "abbccccccde";int c = countCompression(str);System.out.println(c);String str2 = compressAlternate(str);String t = compressBetter(str);System.out.println("Compression: " + t);System.out.println("Old String (len = " + str.length() + "): " + str);System.out.println("New String (len = " + str2.length() + "): " + str2);System.out.println("Potential Compression: " + c);}}



0 0