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
- Arrays_Strings 压缩字符串 @CareerCup
- Arrays_Strings 翻转字符串 @CareerCup
- Arrays_Strings 判断字符串中的字符是否唯一@CareerCup
- Arrays_Strings 判断两个字符串是否有相同的字符 @CareerCup
- Arrays_Strings 把字符串中所有的空格替换为%20 @CareerCup
- Arrays_Strings 90度顺时针逆时针旋转方形矩阵 @CareerCup
- Arrays_Strings 把一个矩阵置为0 @CareerCup
- Arrays_Strings 利用isSubstring检查是否子串 @CareerCup
- CareerCup之1.2C风格字符串翻转
- CareerCup之1.3字符串去重
- CareerCup之1.8 字符串移位包含问题
- Hard 在字符串S中查找T中的字符串 @CareerCup
- 压缩字符串
- 字符串压缩
- 字符串压缩
- 字符串压缩
- 字符串压缩
- 压缩字符串
- 游戏逻辑层在Lua中的内存泄漏与防范
- PAT 1060. Are They Equal (25) 只做出19分。。
- 后端控制器之CommandController、 FormController(三)
- QNX多线程之Mutex
- LeetCode 题解(25): Remove Nth Node From End of List
- Arrays_Strings 压缩字符串 @CareerCup
- LeetCode 题解(26): Add Two Numbers
- aix mount
- 清理php中的bom
- Ubuntu12 解决BadName (named color or font does not exist)
- Opencv(1)
- opencv(2)
- Arrays_Strings 90度顺时针逆时针旋转方形矩阵 @CareerCup
- Android 中用内核模块实现系统调用(Implement system call by kernel module in Android)