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 ...
随机推荐
- 基于Python的密码生成程序的优化
近期刚刚组织完内部的Python基础培训.GUI的开发培训,之后布置的作业是两人一组,利用前面所写的一些模块做一些小软件. 具体就是模拟Advanced Password Generator这个软件的 ...
- Png图片的透明部分穿透测试
private void Window_MouseMove(object sender, MouseEventArgs e){ NavBtnList.Clear(); Point mou ...
- oracle 存储过程编辑 卡死
一.可用SYS登录, 二.查锁session_ID查找存储过程OPERATIONDATA_IMP被哪些session锁住而无法编译select * FROM dba_ddl_locks where n ...
- matlab实现贝塞尔曲线绘图pdf查看
贝塞尔曲线绘图方法: %Program 3.7 Freehand Draw Program Using Bezier Splines %Click in Matlab figure window to ...
- net windows Kafka
net windows Kafka 安装与使用入门(入门笔记) 完整解决方案请参考: Setting Up and Running Apache Kafka on Windows OS 在环境搭建 ...
- Daily Scrum 11.6
摘要:在本次meeting时,所有代码的修改工作已经接近尾声,接下来是进行的就是单元测试以及进行alpha版本的改进.本次的Task列表如下: Task列表 出席人员 Today's Task Tom ...
- C++(MFC)编程一些注意事项
一·书写问题 1.括号:左右大括号最好都放在左侧,这样可以很清楚大括号的看清配对情况以及作用域,便于检查也不易出错. 2.强制转换:强制转换表达式时一定要加括号,否则可能只转换了表达式中的单个量,可能 ...
- 【转】edittext设置点击链接
I put some ClickableSpan in EditText, unfortunately, that spans are still not clickable. When I clic ...
- Codeforces Round #327 (Div. 2) E. Three States
题目链接: 题目 E. Three States time limit per test:5 seconds memory limit per test:512 megabytes 问题描述 The ...
- Unit Test Generator