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 ...
随机推荐
- shiro认证-SSM
shiro认证-SSM pom <dependency> <groupId>org.apache.shiro</groupId> <artifactId> ...
- 基础数据类型-字符串str
什么是字符串? 单引号,双引号,三引号包裹的文本 在我们的代码中换行区别 单/双引号:‘a’\ 'b' 三引号:"""a b""" 索引 s ...
- Ubuntu 16.04 下Redis Cluster集群搭建
实际操作如下: 准备工作 版本:4.0.2 下载地址:https://redis.io/download 离线版本:(链接: https://pan.baidu.com/s/1bpwDtOr 密码: ...
- .netcore signalR 实时消息推送
服务器端引入包 Install-Package Microsoft.AspNetCore.SignalR客户端引入包 npm install @aspnet/signalr <template ...
- 自动化测试报告浅谈之ExtentReports
我们在进行自动化测试时,往往需要有相应的测试报告,比如junit,testng,reportng等等,有会有自带的测试报告,那为什么我要在这边提ExtentReports?首先,我们来看看其它几种测试 ...
- pojo、po、dto、dao、bo区别
j2ee中,经常提到几种对象(object),理解他们的含义有助于我们更好的理解面向对象的设计思维. POJO(plain old java object):普通的java对象,有别于特殊的j ...
- while 循环 continue break 用法例子
py2 temp = "理解" # utf- 8 #解码, 需要指定原来的是什么编码 temp_unicode = temp.decode("utf-8") # ...
- Atcoder ABC 139C
Atcoder ABC 139C 题意: 有 $ n $ 个正方形,选择一个起始位置,使得从这个位置向右的小于等于这个正方形的高度的数量最多. 解法: 简单递推. CODE: #include< ...
- vue+webpack 实现懒加载的三种方式
第一种: 引入方式 就是正常的路由引入方式 const router = new Router({ routes: [ { path: '/hyh', component: hyh, name: 'h ...
- QPixmap QImage 相互转化
QPainter p(this); QPixmap pixmap; pixmap.load("E:\\参考文件\\image\\1.jpg"); //QPixmap->QIm ...