原题链接在这里:https://leetcode.com/problems/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.

For example,
Given n = 2, return ["11","69","88","96"].

题解:

base case 分奇数偶数两种. 拿到base case, 在每一个 string 前后加上"1", "1"; "8","8";...等等添加到新的res中去.

corner case是若是string长度大于1, "0"不能加在最前面. 所以用 m != n搞定.

Time Complexity: O(5^n), exponential.

Space: O(n/2  + 5^(n/2)), n/2层stack, 5^(n/2)是当前base的大小.

AC Java:

 public class Solution {
public List<String> findStrobogrammatic(int n) {
return findHelper(n, n);
} private List<String> findHelper(int cur, int max){
if(cur == 0){
return new ArrayList<String>(Arrays.asList(""));
}
if(cur == 1){
return new ArrayList<String>(Arrays.asList("0", "1", "8"));
} List<String> res = new ArrayList<String>();
List<String> base = findHelper(cur-2, max);
for(String s : base){
if(cur != max){
res.add("0" + s + "0");
}
res.add("1" + s + "1");
res.add("8" + s + "8");
res.add("6" + s + "9");
res.add("9" + s + "6");
}
return res;
}
}

跟上Strobogrammatic Number III.

类似Strobogrammatic Number.

最新文章

  1. socket编程学习step1
  2. WebStrom 10 注册码(转)
  3. Dom终
  4. Recyclerview使用系列教材
  5. 美团在Redis上踩过的一些坑-目录(本人非美团)(转)
  6. jQuery 的 live() 方法对 hover 事件的处理
  7. JavaScript 字符串函数 之查找字符方法(一)
  8. server宕机监控、检測、报警程序(139绑定手机短信报警)monitor_down.sh
  9. 基于 Aliexpress API 的小程序 : 批量 Copy 产品到不同的店铺
  10. React翻译官网文档之JSX
  11. MySQL视图了解
  12. javaScript基础的基础
  13. 拉勾网爬取全国python职位并数据分析薪资,工作经验,学历等信息
  14. VS下.net开发常用扩展、配置
  15. vue的定位
  16. mongodb查看操作记录方法以及用户添加删除权限修改密码
  17. C++之 模板Template的使用
  18. annovar积累
  19. Java中对话框的弹出
  20. Zend Studio使用综述

热门文章

  1. 【python游戏编程之旅】第六篇---pygame中的Sprite(精灵)模块和加载动画
  2. hadoop datanode 挂机恢复后,多复制的块删除的问题
  3. java线程详解
  4. &lt;构建之法&gt;第十一章、十二章有感
  5. 江西理工大学南昌校区acm选拔赛题解
  6. C++11 feature: move constructor
  7. CDOJ 435 (SCOI 2011) 糖果 Label:差分约束系统
  8. 神奇的莫比乌斯带(mobius)
  9. URAL 1501. Sense of Beauty(记忆化搜索)
  10. GridView合并表头、多重表头(转)