【LeetCode】604. Design Compressed String Iterator 解题报告(C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/design-compressed-string-iterator/
题目描述
Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext.
The given compressed string will be in the form of each letter followed by a positive integer representing the number of this letter existing in the original uncompressed string.
next()- if the original string still has uncompressed characters, return the next letter; Otherwise return a white space.hasNext()- Judge whether there is any letter needs to be uncompressed.
Note:
- Please remember to RESET your class variables declared in StringIterator, as static/class variables are persisted across multiple test cases. Please see here for more details.
Example:
StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1");
iterator.next(); // return 'L'
iterator.next(); // return 'e'
iterator.next(); // return 'e'
iterator.next(); // return 't'
iterator.next(); // return 'C'
iterator.next(); // return 'o'
iterator.next(); // return 'd'
iterator.hasNext(); // return true
iterator.next(); // return 'e'
iterator.hasNext(); // return false
iterator.next(); // return ' '
题目大意
对于一个压缩字符串,设计一个数据结构,它支持如下两种操作: next 和 hasNext。
给定的压缩字符串格式为:每个字母后面紧跟一个正整数,这个整数表示该字母在解压后的字符串里连续出现的次数。
- next() - 如果压缩字符串仍然有字母未被解压,则返回下一个字母,否则返回一个空格。
- hasNext() - 判断是否还有字母仍然没被解压。
解题方法
维护当前字符和次数
这个题是很常见的题目,使用变量分别保存当前的字符以及其出现的次数,如果所有的字符都用完则没有下一个字符了。
注意两点:
- 字符出现的次数可能>=10
- 最后一个字符用完时才算结束
C++代码如下:
class StringIterator {
public:
StringIterator(string compressedString) {
str = compressedString;
index = 0;
cur = ' ';
count = 0;
}
char next() {
if (!hasNext()) {
return ' ';
}
if (count != 0) {
count --;
return cur;
}
cur = str[index];
index ++;
while (str[index] >= '0' && str[index] <= '9') {
count = 10 * count + str[index] - '0';
index ++;
}
count --;
return cur;
}
bool hasNext() {
return index < str.size() || count != 0;
}
private:
string str;
int index;
char cur;
int count;
};
/**
* Your StringIterator object will be instantiated and called as such:
* StringIterator* obj = new StringIterator(compressedString);
* char param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
日期
2019 年 9 月 19 日 —— 举杯邀明月,对影成三人
【LeetCode】604. Design Compressed String Iterator 解题报告(C++)的更多相关文章
- LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- LeetCode Design Compressed String Iterator
原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and ...
- Design Compressed String Iterator
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- 【LeetCode】942. DI String Match 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】541. Reverse String II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】844. Backspace String Compare 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...
- [leetcode-604-Design Compressed String Iterator]
Design and implement a data structure for a compressed string iterator. It should support the follow ...
随机推荐
- CUDA计算矩阵相乘
1.最简单的 kernel 函数 __global__ void MatrixMulKernel( float* Md, float* Nd, float* Pd, int Width) { int ...
- 【基因组预测】braker2基因结构注释要点记录
目录 流程使用 问题 记录下braker2的使用要点,以备忘记. 流程使用 braker2有很多流程,根据你的数据:组装的基因组.转录组.蛋白(同源,包括近缘或远缘)选择不同流程,官网有说明: htt ...
- GWAS与GS模型介绍与比较
目录 1.GWAS模型 1.1卡方检验 1.2 相关性系数的t检验 1.3 一般线性模型GLM 1.4 混合线性模型MLM 1.5 压缩混合线性模型CMLM 1.6 SUPER 1.7 FarmCPU ...
- python19 操作mysql
connect 模块下载 https://dev.mysql.com/downloads/connector/python/ import mysql.connector con = mysql.co ...
- Excel-转换单元格格式的函数或“方法”汇总
14.转换单元格格式的函数或"方法"汇总 =value(单元格) #转换为数值 =A1&"" #转换A1为文本 = ...
- Oracle-distinct()用法、count(distinct( 字段A || 字段B))是什么意思?distinct多个字段
0.distinct用法 在oracle中distinct的使用主要是在查询中去除重复出现的数据 直接在字段前加distinct关键字即可,如:select distinct 名字 from tabl ...
- Freeswitch 安装爬坑记录1
2 Freeswitch的安装 2.1 准备工作 服务器安装CentOS 因为是内部环境,可以关闭一些防火墙设置,保证不会因为网络限制而不能连接 关闭防火墙 查看防火墙 systemctl statu ...
- 外网无法访问hdfs文件系统
由于本地测试和服务器不在一个局域网,安装的hadoop配置文件是以内网ip作为机器间通信的ip. 在这种情况下,我们能够访问到namenode机器, namenode会给我们数据所在机器的ip地址供我 ...
- Spark(四)【RDD编程算子】
目录 测试准备 一.Value类型转换算子 map(func) mapPartitions(func) mapPartitions和map的区别 mapPartitionsWithIndex(func ...
- Linux基础命令---uptime
uptime uptime指令用来显示系统运行多长时间.有多少用户登录.系统负载情况. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.SUSE.openSUSE. ...