Strobogrammatic Number

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和自己又是映射,所以是对称数。

 1 public class Solution {
 2     public boolean isStrobogrammatic(String num) {
 3         for (int i = 0; i <= num.length() / 2; i++) {
 4             char a = num.charAt(i);
 5             char b = num.charAt(num.length() - 1 - i);
 6             if (!isValid(a, b)) {
 7                 return false;
 8             }
 9         }
10         return true;
11     }
12     private boolean isValid(char c, char b) {
13         switch (c) {
14             case ‘1‘:
15                 return b == ‘1‘;
16             case ‘6‘:
17                 return b == ‘9‘;
18             case ‘9‘:
19                 return b == ‘6‘;
20             case ‘8‘:
21                 return b == ‘8‘;
22                 case ‘0‘:
23                 return b == ‘0‘;
24             default:
25                 return false;
26         }
27     }
28 }

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的那种情况,参见代码如下:

 1 class Solution {
 2 public:
 3     vector<string> findStrobogrammatic(int n) {
 4         return find(n, n);
 5     }
 6     vector<string> find(int m, int n) {
 7         if (m == 0) return {""};
 8         if (m == 1) return {"0", "1", "8"};
 9         vector<string> t = find(m - 2, n), res;
10         for (auto a : t) {
11             if (m != n) res.push_back("0" + a + "0");
12             res.push_back("1" + a + "1");
13             res.push_back("6" + a + "9");
14             res.push_back("8" + a + "8");
15             res.push_back("9" + a + "6");
16         }
17         return res;
18     }
19 };
时间: 2025-01-12 16:09:59

Strobogrammatic Number的相关文章

[LeetCode] Strobogrammatic Number 对称数

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", "

LeetCode Strobogrammatic Number II

原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-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

LeetCode Strobogrammatic Number

原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: 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 r

248. Strobogrammatic Number III

题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. For example,Given low = "5

Strobogrammatic Number II -- LeetCode

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"

[LeetCode] Strobogrammatic Number III 对称数之三

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high. For example,Given low = "50&qu

[LeetCode#247] Strobogrammatic Number II

Problem: 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",&quo

LeetCode &quot;Strobogrammatic Number III&quot;

It can be solved based on the code from "Strobogrammatic Number II". The idea is pretty straight forward - binary search the boundaries. class Solution { int go_cnt(int n, bool bInner) { int ret = 0; switch (n) { case 0: ret = 1; // ""

Strobogrammatic Number -- LeetCode

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", "