247. Strobogrammatic Number II
https://leetcode.com/problems/strobogrammatic-number-ii/description/
Thoughts
Code
class Solution {
public:
vector<string> findStrobogrammatic(int n) {
vector<string> res;
int start = 0;
if (n & 1 == 1) {
res.push_back("0");
res.push_back("1");
res.push_back("8");
start = 1;
} else res.push_back("");
for (int i = start; i < n; i += 2) {
vector<string> r;
for (auto s : res) {
if (i + 2 < n) r.push_back("0" + s + "0");
r.push_back("6" + s + "9");
r.push_back("9" + s + "6");
r.push_back("1" + s + "1");
r.push_back("8" + s + "8");
}
swap(res, r);
}
return res;
}
};Analysis
Last updated