leetcode-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.
Example:
1 | Input: low = "50", high = "100" |
Note:
Because the range might be a large number, the low and high numbers are represented as string.
分析
思路1: 从low开始采用 {0, 1, 6, 8, 9} 进制压缩的方式来获取下一个待判断的数字直到high。这种做法会超时。
思路2: 通过增加成对数字的方式直接生成对应位数的“中心对称数”,然后再判断是不是在规定的范围内。corner case,奇数时的情况以及第一位不可以是1的情况。
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/05/leetcode-248/
License: 知识共享署名-非商业性使用 4.0 国际许可协议