C++ Solution using a queue


  • 0
    T
    class Solution {
    public:
        vector<string> findStrobogrammatic(int n) {
            queue<string> stro;
            bool odd = n % 2;
            if(!odd){
                stro.push("");
            }else{
                stro.push("1");
                stro.push("8");
                stro.push("0");
            }
            
            
            while(stro.front().length() < n){
                string curr = stro.front();
                if(curr.length() < n - 2){
                    stro.push("0" + curr + "0");
                } 
                stro.push("1" + curr + "1");
                stro.push("6" + curr + "9");
                stro.push("9" + curr + "6");
                stro.push("8" + curr + "8");
                stro.pop();
            }
            vector<string> res;
           
            while(!stro.empty()){
                
                res.push_back(stro.front());
                stro.pop();
                
                
            }
    
        return res;
    }
    

    };


Log in to reply
 

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