leetcode 443 压缩字符串

简介

常规思路, 看了官方的.

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;
    }
}
Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
原文地址:https://www.cnblogs.com/eat-too-much/p/14878249.html