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.
For example,
Given n = 2, return ["11","69","88","96"]
.
Hint:
- Try to use recursion and notice that it should recurse with n - 2 instead of n - 1.
链接: http://leetcode.com/problems/strobogrammatic-number-ii/
题解:
求所有长度为n的strobogrammatic number。这题一开始的思路是用DFS + Backtracking。还要处理一些特殊的边界条件,比如n的长度为奇数和偶数,以及最外层不能为两个'0'等等,代码写得很拖沓。 Discuss区有不少好的代码,二刷时一定要思考清楚再进行优化。这里我主要是从中心向两边添加,而discuss区大家大部分都是从两边向中心递归,所以我的代码还需要回溯,不够简练。
Time Complexity - O(2n), Space Complexity - O(n)。
public class Solution {
public List<String> findStrobogrammatic(int n) {
if(n < 1)
return new ArrayList<String>();
List<String> res = new ArrayList<>();
Map<Character, Character> map = new HashMap<>();
map.put('0', '0');
map.put('1', '1');
map.put('6', '9');
map.put('8', '8');
map.put('9', '6'); StringBuilder sb = new StringBuilder();
int position = (n % 2 == 0) ? 0 : 1;
findStrobogrammatic(res, sb, map, n, position); return res;
} private void findStrobogrammatic(List<String> res, StringBuilder sb, Map<Character, Character> map, int n, int position) {
if(sb.length() > n)
return;
if(sb.length() == n) {
res.add(sb.toString());
return;
} if(position == 1) {
for(char c : map.keySet()) {
if(c == '6' || c == '9')
continue;
sb.append(c);
findStrobogrammatic(res, sb, map, n, position + 1);
sb.setLength(0);
}
} else {
for(char c : map.keySet()) {
if(n - sb.length() == 2 && c == '0')
continue;
sb.insert(0, c);
sb.append(map.get(c));
findStrobogrammatic(res, sb, map, n, position + 2);
sb.deleteCharAt(0);
sb.deleteCharAt(sb.length() - 1);
}
}
}
}
二刷:
两种思路:
- 和一刷一样,用dfs + backtracking
- 求一半String的permutation,剪去一些invalid case,再补上另外一半。也就是使用跟267. Palindrome Permutation II类似的方法。
Java:
Reference:
https://leetcode.com/discuss/50412/ac-clean-java-solution
https://leetcode.com/discuss/50377/my-concise-java-solution-using-dfs
https://leetcode.com/discuss/52277/accepted-java-solution-using-recursion
https://leetcode.com/discuss/53144/my-concise-iterative-java-code
https://leetcode.com/discuss/68215/simple-java-solution-without-recursion
247. Strobogrammatic Number II的更多相关文章
- [LeetCode#247] Strobogrammatic Number II
Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...
- 247. Strobogrammatic Number II输出所有对称数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
- [LeetCode] 247. Strobogrammatic Number II 对称数II
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] 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 ...
- [Swift]LeetCode247.对称数 II $ Strobogrammatic Number II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- Strobogrammatic Number II -- LeetCode
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- 246. Strobogrammatic Number 上下对称的数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
随机推荐
- sqlchemy - day3
session 直接上代码,创建表结构,初始化部分数据. from sqlalchemy import create_engine engine = create_engine(" ...
- 如何计算IP地址及CIDR(收藏)
如何计算IP地址及CIDR 一. IP地址概念 IP地址是一个32位的二进制数,它由网络ID和主机ID两部份组成,用来在网络中唯一的标识的一台计算机.网络ID用来标识计算机所处的网段:主 机ID用来标 ...
- FastLoad错误 — RDBMS error 2634
我们来看一下下面这条语句: BEGIN LOADING stu_flERRORFILES error_1, error_2; 如果此时已经存在error_1或error_2表,那么将会报错,信息如 ...
- redis常见性能问题和解决方案?
Master写内存快照,save命令调度rdbSave函数,会阻塞主线程的工作,当快照比较大时对性能影响是非常大的,会间断性暂停服务,所以Master最好不要写内存快照. Master AOF持久化, ...
- 一点简单的关于ASP.NET下载
一点简单的关于ASP.NET下载 个人简单的认为是有两种方法的,第一种就是直接用一个超链接链接到我们要下载的资源就可以了.只是说这个方法会有一点小问题就是,比如像图片或者文本文件这些浏览器是可以自动将 ...
- 【转】 Android经验: proguard 阻碍 webview 正常工作
转自:http://blog.csdn.net/span76/article/details/9065941 WebView 常识 使用 Alert 提供消息 我在页面经常用 Alert 提供消息, ...
- android动态增加控件时控制样式的方法
在学习android的动画时,发现所谓的tween动画只是改变绘制效果并不改变原控件的位置时是颇为失望的,虽然3.0之后已经有了property animation,但是由于要兼容老版本的androi ...
- 【BZOJ】【1026】【SCOI2009】Windy数
数位DP cxlove基础数位DP第三题 = =预处理是个很有用的东西!然后就是分类讨论! /***************************************************** ...
- 导入 github 步骤
https://github.com/dotnet/corefx 如果出现未能找到解决方案的情况,则找项目文件打开,如:
- [转载]C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素
//防止页面多次刷新页面执行 ) { string GetUserName = System.Configuration.ConfigurationSettings.AppSettings[" ...