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;
}
}
247. Strobogrammatic Number II输出所有对称数字的更多相关文章
- [LeetCode] 247. Strobogrammatic Number II 对称数II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- 247. Strobogrammatic Number II
题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...
- [LeetCode#247] Strobogrammatic Number II
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [Swift]LeetCode247.对称数 II $ Strobogrammatic Number II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode Strobogrammatic Number II
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...
- Strobogrammatic Number II -- LeetCode
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- binary-tree-level-order-traversal I、II——输出二叉树的数字序列
I Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...
随机推荐
- bootstrap table表格前台分页,点击tab选项,重新刷新表格
近期做项目的时候使用bootstrap表格前台分页,并且有一个tab切换选项,共用一个table,效果如下图,上方是tab选项,下方是table: 在实际实现的时候,在默认状态下,表格翻到了第5页,此 ...
- redis使用get key中文变成十六进制编码
redis-cli 后面加上 --raw 解决中文显示问题 redis-cli -h 127.0.0.1 -p 端口 -a 密码 --raw 不带 --raw 参数: redis-cli -h &g ...
- lvs UDP端口负载均衡配置
! Configuration File for keepalived global_defs { notification_email { test@163.com } notification_e ...
- C++ 是 编程界 的 背锅侠
C++ 是 编程界 的 背锅侠, C++ 背的包袱 之 庞大复杂, 举世瞩目, 令人感动 . C++ 标准 委员会 俨然 已成了一个 职业 . C++ 把 静态编译 体系 发展到 庞大复杂, C ...
- ubuntu18关闭系统自动更新
ubuntu18.04关闭系统自动更新有两个方法:1.修改配置文件 修改配置文件/etc/apt/apt.conf.d/10periodic#0是关闭,1是开启,将所有值改为0vi etc/apt/a ...
- mnist全连接层网络权值可视化
一.数据准备 网络结构:lenet_lr.prototxt 训练好的模型:lenet_lr_iter_10000.caffemodel 下载地址:链接:https://pan.baidu.com/s/ ...
- [ZZ] 基于Matlab的标记分水岭分割算法
基于Matlab的标记分水岭分割算法 http://blog.sina.com.cn/s/blog_725866260100rz7x.html 1 综述 Separating touching obj ...
- 【浅色】最强Win7 x64评测
[浅色]最强Win7 x64评测 [浅色]最强Win7 x86 & x64 | WINOS https://www.winos.me/archives/789.htmlESD671MB,安装后 ...
- docker network基础
前面介绍了nginx与php两个容器间是如何进行通信的: [root@docker ~]# docker run -d --name=php -v /www:/usr/local/nginx/html ...
- Activiti的25张表
Activiti流程引擎的运行,背后需要数据库的25张表支持,这25张表, 主要是在流程运行过程中,记录存储一些参与流程的用户主体,以及流程定义的存储,流程执行时候的一些信息,以及流程的历史信息等. ...