LeetCode Strobogrammatic Number II
原题链接在这里: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;
}
}
LeetCode Strobogrammatic Number II的更多相关文章
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
- Leetcode: Strobogrammatic Number III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [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] 247. Strobogrammatic Number II 对称数II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode#247] Strobogrammatic Number II
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- Strobogrammatic Number II -- LeetCode
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
随机推荐
- 完数[HDU1406]
完数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- linux 安装eclipse 和cdt
这个东西说起来简单,但是经历了无数次到失败,终于还是安装完成了. 最早到时候我下载了eclipse和cdt让后安装,安装完成以后,无法运行和编译程序 后来我学到了一个牛逼的命令yum 这个命令会帮助我 ...
- MyIsam和InnoDB的区别
个人见解: 1.最主要的差别就是Innodb 支持事务处理与外键和行级锁.而MyISAM不支持 所以有用到事务处理和外键的,要用Innodb 2. InnoDB 中不保存表的具体行数,也就是说,执行s ...
- [R]R语言里的异常处理与错误控制
之前一直只是在写小程序脚本工具,几乎不会对异常和错误进行控制和处理. 随着脚本结构和逻辑更复杂,脚本输出结果的准确性验证困难,同时已发布脚本的维护也变得困难.所以也开始考虑引入异常处理和测试工具的事情 ...
- Node.js学习
1. 下载 网址:https://nodejs.org/download/ 2. 添加express框架 如下图,运行Node.js command prompt 在命令行中输入:npm instal ...
- Java 动态代理
被代理的接口特点: 1. 不能有重复的接口,以避免动态代理类代码生成时的编译错误. 2. 这些接口对于类装载器必须可见,否则类装载器将无法链接它们,将会导致类定义失败. 3. 需被代理的所有非 pub ...
- Leetcode Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Codeforces Round #195 (Div. 2) A. Vasily the Bear and Triangle
水题,注意数据范围即可 #include <iostream> #include <algorithm> #include <utility> using name ...
- URAL 1346. Intervals of Monotonicity(DP)
题目链接 错误的贪了一下,然后D了两下就过了.注意是不上升和不下降..不是上升和下降.. #include <cstring> #include <cstdio> #inclu ...
- Python拾忆--多线程的socket服务器
阳光明媚的午后,想想最近要开始从写Java到写Python了,就随手打开电脑来体验一下Python与Java之间的不同吧~ 记得我还在上大二的时候,那个时候才开始学Java,最感兴趣的就是Java书最 ...