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 ...
随机推荐
- vue学习笔记——组件的优化
Vue 应用性能优化指南 1 给组件定义name,然后在同级目录新建index文件: import Count from './count.vue' export Count; 2 优化大数据的列表 ...
- PythonStudy——函数的分类 Classification of functions
# PEP8:python写代码的规范 def fn(n1, n2): """ 函数的文档注释 :param n1: 第一个数 :param n2: 第二个数 :retu ...
- PythonStudy——python中如何使输出不换行
1.在python 3.x版本中,使用print(,end="") 可使输出不换行, 例如:
- Django学习笔记之URL与视图
视图 视图一般都写在app的views.py中.并且视图的第一个参数永远都是request(一个HttpRequest)对象.这个对象存储了这个http请求的所有信息,其中包括携带的参数以及一些头部信 ...
- C# 调用打印机 打印 Excel
打印 Excel 模板 大体思路,通过NPOI操作Excel文件,通过Spire将Excel转成图片,将图片传给系统打印. Spire是收费工具,在微软库中下载Free版本. #region 打印所用 ...
- nginx+keeplived+tomcat
1,宣告操作系统版本,nginx,java,tomcat,keeplived版本 操作系统 用途 VIP IP地址 软件版本 CentOS 7.3 mini NTP服务器 无 192.168.197. ...
- 【python】*与**
1. 加了星号(*)的变量名会存放所有未命名的变量参数,不能存放dict,否则报错. 如: 1 def multiple(arg, *args): 2 print "arg: ", ...
- json 数据在textarea中显示的时候,切换 beauty和ugly模式
转化为beauty模式 var jsonText = $('#json').val(); $('#json').val(JSON.stringify(JSON.parse(jsonText), nul ...
- oninput、onchange与onpropertychange事件的区别, 与input输入框实时检测
这几天项目着急,同时也学到好多以前没有接触过的知识.oninput.onchange与onpropertychange事件的区别, 与input输入框实时检测 onchange事件只在键盘或者鼠标操作 ...
- PyQt5 入门
换了VSCODE开发,感觉比sublime好点,可能是由于第三版老弹框烦人吧.VSCODE看着也挺好看的. 学习 PyQt5 中文教程 0. 安装完之后错误 pip 安装了 pyqt5 from Py ...