LeetCode Letter Combinations of a Phone Number C++

xiaoxiao2021-02-28  107

#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"); }
转载请注明原文地址: https://www.6miu.com/read-20881.html

最新回复(0)