1. 程式人生 > >leetcode個人題解——#17 Letter Combinations of a Phone Number

leetcode個人題解——#17 Letter Combinations of a Phone Number

urn its ati 深搜 etc earch ber arch git

思路:用深搜遍歷九宮格字符串,一開始做的時候發生了引用指向空地址的問題,後來發現是vector不能直接=賦值。

class Solution {
public:
    int len;
    
    string map[8]={"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
    void search(int pos, string res, vector<string>& a, string digits){
        if(pos == len) return;
        string
s = map[digits[pos] - 2]; for (int i = 0;i < s.size(); i++) { if(pos == len - 1)a.push_back(res + s[i]); else search(pos + 1, res + s[i], a, digits); } } vector<string> letterCombinations(string digits) { vector<string
> ans; len = digits.size(); if(len == 0)return ans; search(0,"",ans,digits); return ans; } };

leetcode個人題解——#17 Letter Combinations of a Phone Number