简介
常规思路, 看了官方的.
code
class Solution {
public int compress(char[] chars) {
int anchor = 0, write = 0;
for(int read = 0; read < chars.length; read++){
if(read == chars.length - 1 || chars[read] != chars[read + 1]) {
chars[write++] = chars[read];
if(read > anchor) {
for(char c : ("" + (read - anchor + 1)).toCharArray()) { // 很巧妙, 使用这个操作, 将100 转为 "1", "0", "0"
chars[write++] = c;
}
}
anchor = read + 1;
}
}
return write;
}
}