zoukankan      html  css  js  c++  java
  • Strobogrammatic Number II -- LeetCode

    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 };
  • 相关阅读:
    解释基于注解的切面实现?
    @Controller 注解?
    Spring由哪些模块组成?
    解释AOP模块 ?
    什么是Spring的依赖注入?
    自动装配有哪些局限性 ?
    在Spring框架中如何更有效地使用JDBC?
    @Autowired 注解?
    @Required 注解?
    解释AOP?
  • 原文地址:https://www.cnblogs.com/fenshen371/p/5801445.html
Copyright © 2011-2022 走看看