题目地址: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() - 判断是否还有字母仍然没被解压。

解题方法

维护当前字符和次数

这个题是很常见的题目,使用变量分别保存当前的字符以及其出现的次数,如果所有的字符都用完则没有下一个字符了。

注意两点:

  1. 字符出现的次数可能>=10
  2. 最后一个字符用完时才算结束

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++)的更多相关文章

  1. LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  2. [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  3. LeetCode Design Compressed String Iterator

    原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and ...

  4. Design Compressed String Iterator

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  5. 【LeetCode】942. DI String Match 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. 【LeetCode】541. Reverse String II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  7. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】844. Backspace String Compare 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字符串切片 栈 日期 题目地址:https://le ...

  9. [leetcode-604-Design Compressed String Iterator]

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

随机推荐

  1. 有限元边界 Dirichlet 条件处理

    参考自百度文档,这里只考虑 Dirichlet 边界条件情况. 有限元法基本方法就是是构造线性方程组 \[\begin{equation} Au = f \end{equation}\] 进行求解.其 ...

  2. Linux 软件安装位置选择指南

    Linux 软件安装   Linux 下安装软件不像 Windows 下安装这么简单,Windows 下会自动选择合适安装路径,而 Linux 下安装路径大部分完全由自己决定,我可以将软件安装到任意可 ...

  3. 日常Java 2021/11/4

    ServerSocket类的方法服务器应用程序通过使用java.net.ServerSocket类以获取一个端口,并且侦听客户端请求. 构造方法: public ServerSocket(int po ...

  4. iOS 客户端获取七牛上传token

    一.官方参考文档: 1.上传策略http://developer.qiniu.com/article/developer/security/put-policy.html 2.上传凭证(即uptoke ...

  5. liunux 6.5设置网卡默认开启

    编辑如下文件; vi /etc/sysconfig/network-scripts/ifcfg-eth0 把 ONBOOT=no 改为 ONBOOT=yes 好了网卡会在启动机器的时候一起启动了.

  6. 35、搜索插入位置 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(35)搜索插入位置 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 "无视要 ...

  7. drone使用git tag作为镜像tag

    官方自动tag plugin/docker 已支持自动标签,使用方法如下 steps: - name: docker image: plugins/docker settings: repo: foo ...

  8. TCP链接请求的10种状态

    一.状态显示 SYN_SENT:这个状态与SYN_RCVD遥相呼应,当客户端SOCKET执行CONNECT连接时,它首先发送SYN报文,随即进入到了SYN_SENT状态,并等待服务端的发送三次握手中的 ...

  9. java多线程6:ReentrantLock

    下面看下JUC包下的一大并发神器ReentrantLock,是一个可重入的互斥锁,具有比synchronized更为强大的功能. ReentrantLock基本用法 先来看一下ReentrantLoc ...

  10. 为什么要用urlencode()函数进行url编码

    URLEncode就是将URL中特殊部分进行编码.URLDecoder就是对特殊部分进行解码. 因为当字符串数据以url的形式传递给web服务器时,字符串中是不允许出现空格和特殊字符的 譬如:你要传的 ...