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 ...
随机推荐
- MySQL数据分析-(5)数据库设计之ER模型
大家好,我是jacky,很高兴跟大家分享本课时的内容,从本节课开始,就开始了我们第二章的学习,第一章我们抛出了若干问题,从第二章开始往后,都是解决问题的一个过程: 第一章的案例中,我们拿手机销售公司举 ...
- AE开发之txt转shp
实现坐标txt文件转shp点集数据文件的窗体Form txt格式为:首行为“id,x,y” 第二行开始输入具体数值:id,x,y(x,y为具体的xy坐标) using System; using Sy ...
- 你真的会用go语言写单例模式吗?
最近在学习Golang,想着可以就以前的知识做一些串通,加上了解到go语言也是面向对象编程语言之后.在最近的开发过程中,我碰到一个问题,要用go语言实现单例模式.本着“天下知识,同根同源”(我瞎掰的~ ...
- 重读APUE(4)-fcntl和ioctl的区别
fcntl(File Control)-文件控制 ioctl(In/Out Control)-I/O控制 1. fcntl作用于文件,提供对文件的基础控制:ioctl作用于文件和设备对象,一般用来向设 ...
- FreeMarker学习(springmvc配置)
springMvc配置 <bean id="freemarkerConfig" class="org.springframework.web.servlet.vie ...
- 用JS判断号码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python 调用父类方法, 重写父类构造方法, 不显式调用,会报错
子类不显式调用父类的构造方法,而父类构造函数初始化了一些属性,就会出现问题 如果子类和父类都有构造函数,子类其实是重写了父类的构造函数,如果不显式调用父类构造函数,父类的构造函数就不会被执行,导致子类 ...
- 配置nginx支持path_info
默认情况下,nginx是不支持path_info的,我们需要做些配置让它支持. location ~ \.php(.*)$ { root /var/www/nginx/html; fastcgi_pa ...
- 什么是DNS?
什么是DNS域名系统(DNS)是因特网的电话簿.人类通过域名在线访问信息,如nytimes.com或espn.com.Web浏览器通过Internet协议(IP)地址进行交互.DNS将域名转换为IP地 ...
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_06-freemarker基础-遍历map数据
大的map里面有一些小的map 遍历数据模型里面的stuMap <br/> 遍历数据模型中的stuMap(map)数据 <br/> 姓名:${stuMap['stu1'].na ...