java输出数字对称序列_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(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的代码] :

[潜台词] :

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

classSolution {public List findStrobogrammatic(intn) {

List result = new ArrayList();//corner case

if (n < 0) returnresult;//return

returngetStrobogrammaticNums(n, n);

}public List getStrobogrammaticNums(int curCount, inttargetCount) {//corner case: n = 0 or 1

if (curCount == 0) return new ArrayList(Arrays.asList(""));if (curCount == 1) return new ArrayList(Arrays.asList("0", "1", "8"));

List result = new ArrayList();

List 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

returnresult;

}

}

View Code

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值