[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:
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] 248. Strobogrammatic Number III 对称数之三的更多相关文章
- [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 III 对称数之三
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 II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [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 ...
- 248. Strobogrammatic Number III
题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...
- [LeetCode] 246. Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 137. Single Number II 单独数 II
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- LeetCode 246. Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
随机推荐
- tomcat正常运行一段时间后,突然访问不了项目了
前言 我将项目部署在tomcat服务器上,本来都是好好的,输入网站地址就能访问:但是第二天一早去就会发现网站访问提示404,文件无法找到:我就很懵了. 排查 1.我是用的是chrome浏览器,所以尝试 ...
- LeetCode 151:给定一个字符串,逐个翻转字符串中的每个单词 Reverse Words in a String
公众号:爱写bug(ID:icodebugs) 翻转字符串里的单词 Given an input string, reverse the string word by word. 示例 1: 输入: ...
- Leetcode算法题 7. Reverse Integer2
7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...
- 一位IT民工的十年风雨历程
距离2020年只有30天了,转眼毕业快10年. 回首自己,已三十有三,中年危机. 古人云三十而立,我却还在测试途中摸爬滚打. 创业,自由职业永远是一个梦,白日梦. 焦虑.迷茫.看不到希望. 这两天一场 ...
- go中interface空指针不为nil判断方法
interface空指针不为nil 当把一个空指针对象赋值给一个interface后,再判断!= nil就不再成立了 代码如下 package main import "fmt" ...
- Python【day 13】内置函数01
1.python3.6.2 一共有 68个内置函数2.分成6个大类 1.反射相关-4个 2.面向对象相关-9个 3.作用域相关--2个 1.globlas() #注意:最后是s,复数形式 查看全局作用 ...
- JVM源码分析之MetaspaceSize和MaxMetaspaceSize的区别
JVM加载类的时候,需要记录类的元数据,这些数据会保存在一个单独的内存区域内,在Java 7里,这个空间被称为永久代(Permgen),在Java 8里,使用元空间(Metaspace)代替了永久代. ...
- SpringBoot 整合Mybatis操作数据库
1.引入依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> ...
- 通过premake生成vs工程文件
visual studio的工程视图,在引用外部目录时非常麻烦,这时候可以使用premake一键生成工程文件,自动配置好工程的各种属性,还有目录的组织结构. 示例:如下是一个c++的外部目录结构,我们 ...
- c# 第19节 Arraylist数组
本节内容: 1:ArrayList是什么 2:ArrayList数组的添加 3:ArrayList的方法 4:ArrayList 的删除 4:ArrayList 的遍历与查找 1:ArrayList是 ...