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. 再起航,我的学习笔记之JavaScript设计模式23(中介者模式)

    中介者模式 概念介绍 中介者模式(Mediator):通过中介者对象封装一系列对象之间的交互,使对象之间不再相互引用降低他们之间的耦合,有时中介者对象也可以改变对象之间的交互. 创建一个中介 中介者模 ...

  2. MyeclipseJRE版本设置

    1.首先添加JDK版本 Window——Preferences——Java——Install JREs——Add——Stand VM——浏览JDK安装版本完成即可(一定是JDK中JRE的安装目录如:D ...

  3. SpringMVC的数据格式化-注解驱动的属性格式化

    一.什么是注解驱动的属性格式化? --在bean的属性中设置,SpringMVC处理 方法参数绑定数据.模型数据输出时自动通过注解应用格式化的功能. 二.注解类型 1.DateTimeFormat @ ...

  4. JavaScript自动化构建工具入门----grunt、gulp、webpack

    蛮荒时代的程序员: 做项目的时候,会有大量的js 大量的css   需要合并压缩,大量时间需要用到合并压缩 在前端开发中会出现很多重复性无意义的劳动  自动化时代的程序员: 希望一切都可以自动完成  ...

  5. Zend Framework 3.0 安装及创建初始化项目教程

    前言: 最近开始接触关于PHP的框架的学习,然而PHP的框架少说也有七八种. 百度了一下,有人说ThinkPHP简单暴力的,有人说Laravel高大上的,等等等等,难以抉择. 最终我还是选择先从接触Z ...

  6. 复选框demo

    本篇文章是关于复选框的,有2种形式:1.全选.反选由2个按钮实现:2.全选.反选由一个按钮实现. <!DOCTYPE html> <html> <head> < ...

  7. 【codevs1001】[bzoj1050]舒适的路线

    给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径上最大边和最小边的比值最小. ...

  8. RobotFramework自动化测试框架-移动手机自动化测试Click Element关键字的使用

    Click Element关键字用来模拟点击APP界面上的一个元素,该关键字接收一个参数[ locator ] ,这里的locator指的是界面元素的定位方式. 示例1:使用Click Element ...

  9. ZOJ2110 HDU1010 搜索 Tempter of the Bone

    传送门:Tempter of the Bone 大意是给一个矩阵,叫你是否可以在给定的可走路径上不重复地走,在最后一秒走到终点. 我用了两个剪枝,且称其为简直001和剪枝002,事实证明001不要都可 ...

  10. java web Servlet 学习笔记 -3 会话管理技术

     Cookie和HttpSession 什么是会话: 用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 每个用户在使用浏览器与服务器进行会话的过 ...