[LeetCode] Strobogrammatic Number II

This problem can be solved easily once you find the regularities :-) This link has done it for you. You may refer to its Python version. I rewrite it in C++ below.

 1 class Solution {
 2 public:
 3     vector<string> findStrobogrammatic(int n) { 
 4         vector<string> strobos;
 5         if (n & 1) strobos = {"0", "1", "8"};
 6         else strobos = {""};
 7         vector<string> bases = {"00", "11", "88", "69", "96"};
 8         int m = bases.size(); 
 9         while (n > 1) {
10             n -= 2;
11             vector<string> temp;
12             for (string strobo : strobos)
13                 for (int i = (n < 2 ? 1 : 0); i < m; i++)
14                     temp.push_back(bases[i].substr(0, 1) + strobo + bases[i].substr(1));
15             swap(temp, strobos);
16         }
17         return strobos;
18     }
19 };

 

posted @ 2015-08-08 14:52  jianchao-li  阅读(1431)  评论(0编辑  收藏  举报