C++ Clean Solution


  • 0
    J
    class Solution {
      
    public:
        vector<string> findStrobogrammatic(int n) {
            vector<string> base = {"0","1","8"};
            if(n == 0) return {};
            if(n == 1) return base;
            
            vector<string> ret;
            vector<pair<string,string>> mid{{"0","0"},{"1","1"},{"6","9"},{"8","8"},{"9","6"}};
            for(int i = 1; i < mid.size(); i++){
                pair<string,string>& p = mid[i];
                helper(ret,n-2,p.first,p.second,base,mid);
            }
            
            return ret;
        }
        
        void helper(vector<string>& ret, int n, string head, string tail, vector<string>& base, vector<pair<string,string>>& mid){
            if(n <= 0){
                ret.push_back(head + tail);
                return;
            }
            
            if(n == 1){
                for(auto& s : base)
                    ret.push_back(head + s + tail);
                return;
            }
            
            for(auto& p : mid){
                helper(ret,n-2,head+p.first,p.second+tail,base,mid);
            }
            
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.