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.

Example:

Input:  n = 2
Output: ["11","69","88","96"]

这道题是之前那道 Strobogrammatic Number 的拓展,那道题让我们判断一个数是否是对称数,而这道题让找出长度为n的所有的对称数,这里肯定不能一个数一个数的来判断,那样太不高效了,而且 OJ 肯定也不会答应。题目中给了提示说可以用递归来做,而且提示了递归调用 n-2,而不是 n-1。先来列举一下n为 0,1,2,3,4 的情况:

n = 0:   none

n = 1:   0, 1, 8

n = 2:   11, 69, 88, 96

n = 3:   101, 609, 808, 906, 111, 619, 818, 916, 181, 689, 888, 986

n = 4:   1001, 6009, 8008, 9006, 1111, 6119, 8118, 9116, 1691, 6699, 8698, 9696, 1881, 6889, 8888, 9886, 1961, 6969, 8968, 9966

注意观察 n=0 和 n=2,可以发现后者是在前者的基础上,每个数字的左右增加了 [1 1], [6 9], [8 8], [9 6],看 n=1 和 n=3 更加明显,在0的左右增加 [1 1],变成了 101, 在0的左右增加 [6 9],变成了 609, 在0的左右增加 [8 8],变成了 808, 在0的左右增加 [9 6],变成了 906, 然后在分别在1和8的左右两边加那四组数,实际上是从 m=0 层开始,一层一层往上加的,需要注意的是当加到了n层的时候,左右两边不能加 [0 0],因为0不能出现在两位数及多位数的开头,在中间递归的过程中,需要有在数字左右两边各加上0的那种情况,参见代码如下:

解法一:

class Solution {
public:
vector<string> findStrobogrammatic(int n) {
return find(n, n);
}
vector<string> find(int m, int n) {
if (m == ) return {""};
if (m == ) return {"", "", ""};
vector<string> t = find(m - , n), res;
for (auto a : t) {
if (m != n) res.push_back("" + a + "");
res.push_back("" + a + "");
res.push_back("" + a + "");
res.push_back("" + a + "");
res.push_back("" + a + "");
}
return res;
}
};

这道题还有迭代的解法,感觉也相当的巧妙,需要从奇偶来考虑,奇数赋为 0,1,8,偶数赋为空,如果是奇数,就从 i=3 开始搭建,因为计算 i=3 需要 i=1,而已经初始化了 i=1 的情况,如果是偶数,从 i=2 开始搭建,也已经初始化了 i=0 的情况,所以可以用 for 循环来搭建到 i=n 的情况,思路和递归一样,写法不同而已,参见代码如下:

解法二:

class Solution {
public:
vector<string> findStrobogrammatic(int n) {
vector<string> one{"", "", ""}, two{""}, res = two;
if (n % == ) res = one;
for (int i = (n % ) + ; i <= n; i += ) {
vector<string> t;
for (auto a : res) {
if (i != n) t.push_back("" + a + "");
t.push_back("" + a + "");
t.push_back("" + a + "");
t.push_back("" + a + "");
t.push_back("" + a + "");
}
res = t;
}
return res;
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/247

类似题目:

Strobogrammatic Number

Strobogrammatic Number III

参考资料:

https://leetcode.com/problems/strobogrammatic-number-ii/

https://leetcode.com/problems/strobogrammatic-number-ii/discuss/67280/AC-clean-Java-solution

https://leetcode.com/problems/strobogrammatic-number-ii/discuss/67288/Simple-Java-solution-without-recursion

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Strobogrammatic Number II 对称数之二的更多相关文章

  1. [LeetCode] 247. Strobogrammatic Number II 对称数II

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

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

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

  3. [LeetCode] Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

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

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

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

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

  6. LeetCode Strobogrammatic Number II

    原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...

  7. [LeetCode] 264. Ugly Number II 丑陋数之二

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  8. [LeetCode] Strobogrammatic Number 对称数

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

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

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

随机推荐

  1. 黄聪:如何给wordpress的编辑器添加一个自定义按钮,并且实现插入功能

    1.添加按钮 在  functions.php  文件里面添加下面代码: add_action('media_buttons', 'add_my_media_button'); function ad ...

  2. 『.NET Core CLI工具文档』(六)dotnet 命令

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet command 翻译:dotnet 命令 名称 dotnet -- 运行命令行命令的一般驱动程序 概要 d ...

  3. [.NET Core].NET Core R2安装教程及Hello示例

    前言 前几天.NET Core发布了.NET Core 1.0.1 R2 预览版,之前想着有时间尝试下.NET Core.由于各种原因,就没有初试.刚好,前几天看到.NET Core发布新版本了,决定 ...

  4. JavaWeb_day07_JSP

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day07 JSP 全称 :Java Server P ...

  5. Jmeter3.0发布,版本更新都更新了什么

    Jmeter已发布了3.0,一个大版本的开源测试工具,加入了一些新的特性及软件的改进. Jmeter已隔10年的大版本更新 这是在过去12年里jmeter第一个大版本的更新,jmeter 2.0版本发 ...

  6. java基础1.-------抽象类,抽象方法

    抽象类:抽象类不能实例化,类中的方法必须经过子类的重写实现 类里的方法是public修饰时,子类可重写也可不重写 类的方法是abstract修饰时,方法是抽象方法,子类必须重写该方法 类的方法用fin ...

  7. RPC原来就是Socket——RPC框架到dubbo的服务动态注册,服务路由,负载均衡演化

    序:RPC就是使用socket告诉服务端我要调你的哪一个类的哪一个方法然后获得处理的结果.服务注册和路由就是借助第三方存储介质存储服务信息让服务消费者调用.然我们自己动手从0开始写一个rpc功能以及实 ...

  8. UTFGrid

    UTFGrid UTFGrid is a specification for rasterized interaction data. As of version 1.2, it was remove ...

  9. ButterKnife Zelezny从配置到使用

    插件介绍:ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一键生成.又一神器,完美告别 ...

  10. Android开发案例 - 淘宝商品详情

    所有电商APP的商品详情页面几乎都是和淘宝的一模一样(见下图): 采用上下分页的模式 商品基本参数 & 选购参数在上页展示 商品图文详情等其他信息放在下页展示 知识要点 垂直方向的ViewPa ...