Very Simple C++ Recursion Codes in 0ms


  • 0
    T
    class Solution {
    public:
    	vector<string> letterCombinations(string digits) {
    		static const vector<string> hash = { "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz" };
    		vector<string> retvec;
    		combinations(digits, retvec, "",hash);
    		return retvec;
    	}
    	void combinations(string digits, vector<string>& records, string result,const vector<string>& hash) {
    		if (digits.length() == 0&& result.length()>0) records.push_back(result);
    		else {
    			for (auto x : hash[digits[0]-'0']) {
    				result += x;
    				combinations(digits.substr(1, digits.length() - 1), records, result,hash);
    				result = result.substr(0, result.length() - 1);
    			}
    		}
    	}
    
    };
    

Log in to reply
 

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