加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

leetcode个人题解——#17 Letter Combinations of a Phone Numbe

发布时间:2020-12-14 04:23:26 所属栏目:大数据 来源:网络整理
导读:思路:用深搜遍历九宫格字符串,一开始做的时候发生了引用指向空地址的问题,后来发现是vector不能直接=赋值。 class Solution { public : int len; string map[ 8 ]={ " abc " , " def " , " ghi " , " jkl " , " mno " , " pqrs " , " tuv " , " wxyz " };

思路:用深搜遍历九宫格字符串,一开始做的时候发生了引用指向空地址的问题,后来发现是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;   
    }
};

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读