public IList<string> LetterCombinations(string digits) { string[] result = new string[3]; // Code Console.WriteLine("aaaa"); // aaaa return result; // Error }
public class Solution { public IList<string> LetterCombinations(string digits) { int size = ParseToChar(digits[0]).Length; for (int digit = 1; digit < digits.Length; digit++) { size *= ParseToChar(digits[digit]).Length; } Console.WriteLine(size); string[] result = new string[size]; if (size == 1) return new string[0]; for (int digit = 0; digit < digits.Length; digit++) { string abcValues = ParseToChar(digits[digit]); float v = 0; int num = (int)Math.Pow(size / abcValues.Length, digit); for (int i = 0; i < result.Length; i++) { int value = (int)Math.Floor(v / num); Console.WriteLine("{0}, {1}, {2}, {3}, {4}", digit, num, i, v, value); Console.WriteLine(abcValues[value]); result[i] += abcValues[value]; Console.WriteLine(result[i]); v++; if (v == abcValues.Length * num) v = 0; } } Console.WriteLine("Result:"); for (int i = 0; i < result.Length; i++) { Console.WriteLine(result[i]); } return result; } public string[] chars = new string[]{ "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" }; public string ParseToChar(char c) { int num = c - '0'; return chars[num]; }}
30, 1, 0, 0, 0dd0, 1, 1, 1, 1ee0, 1, 2, 2, 2ffResult:def