-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path017-LetterCombinationsOfAPhoneNumber.cs
47 lines (40 loc) · 1.66 KB
/
017-LetterCombinationsOfAPhoneNumber.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//-----------------------------------------------------------------------------
// Runtime: 232ms
// Memory Usage: 31.4 MB
// Link: https://leetcode.com/submissions/detail/378582740/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _017_LetterCombinationsOfAPhoneNumber
{
public IList<string> LetterCombinations(string digits)
{
string[] phoneChars = new string[] { " ",
"",
"abc",
"def",
"ghi",
"jkl",
"mno",
"pqrs",
"tuv",
"wxyz"
};
if (digits.Length == 0) return new List<string>();
var results = new List<string>() { "" };
foreach (var digit in digits)
{
var keys = phoneChars[digit - '0'];
if (keys.Length == 0) continue;
var temp = new List<string>();
foreach (var result in results)
foreach (var ch in keys)
temp.Add(result + ch.ToString());
results = temp;
}
if (results.Count == 1 && results[0] == "") results.Clear();
return results;
}
}
}