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 ' '
 public class StringIterator {
int i;
String[] arr;
int[] counts; public StringIterator(String str) {
arr = str.split("\\d+");
counts = Arrays.stream(str.substring().split("[a-zA-Z]+")).mapToInt(Integer::parseInt).toArray();
} public char next() {
if (!hasNext()) {
return ' ';
}
char ch = arr[i].charAt();
counts[i]--; if (counts[i] == ) {
++i;
}
return ch;
} public boolean hasNext() {
if (i == arr.length) {
return false;
}
return true;
}
}

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] Design Compressed String Iterator 设计压缩字符串的迭代器

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

  3. LeetCode Design Compressed String Iterator

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

  4. 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)

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

  5. [leetcode-604-Design Compressed String Iterator]

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

  6. HDU - 5557 Matching Compressed String (自动机+倍增+表达式计算)

    题意是给你一个自动机和一个字符串的括号表达式,问自动机能否接受这个字符串. 我一想,这不就是个模拟栈计算表达式+倍增么? 再一想,复杂度200*1000*10000*log(1e9),不对啊! 交上去 ...

  7. [LeetCode] String Compression 字符串压缩

    Given an array of characters, compress it in-place. The length after compression must always be smal ...

  8. LeetCode String Compression

    原题链接在这里:https://leetcode.com/problems/string-compression/description/ 题目: Given an array of characte ...

  9. 【LeetCode】设计题 design(共38题)

    链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...

随机推荐

  1. dblclick([[data],fn]) 当双击元素时,会发生 dblclick 事件。

    dblclick([[data],fn]) 概述 当双击元素时,会发生 dblclick 事件.大理石量具哪家好 当鼠标指针停留在元素上方,然后按下并松开鼠标左键时,就会发生一次 click.在很短的 ...

  2. ES6-21.class基本语法

    1.简介(详情参考) class是构造函数的语法糖. class的constructor方法内的实现,就是原来构造函数的实现. class内的所有方法都是在prototype上的,就是原来构造函数的p ...

  3. Codeforces.520B.Two Buttons(正难则反)

    题目链接 \(Description\) 给定两个数\(n,m\),每次可以使\(n\)减一或使\(n\)乘2.求最少需要多少次可以使\(n\)等于\(m\). \(Solution\) 暴力连边BF ...

  4. ZR#996

    ZR#996 解法: 若删除长度为 $ x $ 的子串后序列中没有相同元素,那么一定有至少一个长度为 $ x+1 $ 的子串,删除它后序列中也没有相同元素. CODE: #include <io ...

  5. CF1195A

    CF1195A 题意: 输入n和k,n是学生的数量,k是饮料种类,接下来的n行会输入每个学生想要的饮料的编号,分配饮料是按一对一对分,每一对都是类型相同的饮料.输出能得到自己想要饮料的最大学生数量 解 ...

  6. 你真的会用go语言写单例模式吗?

    最近在学习Golang,想着可以就以前的知识做一些串通,加上了解到go语言也是面向对象编程语言之后.在最近的开发过程中,我碰到一个问题,要用go语言实现单例模式.本着“天下知识,同根同源”(我瞎掰的~ ...

  7. PYTHON -----pyinstaller的安装

    这几天一直在安装pyinstaller库,发现了一个好方法 因为一直使用pip安装,我只能介绍一下pip安装pyinstaller的方法的注意事项 1:一般pip会在安装python的Scripts文 ...

  8. Get Raster Properties获得栅格的信息

    Summary Returns the properties of a raster dataset. Usage The property returned will be displayed in ...

  9. Java同步数据结构之SynchronousQueue

    前言 严格来说SynchronousQueue并不是像它的名字那样是一种Queue,它更像是一个数据接力的交汇点,还记得在介绍Exchanger的时候提到过Exchanger可以看作是Synchron ...

  10. [Scikit-learn] 1.1 Generalized Linear Models - Bayesian Ridge Regression

    1.1.10. Bayesian Ridge Regression 首先了解一些背景知识:from: https://www.r-bloggers.com/the-bayesian-approach- ...