这篇文章会收录到 : 算法通关村第十二关-黄金挑战字符串冲刺题-CSDN博客
压缩字符串
描述 :
给你一个字符数组 chars ,请使用下述算法压缩:
从一个空字符串 s 开始。对于 chars 中的每组 连续重复字符 :
- 如果这一组长度为 1,则将字符追加到s中。
- 否则,需要向 s追加字符,后跟这一组的长度。
压缩后得到的字符串 s 不应该直接返回 ,需要转储到字符数组 chars 中。需要注意的是,如果组长度为 10 或 10 以上,则在 chars 数组中会被拆分为多个字符。
请在 修改完输入数组后 ,返回该数组的新长度。
题目 :
LeetCode 443.压缩字符串 :
443. 压缩字符串

分析 :
为了让大家更好的理解 , 下面有个视频 :
B站 :LeetCode力扣 443. 压缩字符串 String Compression_哔哩哔哩_bilibili
解析 :
class Solution {
    public int compress(char[] chars) {
        int cur = 0;
        int index = 0;
        while(index < chars.length){
            char c = chars[index];
            int count = 0;
            while(index < chars.length && chars[index] == c){
                index++;
                count++;
            }
            chars[cur++] = c;
            if(count != 1){
                for(char arr : String.valueOf(count).toCharArray()){
                    chars[cur++] = arr;
                }
            }
        }
        return cur;
    }
}这期就到这里 , 下期见!












![2023-简单点-机器学习中常用的特殊函数,激活函数[sigmoid tanh ]](https://img-blog.csdnimg.cn/cf2756c94616431bb6c1e7aef3ae82f1.png)






