题目地址: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. 多组学分析及可视化R包

    最近打算开始写一个多组学(包括宏基因组/16S/转录组/蛋白组/代谢组)关联分析的R包,避免重复造轮子,在开始之前随便在网上调研了下目前已有的R包工具,部分罗列如下: 1. mixOmics 应该是在 ...

  2. 55. Binary Tree Preorder Traversal

    Binary Tree Preorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 119655 Tota ...

  3. Webpack 打包 Javascript 详细介绍

    本篇我们主要介绍Webpack打包 Javascript.当然,除了可以打包Javascript之外,webpack还可以打包html.但是这不是我们本篇的重点.我们可以参考 Webpack HTML ...

  4. A Child's History of England.32

    And so, in darkness and in prison, many years, he thought of all his past life, of the time he had w ...

  5. Spring Boot 创建定时任务(配合数据库动态执行)

    序言:创建定时任务非常简单,主要有两种创建方式:一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer). 前者相信大家都很熟悉,但是实际使用中我们往往想从数据库 ...

  6. OC-基础数据类型

    七 字符串与基本数据类型转换 获取字符串的每个字符/字符串和其他数据类型转换 八 NSMutableString 基本概念/常用方法 九 NSArray NSArray基本概念/创建方式/注意事项/常 ...

  7. OpenStack之六: plancement服务(端口8778)

    官网地址:https://docs.openstack.org/placement/stein/install/install-rdo.html #:创建placement库,并授权 MariaDB ...

  8. 通过js禁用浏览器的回退事件

    js代码: <script> history.pushState(null, null, document.URL); window.addEventListener('popstate' ...

  9. 因Console.Read()导致Centos 后台运行.net core程序报错

    .net 控制台程序通常用 Console.Read(),或者Console.ReadKey()让进程阻塞保持,不退出. 但在.net core 需要将程序放在后台执行时 用Console.Read( ...

  10. Linkerd Service Mesh 授权策略(Server & ServerAuthorization)

    简介 Server 和 ServerAuthorization 是 Linkerd 中的两种策略资源, 用于控制对 mesh 应用程序的入站访问. 在 linkerd 安装期间,policyContr ...