LeetCode Design Compressed String Iterator
原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/
题目:
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 ' '
题解:
用 index 标记当前s位置. 用count计数之前char出现的次数. 当count--到0时继续向后移动 index.
Time Complexity: hasNext(), O(1). next(), O(n). n= compressedString.length().
Space: O(1).
AC Java:
class StringIterator {
String s;
int index;
char cur;
int count;
public StringIterator(String compressedString) {
s = compressedString;
index = 0;
count = 0;
}
public char next() {
if(count != 0){
count--;
return cur;
}
if(index >= s.length()){
return ' ';
}
cur = s.charAt(index++);
int endIndex = index;
while(endIndex<s.length() && Character.isDigit(s.charAt(endIndex))){
endIndex++;
}
count = Integer.valueOf(s.substring(index, endIndex));
index = endIndex;
count--;
return cur;
}
public boolean hasNext() {
return index != s.length() || count != 0;
}
}
/**
* Your StringIterator object will be instantiated and called as such:
* StringIterator obj = new StringIterator(compressedString);
* char param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/
LeetCode Design Compressed String Iterator的更多相关文章
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...
- Design Compressed String Iterator
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- [leetcode-604-Design Compressed String Iterator]
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- [LeetCode] Design Snake Game 设计贪吃蛇游戏
Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...
- [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- [LeetCode] Design TinyURL 设计精简URL地址
Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...
- [LeetCode] Design Log Storage System 设计日志存储系统
You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...
随机推荐
- $《第一行代码:Android》读书笔记——第9章 服务
(一)Service简介 服务适合执行那种不需要和用户交互而且还要长期运行的任务.所有的服务代码都是默认运行在主线程中,需要在服务内部手动添加子线程,在子线程中执行耗时任务. (二)线程 1.线程 ...
- 【leetcode刷题笔记】Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- STM32探秘 之FSMC
源:STM32探秘 之FSMC STM32 FSMC总线深入研究
- 跨平台移动开发_Android 平台使用 PhoneGap 方法
PhoneGap 下载地址http://phonegap.com/install/ 1.打开 Eclipse,在文件菜单下面点击 创建> Android Application Proj ...
- 超酷Loading进度条
在线演示 本地下载
- 堆栈在linux内存中的使用
链接:https://www.zhihu.com/question/57013926/answer/151506606 1.Linux 内核中使用 task_struct 作为进程描述符,该结构定义在 ...
- 1028: [JSOI2007]麻将
1028: [JSOI2007]麻将 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2638 Solved: 1168[Submit][Status] ...
- html里id和name的异同
id与name的作用,作为标签的标识符,基本上是一样的. name是老方法,id是在name基础上发明的,比name“现代化”一点,用的范围广一点 <...>中的name原来(刚发明时)就 ...
- 03_01_基本操作_增(insert)
1. 1.1.创建表 create table employee( id number primary key, name varchar2(64) not null, email varchar2( ...
- JDBC方式操作数据库
1.读取配置文件,我将配置信息写入配置文件(.properties)中,方便后期数据库变更后或者其他项目使用,扩展性好些,尽量避免硬编码. driver=oracle.jdbc.driver.Orac ...