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. C#关于通过反射PropertyType判读字符串类型方法

    今天在通过反射判读实体属性来映射数据库表是否修改的时候发现,最开始我通过 p.GetValue(entity) == null && p.PropertyType.IsValueTyp ...

  2. thinkphp5.0无限极分类及格式化输出

    首先我们来看数据表 从上图中可以发现,中国下有贵州,北京两个子节点,而北京有天安门一个子节点,纽约的子节点是"纽约的子类". 从pid为0看出,中国和纽约是顶级节点. 因为贵州的p ...

  3. Linux下利用expect,不用交互模式,直接登陆远程主机

    Linux环境下只有在机器20.200.254.18上ssh dataconv@20.200.31.23才能连接到23的机器,而且还需要输入密码(每次都需要输入地址,密码很烦),所以利用expect写 ...

  4. 3.bootstrap-组件

    1.图标 <button type="button" class="btn btn-default"> <span class="g ...

  5. [UWP]创建一个进度按钮

    1. 前言 最近想要一个进度按钮. 传统上UWP上处理进度可以这样实现,首先是XAML,包括一个ProgressBar和一个按钮: <StackPanel Orientation="H ...

  6. HADOOP源码分析之RPC(1)

    源码位于Hadoop-common ipc包下 abstract class Server 构造Server protected Server(String bindAddress, int port ...

  7. web版的tty

      1.wetty Wetty是使用Node.js和websockets开发的一个开源`Web-based SSH` 2.环境配置 2.1.配置epel源 [epel] name=epel baseu ...

  8. H5音频处理的一些小知识

      前  言 LiuDaP 十一过后,小编要做一个关于音乐播放器的项目,要用到大量H5音频处理的内容,于是在十月一日国庆黄金周闲暇之际,自己学习了一下H5音频的相关内容.虽然自学的没有那么深入,但是对 ...

  9. 入坑IT十年(二)技术以外

    上一篇博客里提到:技术越来越简单,发布后不久,就看到<技术并不是越来越简单>,这显然是打擂台来了. 技术究竟是不是越来越简单?其实这个问题,要看你究竟是以什么角度来思考这个问题.我们可以举 ...

  10. Oracle添加含有脏数据的约束

    需求: 一个表的唯一约束被禁用期间,有脏数据进来,当启用约束时失败. 环境: -bash-4.1$ uname -a Linux dbtest1 2.6.32-279.el6.x86_64 #1 SM ...