Strobogrammatic Number I

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

Write a function to determine if a number is strobogrammatic. The number is represented as a string.

For example, the numbers "69", "88", and "818" are all strobogrammatic.

From:https://segmentfault.com/a/1190000003787462

翻转后对称的数就那么几个,我们可以根据这个建立一个映射关系:8->8, 0->0, 1->1, 6->9, 9->6,然后从两边向中间检查对应位置的两个字母是否有映射关系就行了。比如619,先判断6和9是有映射的,然后1和自己又是映射,所以是对称数。

 public class Solution {
public boolean isStrobogrammatic(String num) {
for (int i = ; i <= num.length() / ; i++) {
char a = num.charAt(i);
char b = num.charAt(num.length() - - i);
if (!isValid(a, b)) {
return false;
}
}
return true;
}
private boolean isValid(char c, char b) {
switch (c) {
case '':
return b == '';
case '':
return b == '';
case '':
return b == '';
case '':
return b == '';
case '':
return b == '';
default:
return false;
}
}
}

Strobogrammatic Number II

From: http://www.cnblogs.com/grandyang/p/5200919.html

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"].

这道题是之前那道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;
}
};

Strobogrammatic Number的更多相关文章

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

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

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

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

  3. [LeetCode] Strobogrammatic Number 对称数

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

  4. LeetCode Strobogrammatic Number II

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

  5. LeetCode Strobogrammatic Number

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

  6. Leetcode: Strobogrammatic Number III

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

  7. 248. Strobogrammatic Number III

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

  8. 247. Strobogrammatic Number II

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

  9. 246. Strobogrammatic Number

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

随机推荐

  1. vs2013使用C#6.0

    安装 nuget Microsoft.Net.Compilers

  2. 分布式消息队列RocketMQ部署

    一.RocketMQ简介: RocketMQ是一款分布式.队列模型的消息中间件,具有以下特点: 1.支持严格的消息顺序: 2.支持Topic与Queue两种模式: 3.亿级消息堆积能力: 4.比较友好 ...

  3. javascript Date 总结

    构造函数 Date 对象的构造函数有以下4种: (1)var variable = new Date(); (2)var variable = new Date(millisenconds); (3) ...

  4. 多线程 GET

    iOS中多线程的实现 方案 简介 语言 线程生命周期 使用频率 pthread 一套通用的多线程API 适用于 Unix / Linux / Windows 等系统 跨平台\可移植 使用难度大 C 程 ...

  5. Promise编程基础

    (一) Promise基础     所谓Promise,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消 ...

  6. SQLSERVER2008R2正确使用索引

    T1表 10000000万条数据,(插入时间36分钟,count(*)查询19秒,空间占用670M左右) 1.真正充分的利用索引比如like '张%' 就是符合SARG(符合扫描参数)标准而like ...

  7. MEF搜索范围

    MEF对扩展组件的查找范围通常有三个: AssemblyCatalog:从某个程序集中查找. ApplicationCatalog:在应用程序所在的目录下查找. DirectoryCatalog:在某 ...

  8. CSS3的REM设置字体大小

    在Web中使用什么单位来定义页面的字体大小,至今天为止都还在激烈的争论着,有人说PX做为单位好,有人说EM优点多,还有人在说百分比方便,以至于出现了CSS Font-Size: em vs. px v ...

  9. Python命令 (if __name__=="__main__":)

    1. 语法 1.以#号开头的语句是注释 2.请务必注意,Python程序是大小写敏感的,如果写错了大小写,程序会报错. 3.按照约定俗成的管理,应该始终坚持使用4个空格的缩进. 4.当语句以冒号:结尾 ...

  10. Mac Pro 安装 Homebrew 软件包管理工具

    Linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案,Red hat有 yum,Ubuntu有 apt-get. Mac os 中没有类似的东东,不过有第三方库支持 ...