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. torch_11_BEGAN

    BEGAN: 创新: 1.不是考虑生成图片与真实图片之间的真实的分布,而是估计分布的误差的分布之间的差距. 2.G,D的能力平衡提出了一种均衡的概念 3.提供了一种超参数,这超参数可以在图片的多样性和 ...

  2. 初探云原生应用管理(一): Helm 与 App Hub

      ​ 系列介绍:初探云原生应用管理系列是介绍如何用云原生技术来构建.测试.部署.和管理应用的内容专辑.做这个系列的初衷是为了推广云原生应用管理的最佳实践,以及传播开源标准和知识.通过这个系列,希望帮 ...

  3. Dev控件使用CheckedListBoxControl获取items.count为0 的解决方法

    CheckedListBoxControl,我使用DataSource属性,给其绑定了一个List对象.界面显示都挺正常的,当若干个项的复选框被选中的后,它的checkedListBoxControl ...

  4. 将多个sass文件合并到一个文件中

    将多个sass文件合并到一个文件中 应用场景:制作angular npm包的时候,定义的一些全局样式,自定义主题色这类的情况下,多个scss文件会要合并成一个文件并写到dist文件里,发布到仓库中. ...

  5. EFLAGS寄存器(标志寄存器)

    这篇文章不是从0开始的,前面还有一些汇编基础指令以及进制,我都没写,时间问题,还是今天空闲,我才想补一下博文,后面我陆续会把前面知识点渐渐补上.我不会重0基础讲起,中间会以.汇编.C.C++交叉的形式 ...

  6. 10.InfluxDB-InfluxQL基础语法教程--OFFSET 和SOFFSET子句

    本文翻译自官网,官网地址:(https://docs.influxdata.com/influxdb/v1.7/query_language/data_exploration/) OFFSET 和SO ...

  7. jmeter连接并使用mysql数据

    一.下载数据库驱动,放至D:\apache-jmeter-2.13\lib\ext目录下 二.打开jmeter,右键添加->配置文件->JDBC Connection Configurat ...

  8. Transformer模型---encoder

    一.简介 论文链接:<Attention is all you need> 由google团队在2017年发表于NIPS,Transformer 是一种新的.基于 attention 机制 ...

  9. 剖析linux内核中的宏-----------offsetof

    offsetof用于计算TYPE结构体中MEMBER成员的偏移位置. #ifndef offsetof#define offsetof(TYPE, MEMBER) ((size_t) &((T ...

  10. Flask框架入门(一)

    Flask诞生于2010年,是Armin ronacher(人名)用 Python 语言基于 Werkzeug 工具箱编写的轻量级Web开发框架. Flask 本身相当于一个内核,其他几乎所有的功能都 ...