Skip to content

Commit b2df7bd

Browse files
committed
Add solutions for #12, #341
1 parent a51bee3 commit b2df7bd

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package org.sean.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/** 12. Integer to Roman */
7+
public class IntRomanConverter {
8+
public String intToRoman(int num) {
9+
int base = 1;
10+
StringBuilder builder = new StringBuilder();
11+
List<String> list = new ArrayList<>();
12+
while (num > 0) {
13+
int dlt = num % 10;
14+
15+
list.add(basedToStr(dlt, base));
16+
17+
base *= 10;
18+
num = num / 10;
19+
}
20+
21+
int size = list.size();
22+
for (int i = size - 1; i >= 0; i--) {
23+
builder.append(list.get(i));
24+
}
25+
26+
return builder.toString();
27+
}
28+
29+
private String nChar(String ch, int count) {
30+
StringBuilder builder = new StringBuilder();
31+
for (int i = 0; i < count; i++) {
32+
builder.append(ch);
33+
}
34+
return builder.toString();
35+
}
36+
37+
String[] midChars1 = new String[] {"I", "V", "X"};
38+
String[] midChars10 = new String[] {"X", "L", "C"};
39+
String[] midChars100 = new String[] {"C", "D", "M"};
40+
String[] midChars1000 = new String[] {"M", "", ""};
41+
42+
private String basedToStr(int i, int base) {
43+
String[] chars = new String[3];
44+
switch (base) {
45+
case 1:
46+
chars = midChars1;
47+
break;
48+
case 10:
49+
chars = midChars10;
50+
break;
51+
case 100:
52+
chars = midChars100;
53+
break;
54+
case 1000:
55+
chars = midChars1000;
56+
break;
57+
}
58+
59+
if (i > 0 && i <= 3) return nChar(chars[0], i);
60+
else if (i == 4) return chars[0] + chars[1]; // "XL";
61+
else if (i == 5) return chars[1];
62+
else if (i > 5 && i < 9) {
63+
return chars[1] + nChar(chars[0], i - 5);
64+
} else if (i == 9) {
65+
return chars[0] + chars[2];
66+
}
67+
return "";
68+
}
69+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.sean.recursive;
2+
3+
import java.util.Iterator;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
7+
/***
8+
* 341. Flatten Nested List Iterator
9+
*/
10+
public class NestedIterator implements Iterator<Integer> {
11+
12+
private final LinkedList<Integer> flatList = new LinkedList<>();
13+
Iterator<Integer> iterator;
14+
15+
public NestedIterator(List<NestedInteger> nestedList) {
16+
for (NestedInteger ni : nestedList) {
17+
extracted(ni);
18+
}
19+
iterator = flatList.iterator();
20+
}
21+
22+
private void extracted(NestedInteger ni) {
23+
if (ni.isInteger()) {
24+
flatList.add(ni.getInteger());
25+
} else {
26+
for (NestedInteger e : ni.getList()) {
27+
extracted(e);
28+
}
29+
}
30+
}
31+
32+
@Override
33+
public Integer next() {
34+
return iterator.next();
35+
}
36+
37+
@Override
38+
public boolean hasNext() {
39+
return iterator.hasNext();
40+
}
41+
42+
public interface NestedInteger {
43+
44+
// @return true if this NestedInteger holds a single integer, rather than a nested list.
45+
public boolean isInteger();
46+
47+
// @return the single integer that this NestedInteger holds, if it holds a single integer
48+
// Return null if this NestedInteger holds a nested list
49+
public Integer getInteger();
50+
51+
// @return the nested list that this NestedInteger holds, if it holds a nested list
52+
// Return empty list if this NestedInteger holds a single integer
53+
public List<NestedInteger> getList();
54+
}
55+
}

0 commit comments

Comments
 (0)