Leetcode 247. Strobogrammatic Number II (数学,递归)

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"]


题解:递归解。N和N-1,N-2有关,此时需判重;最优的解法是只和N-2有关。


class Solution {
public:
    vector<string> findStrobogrammatic(int n) {   //解法一,耗时
        vector<string> res;
        if(n==0) return res;
        if(record[n].size()>0) return record[n];
        if(n==1) {
            string tmp[]={"0","1","8"};
            for(string t:tmp) {
               res.push_back(t);
            }
            return res;
        }
        else if(n==2) {
            string tmp[]={"11","88","69","96"};
            for(string t:tmp) {
               res.push_back(t);
            }
            return res;
        }
        else {
            unordered_set<string> tmp_set;
            vector<string> t1=findStrobogrammatic(n-1);
            vector<string> t2=findStrobogrammatic(n-2);
            string addOne[]={"0","1","8"};
            string addTwo[5][2]={{"0","0"},{"6","9"},{"1","1"},{"9","6"},{"8","8"}};
            for(string s:t1) {
                if(n%2) {
                    for(string tmp:addOne) {
                        tmp_set.insert(s.substr(0,(n-1)/2)+tmp+s.substr((n-1)/2));
                    }
                }
                else {
                    tmp_set.insert(s.substr(0,(n-1)/2)+s[(n-1)/2]+s.substr((n-1)/2));
                }
            }

            for(string s:t2) {
                int l=0,r=(int)s.size()-1;
                while(l<r) {
                    string a,b;
                    string s1=s.substr(0,l+1);
                    string s2=s.substr(r);
                    string s_inner=s.substr(l+1,r-l-1);
                    for(int i=0;i<5;i++) {
                        a=addTwo[i][0];
                        b=addTwo[i][1];
                        if(a!=s[l]+"") {
                            tmp_set.insert(s1+a+s_inner+b+s2);
                        }
                    }
                    l++;r--;
                }
                for(int i=1;i<5;i++) {
                    tmp_set.insert(addTwo[i][0]+s+addTwo[i][1]);
                }
            }
            unordered_set<string>::iterator it;
            for(it=tmp_set.begin();it!=tmp_set.end();it++) {
                res.push_back(*it);
            }
            record[n]=res;
            return res;
        }
    }
private:
    unordered_map<int,vector<string>> record;
};

class Solution {
public:
    vector<string> findStrobogrammatic(int n) {  //解法二,较优
        return helper(n , n);
    }
    vector<string> helper(int m, int n){
        if(m == 0) return vector<string>({""});
        if(m == 1) return vector<string>({"0", "1", "8"});
        vector<string> tmp = helper(m - 2, n), res;
        for(int i = 0; i < tmp.size(); i++){
            if(m != n) res.push_back("0" + tmp[i] + "0");
            res.push_back("1" + tmp[i] + "1");
            res.push_back("6" + tmp[i] + "9");
            res.push_back("8" + tmp[i] + "8");
            res.push_back("9" + tmp[i] + "6");
        }
        return res;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值