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

Strobogrammatic Number II -- LeetCode

时间:2016-08-24 07:43:19      阅读:123      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!