import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param param string字符串
* @return string字符串
*/
public String compressString (String param) {
// write code here
StringBuffer sb = new StringBuffer();
int left = 0, right = param.length();
while (left < right){
char c = param.charAt(left);
int count = 0;
while (left < right && param.charAt(left) == c){
left++;
count++;
}
sb.append(c);
if (count > 1){
sb.append(count);
}
}
return sb.toString();
}
}
使用双指针

该博客探讨了如何使用Java实现字符串压缩算法,通过双指针技术有效地减少重复字符的存储空间。示例代码中展示了如何遍历字符串并计算连续相同字符的数量,然后将其附加到结果缓冲区中。
1856

被折叠的 条评论
为什么被折叠?



