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

题目标签:Design
  这道题目给了我们一个string, string是由一个char 紧接着 一个 正数int 这种重复排列。让我们设计一个迭代器,把每一个字母按照它后面那个数字来print出。
  StringIterator: 当我们创造一个 StringIterator object 的时候,我们要把 这个string 保存好,以便于之后可以call next 和 hasNext。
        我们可以建立两个list, counts 和 letters。 遍历compressedString, 判断这一个char 是不是 letter, 是的话,直接把char 存进letters list。如果这个char 是数字digit 的话,那么我们需要继续往后找,直到找到一个不是数字的char,或者它超出size了。
        因为数字可以是1位,也可以2位或者更多。
 
  Next():当我们存好了letters 和counts, 就利用counts 来return 相对应的char, 每次用完一个char, 要把它的count - 1。如果当这个count = 0 的时候,那么我们需要移动到下一个char。这里我们需要一个cursor_index 来跟踪我们的char。
 
  hasNext():这里只需要判断,如果我们的cursor_index 超出了 任意一个list 的size, 就表示已经用完了所有的char。
 
 

Java Solution:

Runtime beats 86.01%

完成日期:07/10/2017

关键词:Design

关键点:用两个list和一个cursor来找到char和对应char的count

 public class StringIterator
{
ArrayList<Integer> counts;
ArrayList<Character> letters;
int cursor_index = 0; public StringIterator(String compressedString)
{
counts = new ArrayList<>();
letters = new ArrayList<>(); for(int i=0; i<compressedString.length(); i++)
{
// if find a letter
if(Character.isLetter(compressedString.charAt(i)))
letters.add(compressedString.charAt(i)); // if find a digit
else if(Character.isDigit(compressedString.charAt(i)))
{
int end = i+1;
// find the next non-digit char within the length
while(end < compressedString.length() &&
Character.isDigit(compressedString.charAt(end)))
end++; counts.add(Integer.parseInt(compressedString.substring(i, end))); i = end - 1;
}
}
} public char next()
{
char c; // meaning string is finished
if(!hasNext())
return ' '; c = letters.get(cursor_index);
counts.set(cursor_index, counts.get(cursor_index) - 1); if(counts.get(cursor_index) == 0)
cursor_index++; return c;
} public boolean hasNext()
{
// if string is finished
if(cursor_index > counts.size() - 1 )
return false;
else // if string is not finished
return true;
}
} /**
* Your StringIterator object will be instantiated and called as such:
* StringIterator obj = new StringIterator(compressedString);
* char param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$的更多相关文章

  1. [LeetCode] 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

    原题链接在这里: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-604-Design Compressed String Iterator]

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

  6. [LeetCode] 642. Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  7. ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

  9. [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

随机推荐

  1. Hibernate第七篇【对象状态、一级缓存】

    前言 本博文主要讲解Hibernate的细节-->对象的状态和一级缓存- 对象状态 Hibernate中对象的状态: - 临时/瞬时状态 - 持久化状态 - 游离状态 学习Hibernate的对 ...

  2. Oracle_Sequence如何初始化开始值

    Sequence的start with 值如何确定才能保证生成的主键不会冲突??? 我的项目中最开始数据库表主键的生成策略是 increment,但由于后来采用了集群部署的方式,出现了主键冲突的问题. ...

  3. mariadb自带命令行客户端指令笔记

    mysql -H 主机IP -u 用户名 -p -p表示要输密码,不要直接输了,要回车后在程序里输入 显示数据库列表: show databases; 选择XX数据库: use XX; 显示数据库里的 ...

  4. 关于使用lombok遇到的问题

    在官网上下载了lombok.jar包以后,有两种安装方式 : 1. 双击下载下来的 JAR 包安装 lombok    我选择这种方式安装的时候提示没有发现任何 IDE,所以我没安装成功,我是手动安装 ...

  5. codeigniter 去除index.php (nginx,apache) 通用方法

    .htaccess文件配置 1 <IfModule mod_rewrite.c> 2 RewriteEngine On 3 RewriteBase / 4 RewriteCond $1 ! ...

  6. AngularJS指南文档

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 核心概念 模板 在Angular应用当中,我们的工作就是将服务器的数据填充到客户端页面模 ...

  7. 读Zepto源码之Fx模块

    fx 模块为利用 CSS3 的过渡和动画的属性为 Zepto 提供了动画的功能,在 fx 模块中,只做了事件和样式浏览器前缀的补全,没有做太多的兼容.对于不支持 CSS3 过渡和动画的, Zepto ...

  8. Windows+Apache2.4.10+PHP7.0+MySQL5.6.21安装

    一.安装包下载 apache2.4.10 http://www.apachelounge.com/download/win64/ PHP7.0.7 http://windows.php.net/dow ...

  9. 初识Hibernate之关联映射(二)

    上篇我们介绍了关联映射的几种形式,有单向多对一,单向一对多,还有双向一对多.本篇接着介绍有关关联映射的其他几种映射方式,主要有以下几种: 基于外键的单向一对一关联映射 基于主键的单向一对一关联映射 单 ...

  10. zoj3432 Find the Lost Sock 亦或的运用

                     只有一个出现奇数次,答案就是它了: #include<cstdio> #include<cstdlib> #include<iostre ...