Letter Combinations of a Phone Number Solutions in C++
Number 17
Difficulty Medium
Acceptance 46.9%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/// Author : Hao Chen// Date : 2014-07-17#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace std;vector<string> letterCombinations(string digits) {char phone[10][4]={ {' ', '\0', '\0', '\0' }, //0{'\0', '\0', '\0', '\0' }, //1{'a', 'b', 'c', '\0' }, //2{'d', 'e', 'f', '\0' }, //3{'g', 'h', 'i', '\0' }, //4{'j', 'k', 'l', '\0' }, //5{'m', 'n', 'o', '\0' }, //6{'p', 'q', 'r', 's' }, //7{'t', 'u', 'v', '\0' }, //8{'w', 'x', 'y', 'z' } //9};vector<string> result;if (digits.size()<=0){result.push_back("");return result;}for( int i=0; i<digits.size(); i++ ) {if (!isdigit(digits[i])) {vector<string> r;return r;}int d = digits[i] - '0';if (result.size()<=0){for( int j=0; j<4 && phone[d][j]!='\0'; j++ ){string s;s += phone[d][j];result.push_back(s);}continue;}vector<string> r;for (int j=0; j<result.size(); j++){for( int k=0; k<4 && phone[d][k]!='\0'; k++ ){string s = result[j] + phone[d][k];//sort(s.begin(), s.end());r.push_back(s);}}result = r;}//sort(result.begin(), result.end());return result;}void printVector(vector<string>& ss){cout << "{ ";for(int i=0; i<ss.size(); i++){if (i>0) cout << ", ";cout << ss[i];}cout << " }" << endl;}int main(int argc, char**argv){string s="23";if (argc>1){s=argv[1];}vector<string> ss = letterCombinations(s);printVector(ss);return 0;}
C++ solution by pezy/LeetCode
//// Solution.h// Test//// Created by pezy on 12/27/14.// Copyright (c) 2014 pezy. All rights reserved.//#ifndef Test_Solution_h#define Test_Solution_h#include <vector>#include <string>#include <array>using std::vector; using std::string; using std::array;class Solution {public:vector<string> letterCombinations(string digits) {vector<string> ret{""};array<string, 10> map{"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};for (auto digit : digits) {vector<string> tmp;for (auto c : map[digit - '0'])for (const auto &pre : ret)tmp.push_back(pre + c);ret = tmp;}return ret;}};#endif
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/letter-combinations-of-a-phone-number/description//// Author : liuyubobobo/// Time : 2017-11-18#include <iostream>#include <vector>#include <string>#include <cassert>using namespace std;/// Backtracking/// Time Complexity: O(2^len(s))/// Space Complexity: O(len(s))class Solution {private:const string letterMap[10] = {" ", //0"", //1"abc", //2"def", //3"ghi", //4"jkl", //5"mno", //6"pqrs", //7"tuv", //8"wxyz" //9};vector<string> res;void findCombination(const string &digits, int index, const string &s){if(index == digits.size()){res.push_back(s);return;}char c = digits[index];assert(c >= '0' && c <= '9' && c != '1');string letters = letterMap[c - '0'];for(int i = 0 ; i < letters.size() ; i ++)findCombination(digits, index+1, s + letters[i]);return;}public:vector<string> letterCombinations(string digits) {res.clear();if(digits == "")return res;findCombination(digits, 0, "");return res;}};void printVec(const vector<string>& vec){for(string s: vec)cout << s << endl;}int main() {printVec(Solution().letterCombinations("234"));return 0;}