问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. LeetCode刷题第8天字符串系列之《378字符串中的第一个唯一字符》

    LeetCode 378字符串中的第一个唯一字符 题目描述 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 提示:你可以假定该字符串只包含小写字母. 示例 输入 ...

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

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

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

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

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

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

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

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

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

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

  8. [Leetcode刷题心得][数组、字符串]242. 有效的字⺟母异位词

    题目: 给定两个字符串串 s 和 t,编写⼀一个函数来判断 t 是否是 s 的字⺟母 异位词. 说明: 你可以假设字符串串只包含⼩小写字⺟母. 示例: 示例 1 输入: s = "anagr ...

  9. C#LeetCode刷题-字符串

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

最新文章

  1. 导购网站 服务器,导购网站云服务器配置
  2. 基于组件的.NET软件开发(3)
  3. Matlab的不同进制转换
  4. http请求中的Query String Parameters、Form Data、Request Payload
  5. P2414 NOI2011阿狸的打字机 [AC自动机,dfs序]
  6. Java 11即将发布的功能–启动单文件源程序
  7. dockerfile mysql例子_docker-compose 实用示例
  8. Mybatis中trim的使用
  9. 《A First Course in Probability》-chaper3-条件概率和独立性-贝叶斯公式、全概率公式...
  10. Google的Flutter工具允许开发者开发跨平台应用
  11. [转载]百分之百自动登录2345王牌技术员联盟源代码(delphi)
  12. Fiddler—PC上实现手机的抓包
  13. Python之获取中国各地区矢量地图数据(shp格式)
  14. 一千本免费电子书(建议长期保存)转的-用迅雷下载
  15. css外联式6,css样式内联式,外联式,嵌入式的格式是什么?
  16. 2016年民营企业500强榜单(全国工商联发布)
  17. Python计算均值、方差、标准差、协方差等常用指标的方法——Numpy模块+Pandas模块
  18. Cisco服务器硬盘状态jbod,如何为服务器硬盘配置RAID或JBOD模式
  19. Nginx负载均衡配置
  20. 遥感数据在植物识别的应用

热门文章

  1. Ethercat解析(十)之从站配置
  2. C++——size_t 和 size_type的区别
  3. 大一计算机专业学生如何在寒假充电?
  4. 方法重载 java 1614780176
  5. 日期时间选择器 DateTimePicker 1127
  6. python-操作数据库的练习
  7. linux-压缩与解压缩
  8. ZABBIX API简介及使用
  9. SQL Server 2008下日志清理方法
  10. [再寄小读者之数学篇](2014-07-27 $H^{-1}$ 中的有界集与弱收敛极限)