C#LeetCode刷题之#443-压缩字符串(String Compression)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...
- C#LeetCode刷题之#859-亲密字符串(Buddy Strings)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3973 访问. 给定两个由小写字母构成的字符串 A 和 B ,只要 ...
- C#LeetCode刷题之#557-反转字符串中的单词 III(Reverse Words in a String III)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3955 访问. 给定一个字符串,你需要反转字符串中每个单词的字符顺 ...
- C#LeetCode刷题之#345-反转字符串中的元音字母(Reverse Vowels of a String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3935 访问. 编写一个函数,以字符串作为输入,反转该字符串中的元 ...
- C#LeetCode刷题之#344-反转字符串(Reverse String)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3933 访问. 编写一个函数,其作用是将输入的字符串反转过来. 输 ...
- C#LeetCode刷题之#541-反转字符串 II(Reverse String II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3951 访问. 给定一个字符串和一个整数 k,你需要对从字符串开头 ...
- [Swift]LeetCode443. 压缩字符串 | String Compression
Given an array of characters, compress it in-place. The length after compression must always be smal ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- C#LeetCode刷题-双指针
双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.5% 中等 11 盛最多水的容器 43.5% 中等 15 三数之和 16.1% 中等 16 最接近的三数之和 3 ...
随机推荐
- Windows 磁盘分区后如何再合并&如何用Windows自带工具扩大某个分区
Windows 磁盘分区后如何再合并&用Windows自带工具扩大某个分区 注:此方法有一定的成功率,更加完善可行的方法请看http://www.diskgenius.cn/help/part ...
- Docker 概念-1
阅读本文大概需要15分钟,通过阅读本文你将知道一下概念: 容器 什么是Docker? Docker思想.特点 Docker容器主要解决什么问题 容器 VS 虚拟机 Docker基本概念: 镜像(Ima ...
- springboot(五)使用FastJson返回Json视图
FastJson简介: fastJson是阿里巴巴旗下的一个开源项目之一,顾名思义它专门用来做快速操作Json的序列化与反序列化的组件.它是目前json解析最快的开源组件没有之一!在这之前jaskJs ...
- Java事务解析(事务的基本操作+隔离的等级+事务的四大特性+事务的概念)
Java事务解析(事务的基本操作+隔离的等级+事务的四大特性+事务的概念) 什么是事务? 如果一个包含多个步骤的业务操作,这些操作被事务管理,那么这些操作要么同时成功要么同时失败 事务的四大特性(必须 ...
- R语言 循环语句、分支语句和中止语句-控制流篇
for 循环 用法 for (n in m) expr 若n在m中则运行 expr while 循环 用法 while (condition) expr 当符合condition时运行expr rep ...
- 常见的HTTP返回状态值
200 (成功) 服务器已成功处理了请求. 通常,这表示服务器提供了请求的网页. 301 (永久移动) 请求的网页已永久移动到新位置. 服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自 ...
- Docker引言,由来,思想
引言 我本地运行没问题啊? 环境不一致? 哪个哥们又写死循环了?,怎么这么卡? 在多用户操作系统下,会相互影响 淘宝在双11的时候,用户量暴增 运维成果过高的问题 学习一门技术,学习安装成本高 关于安 ...
- 17条嵌入式C语言编程小知识总结
流水线被指令填满时才能发挥最大效能,即每时钟周期完成一条指令的执行(仅指单周期指令). 如果程序发生跳转,流水线会被清空,这将需要几个时钟才能使流水线再次填满.因此,尽量少的使用跳转指令可以提高程序执 ...
- PHP strtotime() 函数
------------恢复内容开始------------ 实例 将任何字符串的日期时间描述解析为 Unix 时间戳: <?php // 设置时区 date_default_timezone_ ...
- PHP zip_entry_compressedsize() 函数
定义和用法 zip_entry_compressedsize() 函数返回 zip 档案项目的压缩文件尺寸.高佣联盟 www.cgewang.com 语法 zip_entry_compressedsi ...