LeetCode17. Combination of phone numbers

Given numbers only a  2-9 string, it returns all letter combinations indicated.

Given digital map to letters as follows (the same telephone key). Note 1 does not correspond to any alphabet.

Example:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Problem-solving ideas:

By digits entered, find the corresponding letter, then the current existing letters and letter combination.

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> s1,s2;
        map<char,vector<string>> mp;
        mp['2']={"a","b","c"};
        mp['3']={"d","e","f"};
        mp['4']={"g","h","i"};
        mp['5']={"j","k","l"};
        mp['6']={"m","n","o"};
        mp['7']={"p","q","r","s"};
        mp['8']={"t","u","v"};
        mp['9']={"w","x","y","z"};
        for(auto &ch:digits){
            int size=s1.size();
            auto &v=mp[ch];
            for(auto &s:v){
                for(int i=0;i<size;i++){
                    string tmp=s1[i]+s;
                    s2.push_back(tmp);
                }
                if(size==0) s2.push_back(s);
            }
            swap(s1,s2);
            s2.clear();
        }
        return s1;
    }
};

https://blog.csdn.net/lv1224/article/details/79767427

Guess you like

Origin blog.csdn.net/ueh286/article/details/88872647