247. Strobogrammatic Number 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.
Example:
Input: n = 2
Output: ["11","69","88","96"]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
当前长度是0或1时需要退出或者返回,所以要新建一个动态数组。
new ArrayList<String>(Arrays.asList(""));
[思维问题]:
不知道怎么控制dfs的长度:
helper(curCount - 2, targetCount)取上一截,然后i取list的size
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
curcount != targetcount时,两端必加0,从中间开始加。
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
helper(curCount - 2, targetCount)取上一截,然后逐渐往外扩展
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
增长回文串也就只有这几种情况:
res.add("0" + s + "0"); res.add("1" + s + "1"); res.add("6" + s + "9"); res.add("8" + s + "8"); res.add("9" + s + "6");
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution { public List<String> findStrobogrammatic(int n) { List<String> result = new ArrayList<String>(); //corner case if (n < 0) return result; //return return getStrobogrammaticNums(n, n); } public List<String> getStrobogrammaticNums(int curCount, int targetCount) { //corner case: n = 0 or 1 if (curCount == 0) return new ArrayList<String>(Arrays.asList("")); if (curCount == 1) return new ArrayList<String>(Arrays.asList("0", "1", "8")); List<String> result = new ArrayList<String>(); List<String> list = getStrobogrammaticNums(curCount - 2, targetCount); //get the new nums into result for (int i = 0; i < list.size(); i++) { String cur = list.get(i); //add 0 from inside if length do not equal if (curCount != targetCount) result.add("0" + cur + "0"); //add other nums result.add("1" + cur + "1"); result.add("6" + cur + "9"); result.add("9" + cur + "6"); result.add("8" + cur + "8"); } //return return result; } }