码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Strobogrammatic Number II

时间:2015-08-08 16:09:29      阅读:994      评论:0      收藏:0      [点我收藏+]

标签:

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 };

 

[LeetCode] Strobogrammatic Number II

标签:

原文地址:http://www.cnblogs.com/jcliBlogger/p/4713146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!