原题链接在这里: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();
*/

类似String Compression.

LeetCode Design Compressed String Iterator的更多相关文章

  1. [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  2. LeetCode 604. Design Compressed String Iterator (设计压缩字符迭代器)$

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  3. 【LeetCode】604. Design Compressed String Iterator 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...

  4. Design Compressed String Iterator

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  5. [leetcode-604-Design Compressed String Iterator]

    Design and implement a data structure for a compressed string iterator. It should support the follow ...

  6. [LeetCode] Design Snake Game 设计贪吃蛇游戏

    Design a Snake game that is played on a device with screen size = width x height. Play the game onli ...

  7. [LeetCode] Design Search Autocomplete System 设计搜索自动补全系统

    Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...

  8. [LeetCode] Design TinyURL 设计精简URL地址

    Note: For the coding companion problem, please see: Encode and Decode TinyURL. How would you design ...

  9. [LeetCode] Design Log Storage System 设计日志存储系统

    You are given several logs that each log contains a unique id and timestamp. Timestamp is a string t ...

随机推荐

  1. iOS 11系列 - Xcode 9新特性

    Xcode 9最近刚刚发布,带来了一系列不错的新特性,可以更好的帮助到开发者完成开发工作. Xcode Runtime Tool Xcode 9中有许多Runtime Tool可以帮助开发者找到代码错 ...

  2. SM3算法

    /* * sm3.h * * 为使此算法兼容32位.64位下Linux或Windows系统, * 选择 int 来表示 32 位整数. * 消息长度最大限定为 2**32 - 1(单位:比特), * ...

  3. python tkinter组件学习

    http://blog.csdn.net/pfm685757/article/details/50162567

  4. 【CodeChef】Factorial(n!末尾0的个数)

    The most important part of a GSM network is so called Base Transceiver Station (BTS). These transcei ...

  5. Shell编程之循环控制及状态返回值

    1.break.continue.exit.return的对比 break.continue在条件语句和循环语句中用于控制程序走向: exit用于终止所有语句并退出当前脚本,还可以返回上一次程序或命令 ...

  6. java.lang.ClassFormatError Duplicate field name&signature in class file XXXXXX【转】

    本文转载自:https://blog.csdn.net/ylchou/article/details/7739742 2012-7-5 15:06:25org.apache.catalina.core ...

  7. Docker Compose yml

    Wordpress + Mysql version: '3' services: db: image: mysql:latest volumes: - db_data:/var/lib/mysql e ...

  8. Luogu-2495 [SDOI2011]消耗战

    虚树第一题 对于每次询问的点建立一棵虚树,然后在树上DP,一个点的答案就是这个点的父边切断的代价与所有儿子切断的代价去最小值,当然如果这个节点是资源点则必须切父边 注意在虚树上一条边的代价应该是中间所 ...

  9. hive学习6

    将查询结果集写入另一个表中的时候报了这个错,Dynamic partition strict mode requires at least one static partition column. T ...

  10. select2 使用

    <link href="/plugin/select2/css/select2.min.css" rel="stylesheet" /> <s ...