【LeetCode】C# 17、Letter Combinations of a Phone Number

2727 ワード

Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string “23” Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]. Note: Although the above answer is in lexicographical order, your answer could be in any order you want.
九宮格入力法のように、2つの数字を入力した後、可能なすべてのアルファベットの組み合わせを返します.
考え方:まず九宮格が表す文字を一つの配列に入れて呼び出しやすくし、それから遍歴して追加しました.digitsの数字は文字を取り出し、stringに変換してから数字に変換しないとインデックスに問題が発生することに注意してください.
public class Solution {
    public IList<string> LetterCombinations(string digits) {
        IList<string> res = new List<string>();
        string[] mapping = new string[] {"0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        if(digits == "") return res;
        res.Add("");
        for(int i=0;istring> temp = new List<string>();
            foreach(string item in res){
                for(int j=0;jstring tempstring = item + mapping[Convert.ToInt32(digits[i].ToString())][j].ToString();
                    temp.Add(tempstring);
                }
            }
            res = temp;
        }
        return res;
    }
}