首页 > 其他 > 详细

Strobogrammatic Number II -- LeetCode

时间:2016-08-24 07:40:08      阅读:242      评论:0      收藏:0      [点我收藏+]

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Find all strobogrammatic numbers that are of length = n.

For example,
Given n = 2, return ["11","69","88","96"].

 1 class Solution {
 2 public:
 3     char num[5] = {‘0‘, ‘1‘, ‘6‘, ‘8‘, ‘9‘};
 4     char rotate(char num) {
 5         if (num == ‘0‘ || num == ‘1‘ || num == ‘8‘)
 6             return num;
 7         else if (num == ‘6‘) return ‘9‘;
 8         else  return ‘6‘;
 9     }
10     void help(vector<string>& res, string& cand, int curInd) {
11         if (cand.size() & 1 && curInd == (cand.size() - 1) / 2) {
12             cand[curInd] = ‘0‘; res.push_back(cand);
13             cand[curInd] = ‘1‘; res.push_back(cand);
14             cand[curInd] = ‘8‘; res.push_back(cand);
15         } else {
16             int st = curInd ? 0 : 1;
17             for (; st < 5; st++) {
18                 cand[curInd] = num[st];
19                 cand[cand.size() - curInd - 1] = rotate(num[st]);
20                 if (curInd == (cand.size() - 1) / 2) res.push_back(cand);
21                 else help(res, cand, curInd + 1);
22             }
23         }
24     }
25     vector<string> findStrobogrammatic(int n) {
26         vector<string> res;
27         if (n < 1) return res;
28         string cand(n, ‘0‘);
29         help(res, cand, 0);
30         return res;
31     }
32 };

 

Strobogrammatic Number II -- LeetCode

原文:http://www.cnblogs.com/fenshen371/p/5801445.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!