LeetCode Letter Combinations of a Phone Number C++

#include <iostream>
#include <vector>
#include<algorithm>
#include <queue>
using namespace std;

/************************************************************************/
/*
Problem:
Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

For example:
    1       2       3
            abc     edf
    4       5       6
    ghi     jkl     mno
    7       8       9
    pqrs    tuv     wxzy
    Input:Digit string "23"
    Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].



Author  :   crazys_popcorn@126.com

DateTime:   2017年8月5日  12:06:17

*/
/************************************************************************/


class Solution {
public:

    vector<string> letterCombinations(string digits)
    {

        vector<string>_result;
        if (digits.size() < 1)
            return _result;
         //定义9个按钮
        vector<string>_zinum = {"", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxzy" };

        int _size = digits.size();
        _result.push_back("");
        for (int i = 0; i < _size; i++)
        {
            int num = digits[i] - '1';
            if (num <1 || num > 9)
                continue;
            std::string& _str = _zinum[num];
            if (_str.empty())
                continue;
            std::vector<string > _tempresult;
            for (int j = 0; j < _str.size(); ++j)
            {
                for (int k = 0; k<_result.size(); ++k)
                {
                    //按照顺序相加  双层for循环。。把每一个for的第一个字母 宇第二组字母都相加插入到新的容器里面
                    //然后swap 交换。
                    _tempresult.push_back(_result[k] + _str[j]);
                }

            }
            _result.swap(_tempresult);
        }
        return _result;
    }



};


void main()
{

    std::string _str = "2";

    Solution s1;

    vector<string>_nums;
    _nums = s1.letterCombinations(_str);



    std::cout << "" << endl;
    system("pause");

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值