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 ...
随机推荐
- fastJson与jackson性能对比
转载:https://blog.csdn.net/u013433821/article/details/82905222最近项目用到fastJson和jackson,为了决定到底弃用哪个,随手写了个测 ...
- 协议基础:SMTP:使用Telnet学习SMTP协议
协议基础:SMTP:使用Telnet学习SMTP协议 2018-07-30 20:05:50 liumiaocn 阅读数 7479更多 分类专栏: 工具 Unix/Linux 版权声明:本文为博主 ...
- Geos判断点是否在多边形内
使用的geo版本是3.5.1 #include <iostream> #include "geos.h" using namespace std; GeometryFa ...
- FOFA 批量采集url 图形化界面编写
这是脚本 # coding:utf- import requests,re import time import sys import getopt import base64 guizhe='' s ...
- Gi命令行操作
一.本地库初始化 命令:git init 效果: 注意:.git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱修改 二.设置签名 形式 用户名:user Email 地址:user@1 ...
- jinjia2-HTML 转义
当从模板生成 HTML 时,始终有这样的风险:变量包含影响已生成 HTML 的字符.有两种 解决方法:手动转义每个字符或默认自动转义所有的东西. Jinja 两者都支持,使用哪个取决于应用的配置.默认 ...
- [drf]model设置
参考 //# 给model添加虚拟字段 class CeleryExampleResult(models.Model): task_id = models.BigIntegerField(defaul ...
- 交叉编译多平台 FFmpeg 库并提取视频帧(转)
交叉编译多平台 FFmpeg 库并提取视频帧 转 https://www.cnblogs.com/leviatan/p/11142579.html 本文档适用于 x86 平台编译 armeabi.a ...
- ckpt convert to pb
import tensorflow as tf #from create_tf_record import * from tensorflow.python.framework import grap ...
- 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_06-新增页面-前端-新增页面
新建一个添加的页面 复制page_list页面改改名字 page_add 一个标准的页面 <template> <div> 新增页面 </div> </tem ...