原题链接在这里: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. $《第一行代码:Android》读书笔记——第3章 UI基础

    (一)Android常用控件及简单用法 1.如下图: 2.补充: (1)margin:外边距:padding:内边距. (2)gravity:子元素的位置:layout_gravity:子元素在父元素 ...

  2. $2015 武汉森果公司web后端开发实习日记----书写是为了更好的思考

    找暑期实习,3月份分别投了百度和腾讯的实习简历,都止步于笔试,总结的主要原因有两点:基础知识不扎实,缺乏项目经验.后来到拉勾网等网站上寻找实习,看了很多家,都还是处于观望状态.后来参加了武汉实习吧在大 ...

  3. JAVA中的Token 基于Token的身份验证

    最近在做项目开始,涉及到服务器与安卓之间的接口开发,在此开发过程中发现了安卓与一般浏览器不同,安卓在每次发送请求的时候并不会带上上一次请求的SessionId,导致服务器每次接收安卓发送的请求访问时都 ...

  4. 登陆weblogic后页面控制台卡主

    输入http://localhost:7001/console进入控制页面,能登陆进去,但是登陆进去后页面就马上卡死,可以看到页面头部,其余都显示不出来. 重启后启动访问,能够正常进入,关闭weblo ...

  5. Android编译系统环境过程初始化分析【转】

    本文转载自:http://blog.csdn.net/luoshengyang/article/details/18928789 Android源代码在编译之前,要先对编译环境进行初始化,其中最主要就 ...

  6. [Codechef November Challenge 2012] Arithmetic Progressions

    题意:给定一个序列,求多少个三元组满足ai+ak=2*aj(i<j<k). 题解:原来叉姐的讲义上有啊..完全忘掉了.. 首先这个式子很明显是一个卷积.我们有了FFT的思路.但是肯定不能每 ...

  7. Qt QTreeWidget节点的添加+双击响应+删除详解

    转自: http://www.cnblogs.com/Romi/archive/2012/08/08/2628163.html 承接该文http://www.cnblogs.com/Romi/arch ...

  8. R语言常用语法总结

    ## 1. 数据输入 ##a$b # 数据框中的变量a = 15 # 赋值a <- 15 # 赋值a = c(1,2,3,4,5) # 数组(向量)b = a[1] # 数组下标,从1开始b = ...

  9. 解决:git warning: LF will be replaced by CRLF in xxxx

    一. git add -A报错 在利用git add -A添加文件时,意外的发现报错了 报错信息中: LF:Line Feed 换行 CRLF:Carriage Return Line Feed  回 ...

  10. 51nod 1686 二分+离散化

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 1686 第K大区间 基准时间限制:1 秒 空间限制:131072 ...