Skip to content

Commit 11c4cd3

Browse files
add 604
1 parent d0d34c7 commit 11c4cd3

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Your ideas/fixes/algorithms are more than welcome!
2424
|609|[Find Duplicate File in System](https://leetcode.com/problems/find-duplicate-file-in-system/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_609.java) | O(n*x) (x is the average length of each string) |O(n*x) | Medium | HashMap
2525
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_606.java) | O(n) |O(n) | Easy | Tree, Recursion
2626
|605|[Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_605.java) | O(n) |O(1) | Easy | Array
27+
|604|[Design Compressed String Iterator](https://leetcode.com/problems/design-compressed-string-iterator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_604.java) | O(n) |O(n) | Easy |Design, String
2728
|600|[Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_600.java) | O(log2(max_int) = 32) | O(log2(max_int) = 32) | Hard | Bit Manipulation, DP
2829
|599|[Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_599.java) | O(max(m,n))|O(max(m,n)) | Easy | HashMap
2930
|598|[Range Addition II](https://leetcode.com/problems/range-addition-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_598.java) | O(n) (n is the number of operations) |O(1) | Easy |
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
/**
7+
* 604. Design Compressed String Iterator
8+
*
9+
* Design and implement a data structure for a compressed string iterator. It should support the following operations: next and hasNext.
10+
The given compressed string will be in the form of each letter followed by a positive integer representing
11+
the number of this letter existing in the original uncompressed string.
12+
13+
next() - if the original string still has uncompressed characters, return the next letter; Otherwise return a white space.
14+
hasNext() - Judge whether there is any letter needs to be uncompressed.
15+
16+
Note:
17+
Please remember to RESET your class variables declared in StringIterator,
18+
as static/class variables are persisted across multiple test cases. Please see here for more details.
19+
20+
Example:
21+
22+
StringIterator iterator = new StringIterator("L1e2t1C1o1d1e1");
23+
24+
iterator.next(); // return 'L'
25+
iterator.next(); // return 'e'
26+
iterator.next(); // return 'e'
27+
iterator.next(); // return 't'
28+
iterator.next(); // return 'C'
29+
iterator.next(); // return 'o'
30+
iterator.next(); // return 'd'
31+
iterator.hasNext(); // return true
32+
iterator.next(); // return 'e'
33+
iterator.hasNext(); // return false
34+
iterator.next(); // return ' '
35+
36+
*/
37+
public class _604 {
38+
public static class StringIterator {
39+
40+
Deque<int[]> deque;
41+
public StringIterator(String compressedString) {
42+
deque = new ArrayDeque<>();
43+
int len = compressedString.length();
44+
int i = 0;
45+
while (i < len) {
46+
int j = i + 1;
47+
while (j < len && Character.isDigit(compressedString.charAt(j))) j++;
48+
deque.addLast(new int[]{compressedString.charAt(i) - 'A', Integer.parseInt(compressedString.substring(i+1, j))});
49+
i = j;
50+
}
51+
}
52+
53+
public char next() {
54+
if (deque.isEmpty()) return ' ';
55+
int[] top = deque.peek();
56+
top[1]--;
57+
if (top[1] == 0) deque.pollFirst();
58+
return (char) ('A' + top[0]);
59+
}
60+
61+
public boolean hasNext() {
62+
return !deque.isEmpty();
63+
}
64+
}
65+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._604;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
/**
9+
* Created by stevesun on 6/10/17.
10+
*/
11+
public class _604Test {
12+
private static _604.StringIterator test;
13+
14+
@Test
15+
public void test1(){
16+
test = new _604.StringIterator("L1e2t1C1o1d1e1");
17+
System.out.println(test.hasNext());
18+
System.out.println(test.next());
19+
System.out.println(test.next());
20+
System.out.println(test.next());
21+
System.out.println(test.next());
22+
System.out.println(test.next());
23+
System.out.println(test.next());
24+
System.out.println(test.next());
25+
System.out.println(test.next());
26+
System.out.println(test.hasNext());
27+
}
28+
29+
@Test
30+
public void test2(){
31+
test = new _604.StringIterator("L10e2t1C1o1d1e11");
32+
System.out.println(test.hasNext());
33+
System.out.println(test.next());
34+
System.out.println(test.next());
35+
System.out.println(test.next());
36+
System.out.println(test.next());
37+
System.out.println(test.next());
38+
System.out.println(test.next());
39+
System.out.println(test.next());
40+
System.out.println(test.next());
41+
System.out.println(test.next());
42+
System.out.println(test.next());
43+
System.out.println(test.next());
44+
System.out.println(test.next());
45+
System.out.println(test.next());
46+
System.out.println(test.next());
47+
System.out.println(test.next());
48+
System.out.println(test.next());
49+
System.out.println(test.next());
50+
System.out.println(test.next());
51+
System.out.println(test.next());
52+
System.out.println(test.next());
53+
System.out.println(test.next());
54+
System.out.println(test.next());
55+
System.out.println(test.next());
56+
System.out.println(test.next());
57+
System.out.println(test.next());
58+
System.out.println(test.next());
59+
System.out.println(test.hasNext());
60+
}
61+
62+
@Test
63+
public void test3(){
64+
test = new _604.StringIterator("x6");
65+
System.out.println(test.hasNext());
66+
System.out.println(test.next());
67+
System.out.println(test.next());
68+
System.out.println(test.next());
69+
System.out.println(test.next());
70+
System.out.println(test.next());
71+
System.out.println(test.next());
72+
System.out.println(test.hasNext());
73+
System.out.println(test.next());
74+
System.out.println(test.hasNext());
75+
}
76+
77+
@Test
78+
public void test4(){
79+
test = new _604.StringIterator("X15D18V8");
80+
System.out.println(test.hasNext());
81+
System.out.println(test.next());
82+
System.out.println(test.next());
83+
System.out.println(test.next());
84+
System.out.println(test.next());
85+
System.out.println(test.next());
86+
System.out.println(test.next());
87+
assertEquals(true, test.hasNext());
88+
System.out.println(test.next());
89+
assertEquals(true, test.hasNext());
90+
}
91+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy