LeetCode 604. 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 ' '
题目标签:Design
Java Solution:
Runtime beats 86.01%
完成日期:07/10/2017
关键词:Design
关键点:用两个list和一个cursor来找到char和对应char的count
public class StringIterator
{
ArrayList<Integer> counts;
ArrayList<Character> letters;
int cursor_index = 0; public StringIterator(String compressedString)
{
counts = new ArrayList<>();
letters = new ArrayList<>(); for(int i=0; i<compressedString.length(); i++)
{
// if find a letter
if(Character.isLetter(compressedString.charAt(i)))
letters.add(compressedString.charAt(i)); // if find a digit
else if(Character.isDigit(compressedString.charAt(i)))
{
int end = i+1;
// find the next non-digit char within the length
while(end < compressedString.length() &&
Character.isDigit(compressedString.charAt(end)))
end++; counts.add(Integer.parseInt(compressedString.substring(i, end))); i = end - 1;
}
}
} public char next()
{
char c; // meaning string is finished
if(!hasNext())
return ' '; c = letters.get(cursor_index);
counts.set(cursor_index, counts.get(cursor_index) - 1); if(counts.get(cursor_index) == 0)
cursor_index++; return c;
} public boolean hasNext()
{
// if string is finished
if(cursor_index > counts.size() - 1 )
return false;
else // if string is not finished
return true;
}
} /**
* 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();
*/
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 604. 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 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护当前字符和次数 日期 题目地址:https://l ...
- LeetCode Design Compressed String Iterator
原题链接在这里:https://leetcode.com/problems/design-compressed-string-iterator/description/ 题目: Design and ...
- 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] 642. Design Search Autocomplete System 设计搜索自动补全系统
Design a search autocomplete system for a search engine. Users may input a sentence (at least one wo ...
- ✡ leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] 341. Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
- [leetcode]173. Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
随机推荐
- temp-成都农商行路径
route add 30.3.4.0 mask 255.255.255.0 30.3.12.254 route add 30.3.12.0 mask 255.255.255.0 30.3.12.254 ...
- webservice06#异常#Handler
1, 异常处理 package com.yangw.soap.service; public class UserException extends Exception { public UserEx ...
- hdfs存取文件机制
HDFS和HBase是Hadoop中两种主要的存储文件系统,两者适用的场景不同,HDFS适用于大文件存储,HBASE适用于大量小文件存储.本文主要讲解HDFS文件系统中客户端是如何从Hadoop集群中 ...
- Openlayers系列(一)关于地图投影的理解
背景 近期开发以MongoDB为基础的分布式地理数据管理平台系统,被要求做一个简单的demo给客户进行演示.于是笔者便打算向数据库中存储一部分瓦片数据,写一个简单的存取服务器,使用Openlayers ...
- Java中迭代器Iterator的使用
Java集合类中Map接口下的相关类并没有像Collection接口的相关类一样实现get()方法,因此在要实现遍历输出的场景中没法直接用get()方法来取得对象中的数据,但Java本身提供了另一种遍 ...
- ThinkPHP中:多个项目共享同一个session问题
使用ThinkPHP3.1.3版本的session时,多个项目同时调试会使得一维数组式的session不够用,导致在A项目登录后台后,在B项目就不用登录后台就可以进入后台操作了. 问题在于他们都调用同 ...
- Qt实现基本QMainWindow主窗口程序
这个实验用Qt实现基本QMainWindow主窗口 先上实验效果图 打开一个文件,读取文件类容 详细步骤: 1.打开Qt creator新建MainWindow工程 右键工程名添加新文件,mai ...
- 基于MyBatis3.0.6的基本操作介绍
每 一 个 MyBatis 的 应 用 程 序 都 以 一 个 SqlSessionFactory 对 象 的 实 例 为 核 心 .SqlSessionFactory本身是由SqlSessionFa ...
- 【POJ】3090 Visible Lattice Points(欧拉函数)
Visible Lattice Points Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7705 Accepted: ...
- 原创:TSP问题解决方案-----禁忌搜索算法C实现
本文着重于算法的实现,对于理论部分可自行查看有关资料可以简略参考该博文:http://blog.csdn.net/u013007900/article/details/50379135 本文代码部分基 ...