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:

  1. StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1");
  2.  
  3. iterator.next(); // return 'L'
  4. iterator.next(); // return 'e'
  5. iterator.next(); // return 'e'
  6. iterator.next(); // return 't'
  7. iterator.next(); // return 'C'
  8. iterator.next(); // return 'o'
  9. iterator.next(); // return 'd'
  10. iterator.hasNext(); // return true
  11. iterator.next(); // return 'e'
  12. iterator.hasNext(); // return false
  13. iterator.next(); // return ' '

思路:

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

参考了大神的代码。。

  1. class StringIterator {
  2. string a;
  3. size_t i = , c = ;
  4. char ch;
  5. public:
  6. StringIterator(string a) {
  7. this->a = a;
  8. }
  9.  
  10. char next() {
  11. if (c)
  12. return c--, ch;
  13. if (i >= a.size())
  14. return ' ';
  15. ch = a[i++];
  16. while (i < a.size() && isdigit(a[i]))
  17. c = c*+a[i++]-'';
  18. c--;
  19. return ch;
  20. }
  21.  
  22. bool hasNext() {
  23. return c || i < a.size();
  24. }
  25. };

参考:

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. 将github上的项目源码导入到eclipse中

    1.注册github帐号 在github上注册一个自己的帐号. 2.安装git插件egit 在eclipse中安装git插件egit,安装方法可以参考这篇文章: http://www.cnblogs. ...

  2. RabbitMQ学习2---使用场景

    RabbitMQ主页:https://www.rabbitmq.com/ AMQP AMQP协议是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现.它主要包括以下组件: 1.Serve ...

  3. jQuery之文档处理

    jQuery 文档处理 1)内部插入 2)外部插入 3)包裹 4)替换 5)删除 6)复制 1.内部插入 append(content|fn) 向每个匹配的元素内部追加内容. 向所有段落中追加一些HT ...

  4. php 知识点 --个人笔记

    ##2015-09-06 为防止用户看到错误信息,而出现的不友好界面.故一般性会在php.ini里设置:display_errors = Off;不过在开发的时候,我们有时候需要打开错误信息.这时候, ...

  5. PHP电商订单自动确认收货redis队列

    一.场景 之前做的电商平台,用户在收到货之后,大部分都不会主动的点击确认收货,导致给商家结款的时候,商家各种投诉,于是就根据需求,要做一个订单在发货之后的x天自动确认收货.所谓的订单自动确认收货,就是 ...

  6. 一个使用openGL渲染的炫丽Android动画库二(碎片化曲面动画)

    续一个使用openGL渲染的炫丽Android动画库 MagicSurfaceView v1.1.0发布, 新增碎片化曲面动画 地址:https://github.com/gplibs/android ...

  7. Java IO详解(六)------序列化与反序列化(对象流)

    File 类的介绍:http://www.cnblogs.com/ysocean/p/6851878.html Java IO 流的分类介绍:http://www.cnblogs.com/ysocea ...

  8. python 简单验证码 random模块

    random 模块,产生随机数: chr 将数字转成字母. ascii 数字与字符对应表 链接 import randomtemp=""for i in range(0,4): r ...

  9. repo版本切换

    repo init -u https://android.googlesource.com/platform/manifest repo sync 之后 这样初始化之后,相当于下载了全部的分支, 本想 ...

  10. 《安卓网络编程》之第五篇 WebView的使用

    Android提供了WebView组件,,在Android的所有组件中,WebView的功能是最强大的,是当之无愧的老大.WebView组件本身就是一个浏览器实现,她的内核是基于开源WebKit引擎. ...