Skip to content

Commit 82220ba

Browse files
committed
fix: show the go and c# solutions for lc No.0017
No.0017.Letter Combinations of a Phone Number
1 parent a01b98c commit 82220ba

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

solution/0000-0099/0017.Letter Combinations of a Phone Number/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,81 @@ class Solution {
111111
}
112112
```
113113

114+
### **Go**
115+
116+
```go
117+
var table = map[string][]string {
118+
"2": {"a", "b", "c"},
119+
"3": {"d", "e", "f"},
120+
"4": {"g", "h", "i"},
121+
"5": {"j", "k", "l"},
122+
"6": {"m", "n", "o"},
123+
"7": {"p", "q", "r", "s"},
124+
"8": {"t", "u", "v"},
125+
"9": {"w", "x", "y", "z"},
126+
}
127+
128+
func letterCombinations(digits string) []string {
129+
if digits == "" {
130+
return make([]string, 0)
131+
}
132+
var result = table[string(digits[0])]
133+
for i:=1; i<len(digits); i++ {
134+
t := table[string(digits[i])]
135+
nr := make([]string, len(result) * len(t))
136+
for j:=0; j<len(result); j++ {
137+
for k:=0; k<len(t); k++ {
138+
nr[len(t)*j + k] = result[j] + t[k]
139+
}
140+
}
141+
result = nr
142+
}
143+
return result
144+
}
145+
```
146+
147+
### **C#**
148+
149+
```cs
150+
using System.Collections.Generic;
151+
using System.Linq;
152+
153+
public class Solution {
154+
private static string[] chars = {
155+
"abc",
156+
"def",
157+
"ghi",
158+
"jkl",
159+
"mno",
160+
"pqrs",
161+
"tuv",
162+
"wxyz"
163+
};
164+
165+
public IList<string> LetterCombinations(string digits) {
166+
var numbers = digits.Where(d => d >= '2' && d <= '9').Select(d => d - '2').ToArray();
167+
var states = new int[numbers.Length];
168+
var results = new List<string>();
169+
if (numbers.Length == 0) return results;
170+
while (true) {
171+
results.Add(new string(states.Select((s, j) => chars[numbers[j]][s]).ToArray()));
172+
var i = states.Length - 1;
173+
++states[i];
174+
while (i >= 0 && states[i] == chars[numbers[i]].Length)
175+
{
176+
states[i] = 0;
177+
--i;
178+
if (i >= 0)
179+
{
180+
++states[i];
181+
}
182+
}
183+
if (i < 0) return results;
184+
}
185+
}
186+
}
187+
```
188+
114189
### **...**
115190

116191
```

solution/0000-0099/0017.Letter Combinations of a Phone Number/README_EN.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,81 @@ class Solution {
101101
}
102102
```
103103

104+
### **Go**
105+
106+
```go
107+
var table = map[string][]string {
108+
"2": {"a", "b", "c"},
109+
"3": {"d", "e", "f"},
110+
"4": {"g", "h", "i"},
111+
"5": {"j", "k", "l"},
112+
"6": {"m", "n", "o"},
113+
"7": {"p", "q", "r", "s"},
114+
"8": {"t", "u", "v"},
115+
"9": {"w", "x", "y", "z"},
116+
}
117+
118+
func letterCombinations(digits string) []string {
119+
if digits == "" {
120+
return make([]string, 0)
121+
}
122+
var result = table[string(digits[0])]
123+
for i:=1; i<len(digits); i++ {
124+
t := table[string(digits[i])]
125+
nr := make([]string, len(result) * len(t))
126+
for j:=0; j<len(result); j++ {
127+
for k:=0; k<len(t); k++ {
128+
nr[len(t)*j + k] = result[j] + t[k]
129+
}
130+
}
131+
result = nr
132+
}
133+
return result
134+
}
135+
```
136+
137+
### **C#**
138+
139+
```cs
140+
using System.Collections.Generic;
141+
using System.Linq;
142+
143+
public class Solution {
144+
private static string[] chars = {
145+
"abc",
146+
"def",
147+
"ghi",
148+
"jkl",
149+
"mno",
150+
"pqrs",
151+
"tuv",
152+
"wxyz"
153+
};
154+
155+
public IList<string> LetterCombinations(string digits) {
156+
var numbers = digits.Where(d => d >= '2' && d <= '9').Select(d => d - '2').ToArray();
157+
var states = new int[numbers.Length];
158+
var results = new List<string>();
159+
if (numbers.Length == 0) return results;
160+
while (true) {
161+
results.Add(new string(states.Select((s, j) => chars[numbers[j]][s]).ToArray()));
162+
var i = states.Length - 1;
163+
++states[i];
164+
while (i >= 0 && states[i] == chars[numbers[i]].Length)
165+
{
166+
states[i] = 0;
167+
--i;
168+
if (i >= 0)
169+
{
170+
++states[i];
171+
}
172+
}
173+
if (i < 0) return results;
174+
}
175+
}
176+
}
177+
```
178+
104179
### **...**
105180

106181
```

0 commit comments

Comments
 (0)