?LeetCode刷題實戰(zhàn)247:中心對稱數(shù)II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Find all strobogrammatic numbers that are of length = n.
示例
示例 :
輸入: n = 2
輸出: ["11","69","88","96"]
解題
public class Solution {
char[] table = {'0', '1', '8', '6', '9'};
List<String> res;
public List<String> findStrobogrammatic(int n) {
res = new ArrayList<String>();
build(n, "");
return res;
}
public void build(int n, String tmp){
if(n == tmp.length()){
res.add(tmp);
return;
}
boolean last = n - tmp.length() == 1;
for(int i = 0; i < table.length; i++){
char c = table[i];
// 第一個字符不能為'0',但n=1除外。只插入一個字符時不能插入'6'和'9'
if((n != 1 && tmp.length() == 0 && c == '0') || (last && (c == '6' || c == '9'))){
continue;
}
StringBuilder newTmp = new StringBuilder(tmp);
// 插入字符c和它的對應字符
append(last, c, newTmp);
build(n, newTmp.toString());
}
}
public void append(boolean last, char c, StringBuilder sb){
if(c == '6'){
sb.insert(sb.length()/2, "69");
} else if(c == '9'){
sb.insert(sb.length()/2, "96");
} else {
sb.insert(sb.length()/2, last ? c : ""+c+c);
}
}
}
