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 ' '

思路:

需要注意的是,数字可能是大于10的也就是说不一定是一位数。。。

参考了大神的代码。。

class StringIterator {
string a;
size_t i = , c = ;
char ch;
public:
StringIterator(string a) {
this->a = a;
} char next() {
if (c)
return c--, ch;
if (i >= a.size())
return ' ';
ch = a[i++];
while (i < a.size() && isdigit(a[i]))
c = c*+a[i++]-'';
c--;
return ch;
} bool hasNext() {
return c || i < a.size();
}
};

参考:

https://leetcode.com/maskray/

[leetcode-604-Design Compressed String Iterator]的更多相关文章

  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】604. Design Compressed String Iterator 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...

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

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

  4. LeetCode Design Compressed String Iterator

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

  5. Design Compressed String Iterator

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

  6. Leetcode 344:Reverse String 反转字符串(python、java)

    Leetcode 344:Reverse String 反转字符串 公众号:爱写bug Write a function that reverses a string. The input strin ...

  7. [LeetCode] 641.Design Circular Deque 设计环形双向队列

    Design your implementation of the circular double-ended queue (deque). Your implementation should su ...

  8. [LeetCode] 622.Design Circular Queue 设计环形队列

    Design your implementation of the circular queue. The circular queue is a linear data structure in w ...

  9. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

随机推荐

  1. 转:Linux中文显示乱码?如何设置centos显示中文

    怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一般是由于没有安装中文语言包,或者设置的默认语言有问题导 ...

  2. WPF学习随笔

    内容控件 Padding内边距,Margin外边距 1.ScrollViewer滚动条控件 <ScrollViewer VerticalScrollBarVisibility="Vis ...

  3. SAP ECC EHP7 RFC 发布成WebService

    1.说明介绍 本文将RFC发布成WebService的详细步骤,参考了百度经验http://jingyan.baidu.com/article/8275fc867c9e2946a13cf66c.htm ...

  4. 【Javascript语言精粹】笔记摘要

    现在大部分编译语言中都流行要求强类型.其原理在于强类型允许编译器在编译时检测错误.我们能越早检测和修复错误,付出的代价越小.Javascript是一门弱类型的语言,所以Javascript编译器不能检 ...

  5. cocoapods卸载重装 解决clone,install,search很慢的问题

    电脑上面的cocoapods clone,pod install search的时候非常非常的慢,尝试了很多方法都无法解决,最后只能尝试着重装看看能不能解决问题 卸载 sudo gem uninsta ...

  6. iOS textfield 限制输入字数长度

    iOS textfield限制输入的最大长度 [self.textFiled addTarget:self action:@selector(textFieldDidChange:) forContr ...

  7. OpenCV探索之路(十三):详解掩膜mask

    在OpenCV中我们经常会遇到一个名字:Mask(掩膜).很多函数都使用到它,那么这个Mask到底什么呢? 一开始我接触到Mask这个东西时,我还真是一头雾水啊,也对无法理解Mask到底有什么用.经过 ...

  8. Metrics

    系统开发到一定的阶段,线上的机器越来越多,就需要一些监控了,除了服务器的监控,业务方面也需要一些监控服务.Metrics作为一款监控指标的度量类库,提供了许多工具帮助开发者来完成自定义的监控工作. 举 ...

  9. Java集合类从属关系

    Java的集合分为了四类:List Set Queue Map,每类都有不同的实现,有基于数组实现的,有基于链表实现的,有基于xx树实现的,不同的实现虽在功能上可以相互替代但都有各自的应用场景,如基于 ...

  10. [认证授权] 4.OIDC(OpenId Connect)身份认证授权(核心部分)

    0 目录 认证授权系列:http://www.cnblogs.com/linianhui/category/929878.html 1 什么是OIDC? 看一下官方的介绍(http://openid. ...