[Leetcode] 248. Strobogrammatic Number III 解题报告
来源:互联网 发布:淘宝不能下载 编辑:程序博客网 时间:2024/06/11 17:44
题目:
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.
For example,
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.
Note:
Because the range might be a large number, the low and high numbers are represented as string.
思路:
在《[Leetcode] 247. Strobogrammatic Number II 解题报告》的基础上稍作改进就可以了。也就是枚举从low的长度到high的长度之间的符合条件的数,一旦发现它在low和high的范围之内,就更新计数器。然而,总觉得这种解法不是最优的,因为从直观上来说我们可能不需要枚举所有符合条件的数。等后面有空再想想具体做法吧。
代码:
class Solution {public: int strobogrammaticInRange(string low, string high) { int n1 = low.length(), n2 = high.length(); int ret = 0; for (int i = n1; i <= n2; ++i) { dfs(low, high, i, "", ret); } return ret; }private: void dfs(string &low, string &high, int n, string str, int &result) { if(n == 0) { if (stol(str) >= stol(low) && stol(str) <= stol(high)) { ++result; } return; } if(n % 2 != 0) { for(auto val : same) { dfs(low, high, n-1, val, result); } } else { for(int i = (n == 2) ? 1 : 0; i < two.size(); ++i) { dfs(low, high, n-2, two[i].first + str + two[i].second, result); } } } vector<string> same{"0", "1", "8"}; vector<pair<char,char>> two{{'0','0'},{'1','1'},{'6','9'},{'8','8'},{'9','6'}};};
阅读全文
0 0
- [leetcode] 248. Strobogrammatic Number III 解题报告
- [Leetcode] 248. Strobogrammatic Number III 解题报告
- LeetCode 248. Strobogrammatic Number III
- [leetcode] 246. Strobogrammatic Number 解题报告
- [leetcode] 247. Strobogrammatic Number II 解题报告
- [Leetcode] 246. Strobogrammatic Number 解题报告
- [Leetcode] 247. Strobogrammatic Number II 解题报告
- leetcode Strobogrammatic Number III
- [leetcode] 260. Single Number III 解题报告
- LeetCode 260. Single Number III 解题报告
- [Leetcode] 260. Single Number III 解题报告
- LeetCode 题解(240) : Strobogrammatic Number III
- [LeetCode248]Strobogrammatic Number III
- Leetcode: Strobogrammatic Number
- *LeetCode-Strobogrammatic Number
- [leetcode 246] Strobogrammatic Number
- Leetcode-246.Strobogrammatic Number
- LeetCode 246. Strobogrammatic Number
- js练习
- MyBatis中resultMap详解
- linux下创建用户、用户组及赋予sudoer权限
- CentOS 7 安装教程、硬盘分区、LVM、网络配置、软件源配置、制作
- bom编程+事件编程+dom编程+正则表达式
- [Leetcode] 248. Strobogrammatic Number III 解题报告
- The Virtual Storage Filter Driver is disabled through the registry. It is inactive for all disk driv
- 笔记
- c++
- 微信小程序购物商城定制开发
- 菜鸟的MATLAB学习记录
- 我还是很喜欢你,像相思藏在树底, 花落是你,花开也是你。
- Factor Attribution
- Android EditText如何控制输入光标的位置