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的更多相关文章

  1. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. [LeetCode] 248. Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  3. [LeetCode#247] Strobogrammatic Number II

    Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...

  4. [LeetCode] Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  5. [LeetCode] 246. Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  6. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  7. 246. Strobogrammatic Number 上下对称的数字

    [抄题]: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at u ...

  8. [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 ...

  9. [LeetCode] Strobogrammatic Number II 对称数之二

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

随机推荐

  1. 逆向破解之160个CrackMe —— 006

    CrackMe —— 006 160 CrackMe 是比较适合新手学习逆向破解的CrackMe的一个集合一共160个待逆向破解的程序 CrackMe:它们都是一些公开给别人尝试破解的小程序,制作 c ...

  2. C#启动计算器并设计算器为活动窗口

    启动计算器,并获取焦点 using System; using System.Runtime.InteropServices; namespace ConsoleApplication3 { clas ...

  3. ms08067 分析与利用

    分析 漏洞位于 NetpwPathCanonicalize 函数里面,这个函数的作用在于处理路径中的 ..\ 和 .\ 信息.该函数声明如下: DWORD NetpwPathCanonicalize( ...

  4. Python开发笔记之-浮点数传输

    操作系统 : CentOS7.3.1611_x64 gcc版本 :4.8.5 Python 版本 : 2.7.5 思路如下 : 1.将浮点数a通过内存拷贝,赋值给相同字节的整型数据b: 2.将b转换为 ...

  5. Maven模块化搭建总结

    1.Maven插件在eclipse的安装 windows——>preferences——>Maven——>installations——>add——>installati ...

  6. 机房断电,导致xfs文件系统损坏

    记一次机房断电,导致xfs文件系统损坏处理方法 挂载时报以下错误: mount: mount /dev/sdb on /dev/sdb failed: Structure needs cleaning ...

  7. django--远程mysql

    settings.py中配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'ttsx', # 数据 ...

  8. go 学习 (四):接口 & 方法

     接口声明 // 接口声明 语法:接口是一个 函数签名 的集合,函数签名(函数的声明,不包括实现) type interfaceName interface { method1(param param ...

  9. 在WinDbg里使用MEX调试扩展

    简介 针对WinDbg的MEX调试扩展可以帮助您简化常见的调试器任务,并为调试器提供强大的文本筛选功能.此扩展被Microsoft支持工程师广泛用于解决流程应用程序的故障. 下载&安装 下载m ...

  10. telegraf 学习三 telegra inputs.net_response + smtp2http+ grafana 进行tcp服务状态监控

    以下演示一个简单的使用telegra inputs.net_response 进行tcp 服务状态的监控,统计集成grafana 的alert 为了方便使用了一个smtp2http 的服务,对于htt ...