[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 ...
随机推荐
- Tarjan算法分解强连通分量(附详细参考文章)
Tarjan算法分解强连通分量 算法思路: 算法通过dfs遍历整个连通分量,并在遍历过程中给每个点打上两个记号:一个是时间戳,即首次访问到节点i的时刻,另一个是节点u的某一个祖先被访问的最早时刻. 时 ...
- CentOS7安装Pycharm
1. 进入官网:https://www.jetbrains.com/pycharm/2. 点击下载3. 直接安装:tar zxvf ***.tar.gz4. 建立软连接:sudo ln -s /you ...
- destoon下动态链接301到伪静态(ngnix)
分享一个destoon6.0/7.0下动态链接301到伪静态上面,实现权重提升. if ($request_uri ~* "^/index.php\?itemid=(\d+)&mod ...
- modbus系列文章—汇总
请移步我博客园的网站 基本上是自己的原创,不是网上抄来抄去的,有很多干货,希望一边整理,一边修改-有不对的地方多多指教. https://www.cnblogs.com/CodeWorkerLiMin ...
- OLED液晶屏幕(3)串口读取文字并分割
https://blog.csdn.net/iracer/article/details/50334041 String comdata = ""; void setup() { ...
- LeetCode 731. My Calendar II
原题链接在这里:https://leetcode.com/problems/my-calendar-ii/ 题目: Implement a MyCalendarTwo class to store y ...
- 小程序支付及H5支付前端代码小结
小程序支付和H5支付前端都不需要引入其他的js , 只需要后台将相关的参数 ( timeStamp: '', nonceStr: '', package: '', signType: 'MD5', p ...
- Windows用户模式调试内部组件
简介 允许用户模式调试工作的内部机制很少得到充分的解释.更糟糕的是,这些机制在Windows XP中已经发生了根本性的变化,当许多支持被重新编写时,还通过将ntdll中的大多数例程作为本地API的一部 ...
- cube.js 学习 cube docker-compose 运行
cube.js 官方为我们也提供了backeng 部署的模型,为了测试方便以下是一个使用docker-compose 运行的demo 项目是一个集成gitbase 的demo,实际可以按照自己的项目修 ...
- 2019-8-26 LinkedHashMap 转 List [java.util.LinkedHashMap cannot be cast to com.zq.dataservice.bean.Index]
java.util.LinkedHashMap cannot be cast to com.zq.dataservice.bean.Index 上述错误是在做一个趋势投资demo时遇到的. 说的是链式 ...