[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.
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
类似题目:
参考资料:
https://leetcode.com/problems/strobogrammatic-number-iii/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Strobogrammatic Number III 对称数之三的更多相关文章
- [LeetCode] 248. Strobogrammatic Number III 对称数之三
		A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ... 
- [LeetCode] 248. Strobogrammatic Number III 对称数III
		A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ... 
- [LeetCode] Strobogrammatic Number II 对称数之二
		A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ... 
- [LeetCode] 247. Strobogrammatic Number II 对称数II
		A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ... 
- Leetcode: Strobogrammatic Number III
		A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ... 
- [LeetCode] Strobogrammatic Number 对称数
		A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ... 
- LeetCode Strobogrammatic Number II
		原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ... 
- LeetCode Strobogrammatic Number
		原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ... 
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
		Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ... 
随机推荐
- windows环境tomcat8配置Solr5.5.1
			前言 前前后后接触Solr有一个多月了,想趁着学习Solr顺便把java拾起来.我分别用4.X和5.X版本在windows环境下用jetty的方式.tomcat部署的方式自己搭建了一把.其中从4.x到 ... 
- Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化
			本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解 ... 
- 微信小程序的应用及信息整合,都放到这里了
			微信小程序终于开始公测了,这篇文章也终于可以发布了. 这篇文章可以说是微信小程序系列三部曲最后一篇.8 月份,小程序推出前,我写了<别开发 app 了>详细阐述了为什么创业应该放弃原生 a ... 
- C#得到某月最后一天晚上23:59:59和某月第一天00:00:00
			项目需求: 某学校订单截止操作时间的上一个月最后一天晚上23:59:59 为止所有支付的订单统计: 代码: /// <summary> /// 通过学校和截止时间得到订单 /// < ... 
- Rafy 框架 - 使用 SqlTree 查询
			本文介绍如何使用 Rafy 框架中的 Sql Tree 查询: 除了开发者常用的 Linq 查询,Rafy 框架还提供了 Sql 语法树的方式来进行查询. 这种查询方式下,开发者不需要直接编写真正的 ... 
- 基于Metronic的Bootstrap开发框架经验总结(14)--条码和二维码的生成及打印处理
			在很多项目里面,对条形码和二维码的生成和打印也是一种很常见的操作,在Web项目里面,我们可以利用JS生成条形码和二维码的组件有很多.本文引入两个比较广泛使用的JS组件,用来处理条形码和二维码的生成处理 ... 
- expect基本使用方法
			参考: http://www.cnblogs.com/lzrabbit/p/4298794.html expect是linux系统中可以和子进程进行交互的一个命令,使用它可以做一些自动化工作.pyth ... 
- spring aop对service层日志和异常的处理
			1.aop是什么 AOP是Aspect Oriented Programming的缩写,意思是面向切面编程,与OOP(Object Oriented Programming)面向对象编程对等,都是一种 ... 
- 纯css3圆形从中心向四周扩散动画效果
			查看效果:http://hovertree.com/texiao/css3/37/ 先来个简单的示例,例如: @keyframes hovertreemove{from {top:30px;}to { ... 
- Flash Professional 报错 TypeError: Error #1034: 强制转换类型失败:无法将 xxxx@zzzz 转换为 yyy
			通常是因为xxx yyy 两个不同链接名的元件 使用了同一个属性名 
