[LeetCode] Strobogrammatic Number III
2015-08-18 15:04
260 查看
The idea is similar to Strobogrammatic Number II: generate all those in-range strobogrammatic numbers and count.
You may refer to this link for a very readable code, which I have rewritten in C++ below.
You may refer to this link for a very readable code, which I have rewritten in C++ below.
class Solution { public: Solution() { mp = {{'0', '0'}, {'1', '1'}, {'6', '9'}, {'8', '8'}, {'9', '6'}}; } int strobogrammaticInRange(string low, string high) { int ans = 0, l = low.length(), u = high.length(); for (int i = l; i <= u; i++) { string temp(i, ' '); strobogrammaticCount(temp, ans, low, high, 0, i - 1); } return ans; } private: unordered_map<char, char> mp; void strobogrammaticCount(string temp, int& ans, string& low, string& high, int lo, int hi) { if (lo > hi) { if ((temp[0] != '0' || temp.length() == 1) && less(low, temp) && less(temp, high)) ans++; return; } for (auto m : mp) { temp[lo] = m.first; temp[hi] = m.second; if ((lo == hi && m.first == m.second) || lo < hi) strobogrammaticCount(temp, ans, low, high, lo + 1, hi - 1); } } bool less(string& s, string& t) { int m = s.length(), n = t.length(), i; if (m != n) return m < n; for (i = 0; i < m; i++) if (s[i] == t[i]) continue; else break; return i == m || s[i] < t[i]; } };
相关文章推荐
- PHP URL变量
- Transact使用
- oracle密码过期ORA-28002: 7天之后口令将过期的解决方法
- ios7 状态栏与导航栏布局与ScrollView的特性分析
- iOS-instancetype与id的比较
- Shredding Company(POJ--1416
- leetcode-94:Binary Tree Inorder Traversal (Java)
- sizeof与strlen的区别
- leetcode-94:Binary Tree Inorder Traversal (Java)
- hdu 4278 2012天津赛区网络赛 数学 *
- c语言实现数据结构中的顺序表
- iOS项目开发实战——使用Xcode6设计自定义控件与图形
- 签到日历升级美化版包含css
- 用MD5验证上传文件的完整性
- DDL、DML和DCL的理解
- Linux安装vmtools
- ccf题目:字符串匹配
- 3D图片轮播实现
- 初识Objective-C Runtime
- volley学习笔记