Design Compressed String Iterator
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的更多相关文章
- LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- LeetCode Design Compressed String Iterator
原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and ...
- 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...
- [leetcode-604-Design Compressed String Iterator]
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- HDU - 5557 Matching Compressed String (自动机+倍增+表达式计算)
题意是给你一个自动机和一个字符串的括号表达式,问自动机能否接受这个字符串. 我一想,这不就是个模拟栈计算表达式+倍增么? 再一想,复杂度200*1000*10000*log(1e9),不对啊! 交上去 ...
- [LeetCode] String Compression 字符串压缩
Given an array of characters, compress it in-place. The length after compression must always be smal ...
- LeetCode String Compression
原题链接在这里:https://leetcode.com/problems/string-compression/description/ 题目: Given an array of characte ...
- 【LeetCode】设计题 design(共38题)
链接:https://leetcode.com/tag/design/ [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure ...
随机推荐
- ubuntu14.0 更改默认python为3.5 并安装tensorflow(cpu)
转:http://blog.csdn.net/qq_27657429/article/details/53482595 第一:安装pip(如果有pip 跳过) #在ubuntu/Linux 64-bi ...
- ERRORS: ?: (corsheaders.E013) Origin '*' in CORS_ORIGIN_WHITELIST is missing scheme or netloc HINT:
报错信息 ERRORS: ?: (corsheaders.E013) Origin '*' in CORS_ORIGIN_WHITELIST is missing scheme or netloc H ...
- 2019巅峰极客CTF-web1(LOL英雄联盟)
今晚有空 以后随缘写博客了 好好沉淀 web1当天做出的队伍很少 其实不难 折腾到最后就差一步 可惜 0x01 读取文件 截图没留了 只留了代码部分. 有个页面 有上传和下载功能 起初 ...
- jvm 线程状态
NEW: Just starting up, i.e., in process of being initialized.NEW_TRANS: Corresponding transition sta ...
- 认识理解Java中native方法(本地方法)
Java不是完美的,Java的不足除了体现在运行速度上要比传统的C++慢许多之外,Java无法直接访问到操作系统底层(如系统硬件等),为此Java使用native方法来扩展Java程序的功能. 可 ...
- C#中正则表达式解析字符串信息
正则表达式提取0~9数字 private static string RegexPickupNumber(string str) { string pattern = @"[^0-9]+&q ...
- 使用druid连接池带来的坑testOnBorrow=false
首先说一下自己程序中遇到的问题,前一段时间新写了一个项目,主要架构改进,为前端提供接口(spring +springmvc+mybatis) 在新项目中使用的是阿里的druid连接池,配置简单,除了数 ...
- Markdown使用TOC自动生成导航栏
经常使用markdown 的玩家一定很想要一个自动生成的导航栏吧,自己写的基本思路就是 轮询监听滚动条的位置,通过抛锚和跳锚实现,这里介绍一下今天的主角,markdown-toc插件: https:/ ...
- nginx ssl
SSL 私钥/etc/pki/CA/ (umask 077;openssl genrsa -out private/cakey.pem 2048) 自签证书 openssl req -new -x50 ...
- ES6深入浅出-1 新版变量声明:let 和 const-1.视频 概述
es7语法比较少,只占了一点点 ES 6 新特性一览:https://frankfang.github.io/es-6-tutorials/ 我用了两个月的时间才理解 let https://zh ...