问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3943 访问。

给定一组字符,使用原地算法将其压缩。

压缩后的长度必须始终小于或等于原数组长度。

数组的每个元素应该是长度为1 的字符(不是 int 整数类型)。

在完成原地修改输入数组后,返回数组的新长度。

进阶:你能否仅使用O(1) 空间解决问题?

输入:['a','a','b','b','c','c','c']

输出:返回6,输入数组的前6个字符应该是:['a','2','b','2','c','3']

说明:"aa"被"a2"替代。"bb"被"b2"替代。"ccc"被"c3"替代。

输入:['a']

输出:返回1,输入数组的前1个字符应该是:['a']

说明:没有任何字符串被替代。

输入:['a','b','b','b','b','b','b','b','b','b','b','b','b']

输出:返回4,输入数组的前4个字符应该是:['a','b','1','2']。

说明:由于字符"a"不重复,所以不会被压缩。"bbbbbbbbbbbb"被“b12”替代。注意每个数字在数组中都有它自己的位置。

注意:

  • 所有字符都有一个ASCII值在[35, 126]区间内。
  • 1 <= len(chars) <= 1000。

Given an array of characters, compress it in-place.

The length after compression must always be smaller than or equal to the original array.

Every element of the array should be a character (not int) of length 1.

After you are done modifying the input array in-place, return the new length of the array.

Follow up:Could you solve it using only O(1) extra space?

Input:['a','a','b','b','c','c','c']

Output:Return 6, and the first 6 characters of the input array should be: ['a','2','b','2','c','3']

Explanation:"aa" is replaced by "a2". "bb" is replaced by "b2". "ccc" is replaced by "c3".

Input:['a']

Output:Return 1, and the first 1 characters of the input array should be: ['a']

Explanation:Nothing is replaced.

Input:['a','b','b','b','b','b','b','b','b','b','b','b','b']

Output:Return 4, and the first 4 characters of the input array should be: ['a','b','1','2'].

Explanation:Since the character "a" does not repeat, it is not compressed. "bbbbbbbbbbbb" is replaced by "b12".Notice each digit has it's own entry in the array.

Note:

  • All characters have an ASCII value in [35, 126].
  • 1 <= len(chars) <= 1000.

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3943 访问。

public class Program {

    public static void Main(string[] args) {
var chars = new char[] { 'a', 'b', 'b' }; var res = Compress(chars);
Console.WriteLine(res); chars = new char[] { 'a', 'a', 'b', 'b', 'c', 'c', 'c' }; res = Compress2(chars);
Console.WriteLine(res); Console.ReadKey();
} private static int Compress(char[] chars) {
if(chars.Length == 1) return 1;
var count = 1;
var res = string.Empty;
for(var i = 1; i < chars.Length; i++) {
if(chars[i] == chars[i - 1]) {
count++;
} else {
res += chars[i - 1].ToString() + (count == 1 ? "" : count.ToString());
count = 1;
}
if(i == chars.Length - 1) res +=
chars[i].ToString() + (count == 1 ? "" : count.ToString());
}
for(int i = 0; i < res.Length; i++) {
chars[i] = res[i];
}
return res.Length;
} private static int Compress2(char[] chars) {
if(chars.Length == 1) return 1;
var index = 0;
var count = 1;
for(var i = 1; i < chars.Length; i++) {
if(chars[i] == chars[i - 1]) {
count++;
} else {
ResetChars(chars, ref count, ref index, i - 1);
}
if(i == chars.Length - 1) {
ResetChars(chars, ref count, ref index, i);
}
}
return index;
} private static void ResetChars(char[] chars, ref int count, ref int index, int i) {
if(count != 1) {
chars[index] = chars[i];
for(int j = 0; j < count.ToString().Length; j++) {
chars[j + index + 1] = count.ToString()[j];
}
index += count.ToString().Length + 1;
} else {
chars[index] = chars[i];
index++;
}
count = 1;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3943 访问。

3
6

分析:

显而易见,以上2种算法的时间复杂度均为:  。请注意 Compress2 的时间复杂度不是  ,因为数组只被扫描了一遍。

C#LeetCode刷题之#443-压缩字符串​​​​​​​(String Compression)的更多相关文章

  1. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

  2. C#LeetCode刷题之#859-亲密字符串​​​​​​​​​​​​​​(Buddy Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...

  3. C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...

  4. C#LeetCode刷题之#345-反转字符串中的元音字母​​​​​​​(Reverse Vowels of a String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...

  5. C#LeetCode刷题之#344-反转字符串​​​​​​​(Reverse String)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...

  6. C#LeetCode刷题之#541-反转字符串 II(Reverse String II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3951 访问. 给定一个字符串和一个整数 k,你需要对从字符串开头 ...

  7. [Swift]LeetCode443. 压缩字符串 | String Compression

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  8. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  9. C#LeetCode刷题-双指针

    双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.5% 中等 11 盛最多水的容器   43.5% 中等 15 三数之和   16.1% 中等 16 最接近的三数之和   3 ...

随机推荐

  1. html 转义和反转义

    public static void main(String[] args) {// String html = "<img style=\"width: 100%; hei ...

  2. Java顺序查找、二分查找

    Java顺序查找.二分查找   查找算法中顺序查找算是最简单的了,无论是有序的还是无序的都可以,只需要一个个对比即可,但其实效率很低. 顺序查找 动图演示 详细代码 // 顺序查找 public st ...

  3. 当小程序遇见物联网IoT,几行代码搞定智能插座控制

    在 5G 热潮的推动下,与其紧密结合的物联网(IoT)正日益成为个人和企业工作生活中的重要组成部分,它为企业和个人带来了操作流程的改进和更好的生活体验,随着人工智能(AI)技术的日趋成熟,IoT 与 ...

  4. 03 AMD规范的基础使用详解

    AMD模块规范 1.1 AMD规范说明 AMD规范专门用来实现浏览器端的模块化,并且模块的加载是异步的:引入一个第三方的require.js脚本用来解析AMD规范编写的模块 1.2 基本语法 使用de ...

  5. xctf-web supersqli

    单引号注入,用order by查到了两个column.用union select的时候发现select关键字被过滤了 用分号尝试堆叠注入显示出了两张表 分别查询字段 flag在表19198109311 ...

  6. PHP入门之类型与运算符(一)

    前言 PHP对于大部分人来说,是比较容易入门的.笔者也是刚学习不久,所以就把自己学习的基础知识进行总结和整理.第一部分是类型与运算符.如果你想学习PHP,可以参考PHP学习手册学习,任何一本教学资料也 ...

  7. PHP 变量讲解

    PHP 变量 变量是用于存储信息的"容器": 实例 <?php $x=5; $y=6; $z=$x+$y; echo $z; ?> 运行实例 » 与代数类似 x=5y= ...

  8. Skill 如何Flatten一个list

    https://www.cnblogs.com/yeungchie/ code unless(fboundp('ycFlattenList) procedure(ycFlattenList(listi ...

  9. luogu P3285 [SCOI2014]方伯伯的OJ splay 线段树

    LINK:方伯伯的OJ 一道稍有质量的线段树题目.不写LCT splay这辈子是不会单独写的 真的! 喜闻乐见的是 题目迷惑选手 \(op==1\) 查改用户在序列中的位置 题目压根没说位置啊 只有排 ...

  10. EC R 86 D Multiple Testcases 构造 贪心 二分

    LINK:Multiple Testcases 得到很多种做法.其中O(n)的做法值得一提. 容易想到二分答案 check的时候发现不太清楚分配的策略. 需要先考虑如何分配 容易发现大的东西会对小的产 ...