[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 ...
随机推荐
- Linux和windows下修改tomcat内存
原文地址:https://www.cnblogs.com/wdpnodecodes/p/8036333.html 由于服务器上放的tomcat太多,造成内存溢出. 常见的内存溢出有以下两种: java ...
- 【shell脚本】不停地telnet一个ip或域名,并输出结果到文件中===telnetscript.sh
编写shell脚本不停地telnet一个域名,并输出结果到文件中 [root@localhost ~]# cat telnetscript.sh #!/bin/bash #检查是否在root用户下执行 ...
- NoNodeAvailableException[None of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
我在springboot 集成 elasticsearch,启动springboot测试创建索引,建立索引的时候报 : NoNodeAvailableException[None of the con ...
- java架构之路-(JVM优化与原理)JVM的运行时内存模型
还是我们上次的图,我们上次大概讲解了类加载子系统的执行过程,验证,准备,解析,初始化四个过程.还有我们的双亲委派机制. 我们这次来说一下运行时内存模型.上一段小代码. public class Mai ...
- sap和OA之间数值传递1(环境准备)
1.本公司使用的是致远A8,首先在本机上准备好A8环境,java环境(jre1.8.0_131),eclipse版本(建议用eclipseInstaller下载最新的NEON版本),安装致远ide插件 ...
- 绑定 Binding Path=.,Binding.,Binding Source={StaticResource ResourceKey="Hello"} xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib" <Window.Resources> <Style Targ ...
- python中class的总结
思维导图文件:https://files-cdn.cnblogs.com/files/benjieming/class%E6%9C%BA%E5%88%B6.zip
- css中的行内元素和块级元素总结
块级元素 <address>, <button>, <caption>, <dd>, <del>, <div>, & ...
- 从webkit内核简单看css样式和css规则优先级(权重)
目录 webkit中样式相关类及类间关系 样式规则匹配 权重(优先级)计算 权重相同时的覆盖原则 webkit中样式相关类及类间关系 资料来源: <webkit技术内幕> 结构相关类: 1 ...
- 团队作业第3周——需求改进&系统设计
目录 团队作业第3周--需求改进&系统设计 1.需求&原型改进 2.系统设计 3.Alpha任务分配计划 4.测试计划 1 测试术语 4.2 有关项目人员组成 2 任务概述 3.测试策 ...