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 lowand high numbers are represented as string.

 

这道题是之前那两道 Strobogrammatic Number II 和 Strobogrammatic Number 的拓展,又增加了难度,让找给定范围内的对称数的个数,我们当然不能一个一个的判断是不是对称数,也不能直接每个长度调用第二道中的方法,保存所有的对称数,然后再统计个数,这样 OJ 会提示内存超过允许的范围,所以这里的解法是基于第二道的基础上,不保存所有的结果,而是在递归中直接计数,根据之前的分析,需要初始化 n=0 和 n=1 的情况,然后在其基础上进行递归,递归的长度 len 从 low 到 high 之间遍历,然后看当前单词长度有没有达到 len,如果达到了,首先要去掉开头是0的多位数,然后去掉长度和 low 相同但小于 low 的数,和长度和 high 相同但大于 high 的数,然后结果自增1,然后分别给当前单词左右加上那五对对称数,继续递归调用,参见代码如下:

解法一:

class Solution {
public:
int strobogrammaticInRange(string low, string high) {
int res = ;
for (int i = low.size(); i <= high.size(); ++i) {
find(low, high, "", i, res);
find(low, high, "", i, res);
find(low, high, "", i, res);
find(low, high, "", i, res);
}
return res;
}
void find(string low, string high, string path, int len, int &res) {
if (path.size() >= len) {
if (path.size() != len || (len != && path[] == '')) return;
if ((len == low.size() && path.compare(low) < ) || (len == high.size() && path.compare(high) > )) {
return;
}
++res;
}
find(low, high, "" + path + "", len, res);
find(low, high, "" + path + "", len, res);
find(low, high, "" + path + "", len, res);
find(low, high, "" + path + "", len, res);
find(low, high, "" + path + "", len, res);
}
};

上述代码可以稍微优化一下,得到如下的代码:

解法二:

class Solution {
public:
int strobogrammaticInRange(string low, string high) {
int res = ;
find(low, high, "", res);
find(low, high, "", res);
find(low, high, "", res);
find(low, high, "", res);
return res;
}
void find(string low, string high, string w, int &res) {
if (w.size() >= low.size() && w.size() <= high.size()) {
if (w.size() == high.size() && w.compare(high) > ) {
return;
}
if (!(w.size() > && w[] == '') && !(w.size() == low.size() && w.compare(low) < )) {
++res;
}
}
if (w.size() + > high.size()) return;
find(low, high, "" + w + "", res);
find(low, high, "" + w + "", res);
find(low, high, "" + w + "", res);
find(low, high, "" + w + "", res);
find(low, high, "" + w + "", res);
}
};

Github 同步地址:

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

类似题目:

Strobogrammatic Number II

Strobogrammatic Number

参考资料:

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

https://leetcode.com/problems/strobogrammatic-number-iii/discuss/67431/My-Java-solution-easy-to-understand

https://leetcode.com/problems/strobogrammatic-number-iii/discuss/67406/Clear-Java-AC-solution-using-Strobogrammatic-Number-II-method

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

[LeetCode] 248. Strobogrammatic Number III 对称数之三的更多相关文章

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

    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] 247. Strobogrammatic Number II 对称数II

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

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

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

  5. [LeetCode] 260. Single Number III 单独数 III

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  6. 248. Strobogrammatic Number III

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

  7. [LeetCode] 246. Strobogrammatic Number 对称数

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

  8. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  9. LeetCode 246. Strobogrammatic Number

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

随机推荐

  1. Dubbo从入门到实战:入门篇

    很多时候,其实我们使用这个技术的时候,可能都是因为项目需要,所以,我们就用了,但是,至于为什么我们需要用到这个技术,可能自身并不是很了解的,但是,其实了解技术的来由及背景知识,对于理解一项技术还是有帮 ...

  2. 如何在 Knative 中部署 WebSocket 和 gRPC 服务?

    作者 | 冬岛  阿里云容器平台工程师 导读:虽然说 Knative 默认就支持 WebSocket 和 gRPC,但在使用中会发现,有时想要把自己的 WebSocket 或 gRPC 部署到 Kna ...

  3. Rust从入门到放弃(1)—— hello,world

    安装及环境配置 特点:安全,性能,并发 rust源配置 RLS安装 cargo rust管理工具,该工具可以愉快方便的管理rust工程 #!/bin/bash mkdir learn cd learn ...

  4. Spring Cloud 新一代Web框架微服务翘楚(一)

    序言 springcloud是微服务架构的集大成者,将一系列优秀的组件进行了整合.基于springboot构建,对我们熟悉spring的程序员来说,上手比较容易. 通过一些简单的注解,我们就可以快速的 ...

  5. AdminLTE 3.0发布了

    在11月2日,作者正式发布了AdminLTE 3.0版本.该版本基于Bootstrap 4.x.使用Bootstrap 4.x的小伙伴可以愉快的使用AdminLTE. Github AdminLTE是 ...

  6. python3之int类的常用方法练习

    int类的常用方法练习 #coding:utf-8 #int类的常用方法 num1 = 18 num2 = -15 #查询创建num1所用的类 print(type(num1)) #num1占用的最小 ...

  7. GitBash上传项目出现[fatal: remote origin already exists.]问题解决方案

    问题截图如下: 当出现这个问题时,不要慌,只需要输入 git remote rm origin 就可以解决,输入完前面的命令后再次 输入 git remote add origin https://. ...

  8. gsoap生成webservice调用客户端接口

    1.下载gsoap2.8 2.运行 wsdl2h.exe -o XXX.h XXX.wsdl wsdl文件可以是本地文件,也可以是服务器的wsdl,比如http://192.168.0.122:333 ...

  9. iFrmae_HTML

    iframe(HTML框架) <iframe src="URL"></iframe> 该URL指向的页面 会显示在当前页面的一个窗口上,默认大小为 widt ...

  10. python基础-内置函数 isinstance() 与 issubclass()

    面向对象编程之内置函数 isinstance() 与 issubclass() isinstance():判断一个对象是否是另一个类的实例.返回bool值,True表示是,False表示不是. 语法: ...