LeetCode 246/247/248 中心对称数

落爺英雄遲暮 提交于 2020-03-06 02:00:32

目录

LeetCode 246 (Easy):判断一个数是否为中心对称数

LeetCode 247 (Medium):给出长度为 n 的所有中心对称数

LeetCode 248 (Hard):求给定区间内有多少中心对称数


 

  • LeetCode 246 (Easy):判断一个数是否为中心对称数

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.

Examples:

Input:  "69"  Output: true
Input:  "88"  Output: true
Input:  "962" Output: false

解答

两个数字只有这5种情况满足中心对称:11,88, 00, 69,96。可以看做求回文数的一种特殊情况,用双指针。

参考代码:https://www.cnblogs.com/grandyang/p/5196960.html

 

  • LeetCode 247 (Medium):给出长度为 n 的所有中心对称数

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.

解答

题目中给了提示说可以用递归来做,而且提示了递归调用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:   1111, 6119, 8118, 9116,          1691, 6699, 8698, 9696,

            1881, 6889, 8888, 9886,         1961, 6969, 8968, 9966,       1001, 6009, 8008, 9006

可以看到,长度为 i 的数是在长度为 i-2 的所有数的两端加上 11,88,69,96.

有一点要注意:长度为n的数,除了上述规律外,还多4个数 (因为长度为n-2的数两端不能为0)

因此,在中间递归(构造长度为i的数, i<n)的过程中,都需要在数字两端加上00,11,88,69,96。

最后构造长度为n的数时,只在两端加上11,88,69,96。

参考代码:https://www.cnblogs.com/grandyang/p/5200919.html

 

  • LeetCode 248 (Hard):求给定区间内有多少中心对称数

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.

Example:

Input: low = "50", high = "100"
Output: 3 
Explanation: 69, 88, and 96 are three strobogrammatic numbers.

Note: Because the range might be a large number, the low and high numbers are represented as string.

解答

从 n=0 和 n=1 的中心对称数开始,在每个数两端加上00,11,88,69,96。

得到的数num一定也是中心对称数,所以只需要判断num是否在[low, high]内即可( string可用运算符直接比较大小)。

class Solution {
public:
    string low, high;
    int cnt;

    int strobogrammaticInRange(string low, string high) {
        this->low = low;
        this->high = high;
        cnt = 0;
        solve("");
        solve("0");
        solve("1");
        solve("8");
        return cnt;
    }

    void solve(string num) {
        if((num.size() > high.size()) ||                 // 递归边界:若num的长度 > high的长度
           (num.size()==high.size() && num > high))      // 或长度相等,但num比high大 (string可用>直接比较)
            return;

        // 若num在[low,high]内, 就计数
        if(!(num.size()>1 && num[0]=='0') &&             // num开头不是0
           ((num.size() > low.size()) ||
            (num.size()==low.size() && num>=low))        // 且num>=low (string可用>直接比较)
        )
            ++cnt;

        solve("0" + num + "0");
        solve("1" + num + "1");
        solve("8" + num + "8");
        solve("6" + num + "9");
        solve("9" + num + "6");
    }
};

参考代码:https://www.cnblogs.com/grandyang/p/5203228.html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!