您的位置:首页 > 其它

Letter Combinations of a Phone Number

2015-07-17 10:29 225 查看
参考编程之美3.2电话号码对应英语单词

以及参考这篇博文

[code]class Solution {
public:
    vector<string> letterCombinations(string digits) {

        vector<string> result;

        char hash_map[10][4] = {{}, {}, {'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}};
        int mapNumber[10] = {0, 0, 3, 3, 3, 3, 3, 4, 3, 4};

        int length = digits.size();

        if(length == 0) 
            return result;

        int *combinationNumber = new int[length];
        for(int i = 0; i < length; i++)
            combinationNumber[i] = 0;

        while(true) {

            string tmp;
            for(int i = 0; i < length; i++) {

                tmp.push_back(hash_map[digits[i] - '0'][combinationNumber[i]]);

            }
            result.push_back(tmp);

            int k = length - 1;
            while(k >= 0) {

                if(combinationNumber[k] < mapNumber[digits[k] - '0'] - 1) {

                    combinationNumber[k]++;
                    break;

                }else {

                    combinationNumber[k] = 0;
                    k--;

                }

            }
            if(k < 0)

                break;
        }

        return result;    

    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: