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. 国庆集训Day1

    T1 divide 题意: 有\(n\)个数 \(a_1, a_2,..., a_n\) 有m个数\(b_1, b_2,..., b_n\) 令\(a = a_1\times a_2\,\times ...

  2. Ubuntu14.04 系统复制迁移到新的机器上

    1.打包系统文件 制作启动盘,然后进入bios, #进入bios的boot标签选择sandisk启动 如果没有找到u盘进入save & exit标签页选择boot override中的sand ...

  3. 【CF589 E】Another Filling the Grid

    一个很套路的容斥裸题,这里记录一下scb 的切题过程 Description 有一个 \(n\times n\) 的矩阵,你需要往每格里填一个 \([1,k]\) 的整数,使得每一行.每一列的最小值都 ...

  4. 2016百度之星资格赛 Problem A(前缀积与求逆元)

    题意:给出一个字符串,每次询问给出x和y要求算出从x到y的每个字符的(ASCII 码值-28)的值的积(mod9973). 分析:首先的想法肯定是算出每个位置的前缀积,然后只要F[y]/F[x-1]即 ...

  5. elasticsearch _bulk api

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/bulk.htmlbulk API 允许在单个步骤中进行多次 create . ...

  6. 表单 Flask-WTF - 使用

    1 配置 可以使用Flask-WTF来处理web表单,在使用之前要先配置下,打开config.py,编辑添加如下内容 WTF_CSRF_ENABLED = True SECRET_KEY = 'you ...

  7. QT 自定义消息

    #define TEST_EVENT QEvent::User + 100   class CVxActuatorMain : public QMainWindow {   protected:    ...

  8. flutter 中 List 和 Map 的用法

    list集合 在Dart中,数组是List对象,因此大多数人只是将它们称为List.以下是一个简单的Dart的List: 创建一个int类型的list List list = [10, 7, 23]; ...

  9. [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- ...

  10. 【404】int main(int argc,char * argv[]) windows 下的使用

    参考:int main(int argc,char * argv[]) windows 下的使用 参考:Theprogram can't start because libgcc_s_dw2-1.dl ...