# [LeetCode] 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.

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.

Show Tags

class Solution {
public:
int strobogrammaticInRange(string low, string high) {
int res = 0;
for (int i = low.size(); i <= high.size(); ++i) {
find(low, high, "", i, res);
find(low, high, "0", i, res);
find(low, high, "1", i, res);
find(low, high, "8", 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 != 1 && path[0] == '0')) return;
if ((len == low.size() && path.compare(low) < 0) || (len == high.size() && path.compare(high) > 0)) {
return;
}
++res;
}
find(low, high, "0" + path + "0", len, res);
find(low, high, "1" + path + "1", len, res);
find(low, high, "6" + path + "9", len, res);
find(low, high, "8" + path + "8", len, res);
find(low, high, "9" + path + "6", len, res);
}
};

class Solution {
public:
int strobogrammaticInRange(string low, string high) {
int res = 0;
find(low, high, "", res);
find(low, high, "0", res);
find(low, high, "1", res);
find(low, high, "8", 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() == low.size() && w.compare(low) < 0) || (w.size() == high.size() && w.compare(high) > 0)) {
return;
}
if (!(w.size() > 1 && w[0] == '0')) ++res;
}
if (w.size() + 2 > high.size()) return;
find(low, high, "0" + w + "0", res);
find(low, high, "1" + w + "1", res);
find(low, high, "6" + w + "9", res);
find(low, high, "8" + w + "8", res);
find(low, high, "9" + w + "6", res);
}
};

|
9月前
|

Leetcode 313. Super Ugly Number

81 1
|
2月前
|

LeetCode 题目 65：有效数字（Valid Number）【python】
LeetCode 题目 65：有效数字（Valid Number）【python】
37 5
|
3月前
|

【LeetCode力扣】单调栈解决Next Greater Number（下一个更大值）问题
【LeetCode力扣】单调栈解决Next Greater Number（下一个更大值）问题
25 0
|
9月前
|

Leetcode Single Number II （面试题推荐）

30 0
|

LeetCode 414. Third Maximum Number

79 0
|

LeetCode 405. Convert a Number to Hexadecimal

74 0
|
API
LeetCode 375. Guess Number Higher or Lower II

93 0
|
API
LeetCode 374. Guess Number Higher or Lower

65 0
|

LeetCode 321. Create Maximum Number

57 0
|

LeetCode 313. Super Ugly Number

83 0