首页 > 其他 > 详细

[LeetCode] Strobogrammatic Number II

时间:2015-08-08 16:09:24      阅读:2313      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!