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 ...
随机推荐
- .NET SDK和下载
http://blogs.msdn.com/b/dotnet/p/dotnet_sdks.aspx .NET SDK和下载 您可以通过下载.NET框架针对包和软件开发工具包,并使用它们与Visual ...
- python操作mysql之pymysql
pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文测试python版本:2.7.11.mys ...
- 基于Python的密码生成程序的优化
近期刚刚组织完内部的Python基础培训.GUI的开发培训,之后布置的作业是两人一组,利用前面所写的一些模块做一些小软件. 具体就是模拟Advanced Password Generator这个软件的 ...
- HTTP 错误405.0 - Method Not Allowed
在使用IIS发布网页的时候,出现下图这个问题 那么出现这个问题的原因可能有很多种,但是我目前是这么解决的,下面说一下我个人解决这个问题的方案,仅供参考. 首先,以管理员的身份启动cmd 如果电脑是64 ...
- c++迭代器(iterator)详解
1. 迭代器(iterator)是一中检查容器内元素并遍历元素的数据类型.(1) 每种容器类型都定义了自己的迭代器类型,如vector:vector<int>::iterator iter ...
- Ubuntu下Apache+php+mysql网站架设详解
目录 1 基础 2 安装 2.1 安装LAMP 2.2 图形化管理软件(可选) 2.2.1 安装webmin 2.2.2 安装phpmyadmin 3 配置文件路径 3.1 常用命令 3.2 配置ap ...
- winrar 压缩文件方法
问题描述: 我要一些大的文件进行压缩,看了网上有很多类拟的写法.但在我这都存在两个问题. 1.就是他们都是通过注册表找到rar的exe目录.我安装好winrar后,虽然注册表有,但那个目录一直报一个错 ...
- Jquery post 传递数组给asp.net mvc方法
以批量删除数据为例 做批量删除会需要传递要删除的数据ID数组 function RemoveLog(){ var postModel=[]; //遍历复选框获取要删除的数据ID 存放到数组中 $( ...
- android开发之socket快传文件以及消息返回
应用场景: 两台android机器:一台自建wifi热点,另一台搜索到,连接该wifi热点.之后再通过socket传递消息,文件等,当服务器端接收到消息之后会返回对应的应答消息: 注意点:接收到消息之 ...
- 所有的代码生成器都是浮云,如果可以用aspx文件作为模板
首先申明:标题中的如果是可以去掉的. 想写这篇文章很长时间了,一来是跟大家分享一下,别浪费时间在写代码生成器上面了,什么CodeSmith,XXCodeGenerator等等,都是浮云:二来想跟大家交 ...