标签:string dfs git end tor set 基本 amp res
题目描述链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/
基本思路:dfs搜索即可。
LeetCode C++求解代码:
class Solution { public: vector<string>res; unordered_map<char,string>fun; unordered_set<string>record; vector<string> letterCombinations(string digits) { fun[‘2‘]="abc"; fun[‘3‘]="def"; fun[‘4‘]="ghi"; fun[‘5‘]="jkl"; fun[‘6‘]="mno"; fun[‘7‘]="pqrs"; fun[‘8‘]="tuv"; fun[‘9‘]="wxyz"; string ans=""; for(int i=0;i<fun[digits[0]].size();i++) dfs(digits,0,i,ans); return res; } void dfs(string digits,int i,int j,string ans){ if(i==digits.size()-1&&ans.size()==digits.size()-1){ ans+=fun[digits[i]][j]; if(record.find(ans)==record.end()){ res.push_back(ans); record.insert(ans); } return; } ans+=fun[digits[i]][j]; for(int x=i+1;x<digits.size();x++){ for(int y=0;y<fun[digits[x]].size();y++){ dfs(digits,x,y,ans); } } ans.pop_back(); } };
标签:string dfs git end tor set 基本 amp res
原文地址:https://www.cnblogs.com/zzw-/p/13567390.html