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.
Example:
Input: n = 2
Output: ["11","69","88","96"]
思路:不知道和回溯有啥关系?感觉是去要去加。
cur = ans;
然后根据cur,给ans里加东西
这是用iterative的方法去加,还蛮特殊的
for (;n > 1; n -= 2) {
cur = ans;
ans = new ArrayList<String>();
for (String i : cur) {
if (n > 3) ans.add('0' + i + '0');
如果长度 > 3, 就可以有'010'这种被0夹的数字
class Solution {
public List<String> findStrobogrammatic(int n) {
List<String> cur, ans;
ans = new ArrayList<String>((n & 1) == 0 ? Arrays.asList("") : Arrays.asList("0", "1", "8"));
if (n < 2) return ans;
for (; n > 1; n -= 2) {
cur = ans;
ans = new ArrayList<String>();
for (String currentString : cur) {
if (n > 3)
ans.add('0' + currentString + '0');
ans.add('1' + currentString + '1');
ans.add('6' + currentString + '9');
ans.add('9' + currentString + '6');
ans.add('8' + currentString + '8');
}
}
return ans;
}
}