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"
  2. Output: 3
  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,然后分别给当前单词左右加上那五对对称数,继续递归调用,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int strobogrammaticInRange(string low, string high) {
  4. int res = ;
  5. for (int i = low.size(); i <= high.size(); ++i) {
  6. find(low, high, "", i, res);
  7. find(low, high, "", i, res);
  8. find(low, high, "", i, res);
  9. find(low, high, "", i, res);
  10. }
  11. return res;
  12. }
  13. void find(string low, string high, string path, int len, int &res) {
  14. if (path.size() >= len) {
  15. if (path.size() != len || (len != && path[] == '')) return;
  16. if ((len == low.size() && path.compare(low) < ) || (len == high.size() && path.compare(high) > )) {
  17. return;
  18. }
  19. ++res;
  20. }
  21. find(low, high, "" + path + "", len, res);
  22. find(low, high, "" + path + "", len, res);
  23. find(low, high, "" + path + "", len, res);
  24. find(low, high, "" + path + "", len, res);
  25. find(low, high, "" + path + "", len, res);
  26. }
  27. };

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

解法二:

  1. class Solution {
  2. public:
  3. int strobogrammaticInRange(string low, string high) {
  4. int res = ;
  5. find(low, high, "", res);
  6. find(low, high, "", res);
  7. find(low, high, "", res);
  8. find(low, high, "", res);
  9. return res;
  10. }
  11. void find(string low, string high, string w, int &res) {
  12. if (w.size() >= low.size() && w.size() <= high.size()) {
  13. if (w.size() == high.size() && w.compare(high) > ) {
  14. return;
  15. }
  16. if (!(w.size() > && w[] == '') && !(w.size() == low.size() && w.compare(low) < )) {
  17. ++res;
  18. }
  19. }
  20. if (w.size() + > high.size()) return;
  21. find(low, high, "" + w + "", res);
  22. find(low, high, "" + w + "", res);
  23. find(low, high, "" + w + "", res);
  24. find(low, high, "" + w + "", res);
  25. find(low, high, "" + w + "", res);
  26. }
  27. };

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. 将Excel表格数据转换成Datatable

    /// <summary> /// 将Excel表格数据转换成Datatable /// </summary> /// <param name="fileUrl ...

  2. Javascript 实现倒计时效果

    代码来自于网上. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  3. Entity Framework 6 中如何获取 EntityTypeConfiguration 的 Edm 信息?(四)

    经过上一篇,里面有测试代码,循环60万次,耗时14秒.本次我们增加缓存来优化它. DbContextExtensions.cs using System; using System.Collectio ...

  4. WebApi接口安全性 接口权限调用、参数防篡改防止恶意调用

    背景介绍 最近使用WebApi开发一套对外接口,主要是数据的外送以及结果回传,接口没什么难度,采用WebApi+EF的架构简单创建一个模板工程,使用template生成一套WebApi接口,去掉put ...

  5. Java内功心法,深入解析面向对象

    什么是对象 对象是系统中用来描述客观事物的一个实体,它是构成系统的一个基本单位.一个对象由一组属性和对这组属性进行操作的一组服务组成. 类的实例化可生成对象,一个对象的生命周期包括三个阶段:生成.使用 ...

  6. linux内核级同步机制--futex

    在面试中关于多线程同步,你必须要思考的问题 一文中,我们知道glibc的pthread_cond_timedwait底层是用linux futex机制实现的. 理想的同步机制应该是没有锁冲突时在用户态 ...

  7. Linux性能调优 | 01 平均负载的理解和分析

    01 uptime命令 通常我们发现系统变慢时,我们都会执行top或者uptime命令,来查看当前系统的负载情况,比如像下面,我执行了uptime,系统返回的了结果. [root@lincoding ...

  8. ASP.NET Core系列:依赖注入

    1. 控制反转(IoC) 控制反转(Inversion of Control,IoC),是面向对象编程中的一种设计原则,用来降低代码之间的耦合度. 1.1 依赖倒置 依赖原则: (1)高层次的模块不应 ...

  9. C# 获取社会统一信用代码

    时间不多,废话少说: 网络请求代码如下: using System; using System.Collections.Generic; using System.Linq; using System ...

  10. 怎样解决非管理员账户添加Notepad++右键菜单的批处理的问题?

    bat脚本如下: @echo off color 1e title 将Notepad++增加到右键菜单(或者去关联) goto :menu :menu cls echo. echo. 1 将Notep ...