A C++ recursive solution (not too fancy but very comprehensive)


  • 0
    G
    class Solution {
    public:
    
        vector<string> FindSob(int n,bool IncZero)
        {
            vector<string> Res;
            
            if(n == 0)
            {
                Res.push_back("");
                return (Res);
            }
            
            if(n == 1)
            {
                
                Res.push_back("0");
                Res.push_back("1");
                Res.push_back("8");
                
                return (Res);
            }
            
            vector<string> Temp = FindSob(n - 2,n > 2);
            vector<string> Res1;
            
            for(auto &s : Temp)
            {
                if(IncZero)
                {
                    Res.push_back("0" + s + "0");
                }
                
                Res.push_back("1" + s + "1");
                Res.push_back("6" + s + "9");
                Res.push_back("8" + s + "8");
                Res.push_back("9" + s + "6");
            }
            
            return (Res);
        }
    
        vector<string> findStrobogrammatic(int n) 
        {
            return FindSob(n,false);
        }
    };
    

Log in to reply
 

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