add new solutions
This commit is contained in:
parent
475d438db4
commit
1433bf4850
17 changed files with 394 additions and 0 deletions
39
solutions/0/q17/solution.go
Normal file
39
solutions/0/q17/solution.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package q17
|
||||
|
||||
var alphabets = [][]byte{
|
||||
[]byte("abc"),
|
||||
[]byte("def"),
|
||||
[]byte("ghi"),
|
||||
[]byte("jkl"),
|
||||
[]byte("mno"),
|
||||
[]byte("pqrs"),
|
||||
[]byte("tuv"),
|
||||
[]byte("wxyz"),
|
||||
}
|
||||
|
||||
func combinations(offsets []int, i int, buffer []byte, strings []string) []string {
|
||||
if buffer == nil {
|
||||
buffer = make([]byte, len(offsets))
|
||||
}
|
||||
|
||||
if i == len(offsets) {
|
||||
return append(strings, string(buffer))
|
||||
}
|
||||
|
||||
for _, c := range alphabets[offsets[i]] {
|
||||
buffer[i] = c
|
||||
strings = combinations(offsets, i+1, buffer, strings)
|
||||
}
|
||||
return strings
|
||||
}
|
||||
|
||||
func letterCombinations(digits string) []string {
|
||||
offsets := make([]int, len(digits))
|
||||
for i := range len(digits) {
|
||||
offsets[i] = int(digits[i] - '2')
|
||||
}
|
||||
|
||||
return combinations(offsets, 0, nil, []string{})
|
||||
}
|
||||
|
||||
var _ = letterCombinations
|
||||
Loading…
Add table
Add a link
Reference in a new issue