C++ accepted solution


  • 1
    J
    void getAllStrobogrammatic(vector<string>& result, vector<char>& nums1, vector<char>& nums2, string t, int n) {
    	if (n == 0) {
    	    if(t.length() > 1 && t.front() == '0')
    	        return;
    		result.push_back(t);
    		return;
    	}
    
    	for (int i = 0; i<nums1.size(); i++)
    		getAllStrobogrammatic(result, nums1, nums2, nums1[i] + t + nums2[i], n - 2);
    }
    
    vector<string> findStrobogrammatic(int n) {
    	vector<char> nums1 = { '0', '1', '6', '8', '9' };
    	vector<char> nums2 = { '0', '1', '9', '8', '6' };
    	vector<string> result;
    	
    	string t;
    	if (n % 2 == 0)
    		getAllStrobogrammatic(result, nums1, nums2, t, n);
    	else {
    		t = "0"; getAllStrobogrammatic(result, nums1, nums2, t, n - 1);
    		t = "1"; getAllStrobogrammatic(result, nums1, nums2, t, n - 1);
    		t = "8"; getAllStrobogrammatic(result, nums1, nums2, t, n - 1);
    	}
    
    	return result;
    }

Log in to reply
 

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