C++ easy recursion


  • 1
    D
    class Solution {
    public:
        vector<string> findStrobogrammatic(int n) {     
            mp["6"] = "9";
            mp["9"] = "6";
            mp["8"] = "8";
            mp["1"] = "1";
            mp["0"] = "0";
            if (n & 1) {
                helper("1", n - 1);
                helper("0", n - 1);
                helper("8", n - 1);
            }
            else helper("", n);
            return res;
        }
        void helper(string s, int n) {
            if (n == 0) {
                res.push_back(s);
                return;
            }
            for (auto p : mp) {
                if (n == 2 && p.first == "0") continue;
                helper(p.first + s + p.second, n - 2);
            }
        }
    private:
        vector<string> res;
        unordered_map<string, string> mp;
    };
    

Log in to reply
 

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