[LeetCode] 247. Strobogrammatic Number II 对称数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.
246. Strobogrammatic Number 的变形,那道题让我们判断一个数是否是对称数,而这道题让我们找出长度为n的所有的对称数。提示用递归来做,调用n-2,而不是n-1。
DFS,每次n-2,一层一层的迭代,到最后一层,n = 0 或者 n = 1 停止,backtracking加对称数字。n = 0时加"', n = 1时,加0或1或8,其它层每次加能组成对称数字对的一种组合,注意最外边一层不能是0。
Java:
public class Solution {
public List<String> findStrobogrammatic(int n) {
return helper(n, n);
} List<String> helper(int n, int m) {
if (n == 0) return new ArrayList<String>(Arrays.asList(""));
if (n == 1) return new ArrayList<String>(Arrays.asList("0", "1", "8")); List<String> list = helper(n - 2, m);
List<String> res = new ArrayList<String>(); for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if (n != m) res.add("0" + s + "0");
res.add("1" + s + "1");
res.add("6" + s + "9");
res.add("8" + s + "8");
res.add("9" + s + "6");
} return res;
}
}
Python:
class Solution:
lookup = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'} def findStrobogrammatic(self, n):
return self.findStrobogrammaticRecu(n, n) def findStrobogrammaticRecu(self, n, k):
if k == 0:
return ['']
elif k == 1:
return ['0', '1', '8'] result = []
for num in self.findStrobogrammaticRecu(n, k - 2):
for key, val in self.lookup.iteritems():
if n != k or key != '0':
result.append(key + num + val) return result
C++:
class Solution {
public:
vector<string> findStrobogrammatic(int n) {
return find(n, n);
}
vector<string> find(int m, int n) {
if (m == 0) return {""};
if (m == 1) return {"0", "1", "8"};
vector<string> t = find(m - 2, n), res;
for (auto a : t) {
if (m != n) res.push_back("0" + a + "0");
res.push_back("1" + a + "1");
res.push_back("6" + a + "9");
res.push_back("8" + a + "8");
res.push_back("9" + a + "6");
}
return res;
}
};
类似题目:
[LeetCode] 246. Strobogrammatic Number 对称数
[LeetCode] 248. Strobogrammatic Number III 对称数III
All LeetCode Questions List 题目汇总
[LeetCode] 247. Strobogrammatic Number II 对称数II的更多相关文章
- [LeetCode] 248. Strobogrammatic Number III 对称数III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 248. Strobogrammatic Number III 对称数之三
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 ...
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 246. Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 137. Single Number II 单独数 II
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- 246. Strobogrammatic Number 上下对称的数字
[抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...
- [LeetCode] 260. Single Number III 单独数 III
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
随机推荐
- 动态规划——背包问题python实现(01背包、完全背包、多重背包)
目录 01背包问题 完全背包问题 多重背包问题 参考: 背包九讲--哔哩哔哩 背包九讲 01背包问题 01背包问题 描述: 有N件物品和一个容量为V的背包. 第i件物品的体积是vi,价值是wi. 求解 ...
- Python开发应用之-SQL 建索引的几大原则
SQL 建索引的几大原则: 最左前缀匹配原则,非常重要的原则,mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹配,比如a = 1 and b = ...
- pyinstaller打包多个py文件和去除cmd黑框
1.打包多个py文件并且去除cmd黑框 格式:pyinstaller.exe -F 路径\文件名.py空格路径\文件名.py空格--noconsole pyinstaller.exe -F ui.py ...
- 10-Flutter移动电商实战-使用FlutterSwiper制作轮播效果
1.引入flutter_swiper插件 flutter最强大的siwiper, 多种布局方式,无限轮播,Android和IOS双端适配. 好牛X得介绍,一般敢用“最”的一般都是神级大神,看到这个介绍 ...
- 关于redash 自定义可视化以及query runner 开发的几篇文章
以下是几篇关于如如何编码redash 自定义可视化插件以及query runner 的连接,很有借鉴价值 参考连接 https://discuss.redash.io/t/how-to-create- ...
- 洛谷 P4071 [SDOI2016]排列计数 题解
P4071 [SDOI2016]排列计数 题目描述 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳 ...
- 《RabbitMQ 实战》读书笔记
MQ的好处: 1.业务上接口(系统扩展性变强) 2.性能提升(同步变异步,效率提高,还方便做负载均衡) 3.技术兼容(可以连接各种不同语言的系统,作为粘合剂) 读书笔记: 1.消息队列的应用场景:系统 ...
- 阿里云域名注册详解与Github绑定
关注我,每天都有优质技术文章推送,工作,学习累了的时候放松一下自己. 本篇文章同步微信公众号 欢迎大家关注我的微信公众号:「醉翁猫咪」 今教一篇如何注册域名,拥有自己的域名是不是很爽呢?答案是是的,那 ...
- vsftp 匿名访问设置设置
本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/vsftpd_configuration vsftpd (very ...
- delphi开第二个进程报错cannot create file editorlineends.ttr
网上说问题是windows系统补丁造成的,解决办法有卸补丁.装插件,还有自己搞个bat启动. 在网上看到最好的一个办法是: 把这个文件EditorLineEnds.ttr的后缀改为ttf,然后安装这个 ...