248 LeetCode Java: Different Ways to Add Parentheses – Hard
Problem:
Thoughts:
Solutions:
public class Solution {
public int strobogrammaticInRange(String low, String high) {
if (low.length() > high.length() || (low.length() == high.length() && low.compareTo(high) == 1)) {
return 0;
}
int n1 = low.length();
int n2 = high.length();
int count = 0;
if (n1 == n2) {
List<String> tmp = findStrobogrammatic(n1);
for (String s:tmp) {
if (s.compareTo(low) >= 0 && s.compareTo(high) <= 0) {
count ++;
}
}
return count;
}
List<String> tmp = findStrobogrammatic(n1);
for (String s:tmp) {
if (s.compareTo(low) >= 0) {
count ++;
}
}
tmp = findStrobogrammatic(n2);
for (String s:tmp) {
if (s.compareTo(high) <= 0) {
count ++;
}
}
for (int i = n1 + 1; i < n2; i ++) {
int size = findStrobogrammatic(i).size();
count += size;
}
return count;
}
//from solution of version II problem
private List<String> findStrobogrammatic(int n) {
HashMap<Character, Character> map = new HashMap<Character, Character>();
map.put('1','1');
map.put('0','0');
map.put('8','8');
map.put('6','9');
map.put('9','6');
List<String> result = new LinkedList<String>();
if (n <= 0) {
return result;
}
result.add("1");
result.add("0");
result.add("8");
if (n == 1) {
return result;
}
if (n % 2 != 0) {
n --;
}
else {
result.clear();
result.add("");
}
while (n > 1) {
List<String> newresult = new LinkedList<String>();
for (String s:result) {
for (Character c:map.keySet()) {
if (n != 2 || c != '0') {
newresult.add(c + s + map.get(c));
}
}
}
result = newresult;
n = n - 2;
}
return result;
}
}Last updated