leet-code/src/main/java/leetcode/editor/cn/StringCompression.java
2021-08-31 11:32:49 +08:00

99 lines
2.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//给你一个字符数组 chars ,请使用下述算法压缩:
//
// 从一个空字符串 s 开始。对于 chars 中的每组 连续重复字符
//
//
// 如果这一组长度为 1 ,则将字符追加到 s 中。
// 否则,需要向 s 追加字符,后跟这一组的长度。
//
//
// 压缩后得到的字符串 s 不应该直接返回 ,需要转储到字符数组 chars 中。需要注意的是,如果组长度为 10 或 10 以上,则在 chars 数组中会
//被拆分为多个字符。
//
// 请在 修改完输入数组后 ,返回该数组的新长度。
//
// 你必须设计并实现一个只使用常量额外空间的算法来解决此问题。
//
//
//
// 示例 1
//
//
//输入chars = ["a","a","b","b","c","c","c"]
//输出:返回 6 ,输入数组的前 6 个字符应该是:["a","2","b","2","c","3"]
//解释:
//"aa" 被 "a2" 替代。"bb" 被 "b2" 替代。"ccc" 被 "c3" 替代。
//
//
// 示例 2
//
//
//输入chars = ["a"]
//输出:返回 1 ,输入数组的前 1 个字符应该是:["a"]
//解释:
//没有任何字符串被替代。
//
//
// 示例 3
//
//
//输入chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
//输出:返回 4 ,输入数组的前 4 个字符应该是:["a","b","1","2"]。
//解释:
//由于字符 "a" 不重复,所以不会被压缩。"bbbbbbbbbbbb" 被 “b12” 替代。
//注意每个数字在数组中都有它自己的位置。
//
//
//
//
// 提示:
//
//
// 1 <= chars.length <= 2000
// chars[i] 可以是小写英文字母、大写英文字母、数字或符号
//
// Related Topics 双指针 字符串 👍 260 👎 0
package leetcode.editor.cn;
//443:压缩字符串
class StringCompression {
public static void main(String[] args) {
//测试代码
Solution solution = new StringCompression().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int compress(char[] chars) {
char ch = chars[0];
String str = "";
int count = 1;
for (int i = 1; i < chars.length; i++) {
if (chars[i] == ch) {
count++;
} else {
if (count > 1) {
str += "" + ch + count;
} else {
str += "" + ch;
}
ch = chars[i];
count = 1;
}
}
if (count > 1) {
str += "" + ch + count;
} else {
str += "" + ch;
}
for (int i = 0; i < str.length(); i++) {
chars[i] = str.charAt(i);
}
return str.length();
}
}
//leetcode submit region end(Prohibit modification and deletion)
}