電話番号の17文字の組み合わせ[M]電話番号の文字の組み合わせ

タイトル

包括的な2-9から数字を含む文字列を考えると、数は表すことができ、すべての可能な文字の組み合わせを返します。
(ちょうど電話ボタンのような)の文字に数字のマッピングは以下のとおりです。1は、任意の文字にマップされないことに注意してください。

考え

思考:再帰

まず、電話ボタン上の数字と文字の間の対応を確立し、マップ内のC ++の使用を考えるのは簡単です

2思考:バックトラック

ヒント

再帰

バックトラッキング

C ++

  • 思考
    map<int,string> table={{2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};
    
    vector<string> letterCombinations(string digits) {
        
        vector<string> result;
        
        if(digits.length()==0)
            return result;
            
        
        combineNum(result,0,"",digits);
        
        return result;
    }
    
    void combineNum(vector<string>& r,int count,const string& a,const string& b){
    
        if (count == b.size()){
          r.push_back(a);
          return;
        }

        string curStr = table[b[count] - '0'];
    
        for (char s : curStr){ 
            combineNum(r, count + 1, a + s, b);
        }   
    }
  • アイデア2

パイソン

おすすめ

転載: www.cnblogs.com/Jessey-Ge/p/10993495.html