[抄题]:

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输出所有对称数字的更多相关文章

  1. [LeetCode] 247. Strobogrammatic Number II 对称数II

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. 247. Strobogrammatic Number II

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

  3. [LeetCode#247] Strobogrammatic Number II

    Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...

  4. [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 ...

  5. [LeetCode] Strobogrammatic Number II 对称数之二

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  6. [Swift]LeetCode247.对称数 II $ Strobogrammatic Number II

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  7. LeetCode Strobogrammatic Number II

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...

  8. Strobogrammatic Number II -- LeetCode

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  9. 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 ...

随机推荐

  1. day053 url反向解析图解 模板渲染

    一.语法 两种特殊符号(语法): {{ }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 二.变量 1. 可直接用  {{ 变量名 }} (可调用字符串, 数字 ,列表,字典,对象等) ...

  2. kindle試玩

    Q:試玩感受如何? Pros 按這個版本,沒有燈,在光纖不好的時候看起來很累.但陽光好的時候看起來很爽 重量很輕,比手機邀請很多 看書很容易沉浸下去,這一點比收集好狠毒 Cons 網上說看pdf不大行 ...

  3. MySQL 中,字符串 0 和数字 0 的区别

    我的理解: 用户输入值后,MySQL 根据该字段的数据类型,来转换值.

  4. Python神坑:sum和numpy.sum

    同样的一段代码,在两个python文件里面执行的结果不一样,一个是按照列单位进行sum一个是所有元素进行sum: def distCal(vecA, vecB): return sqrt(sum(po ...

  5. JS 60秒后重发送验证码

    //settime($("#getPhoneCode"),60); function settime($obj, time) { if (time == 0) { $obj.att ...

  6. The query below helps you to locate tables without a primary key:

    SELECT tables.table_schema, tables.table_name, tables.table_rows FROM information_schema.tables LEFT ...

  7. Flask 框架

    装饰器知识回顾 http://www.cnblogs.com/0bug/p/7978595.html 普通装饰器格式: def wrapper(func): def inner(*args, **kw ...

  8. set-----》集合

    1.set 是无序  不重复的序列 2.创建 list = [] dic = {"k1":123} set = {"123","333"}  ...

  9. 干掉hao123劫持浏览器主页

    原因可能是安装某个软件流氓捆绑了IE主页导致的,建议这样尝试: 一.如果安装有三方安全防护类软件,排查流氓软件,建议运行系统自带的Windows Defender或者MSE程序扫描系统. 二.如果有检 ...

  10. [java,2018-02-24] svn检出项目名称不正确

    ,今天从svn中检出项目时发现,检出项目的名称与实际的不相同,如下图: 这才想起来,当时创建项目时是随意起了个test的名称作为项目名,后来觉得能用,就在me中直接把项目名称改掉,提交到了svn.再从 ...