Arrays_Strings 压缩字符串 @CareerCup
2014-02-28 05:09
316 查看
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数组中
// 最后返回更新过的index
public 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);
}
}
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数组中
// 最后返回更新过的index
public 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);
}
}
相关文章推荐
- android 导航布局
- QNX多线程之Mutex
- 后端控制器之CommandController、 FormController(三)
- PAT 1060. Are They Equal (25) 只做出19分。。
- 游戏逻辑层在Lua中的内存泄漏与防范
- 游戏开发中内存泄露的一些防范措施
- 编译器技术简析(一)- Lexical Analysis之NFA-DFA
- 线程和进程
- What are the __init* and __exit* macros ?
- 常用的排序算法
- js原型链
- Oracle调优总结(经典实践 重要)
- STL中的map和hashmap数据结构
- 后端控制器之CommondController、 FormController(二)
- 几种常见容器的比较:vector,map,hashmap,list,set
- 后端控制器之CommondController、 FormController(一)
- 游戏中自动寻路通用算法
- 深度优先算法和广度优先算法
- new/delete和malloc/free的使用场合和区别
- 后端控制器之MultiActionController