mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 21:24:46 +01:00
20210120
This commit is contained in:
67
20210120/main.go
Normal file
67
20210120/main.go
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
//给你一个字符串 s,它由数字('0' - '9')和 '#' 组成。我们希望按下述规则将 s 映射为一些小写英文字符:
|
||||||
|
//
|
||||||
|
//字符('a' - 'i')分别用('1' - '9')表示。
|
||||||
|
//字符('j' - 'z')分别用('10#' - '26#')表示。
|
||||||
|
//返回映射之后形成的新字符串。
|
||||||
|
//
|
||||||
|
//题目数据保证映射始终唯一。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//示例 1:
|
||||||
|
//
|
||||||
|
//输入:s = "10#11#12"
|
||||||
|
//输出:"jkab"
|
||||||
|
//解释:"j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
|
||||||
|
//示例 2:
|
||||||
|
//
|
||||||
|
//输入:s = "1326#"
|
||||||
|
//输出:"acz"
|
||||||
|
//示例 3:
|
||||||
|
//
|
||||||
|
//输入:s = "25#"
|
||||||
|
//输出:"y"
|
||||||
|
//示例 4:
|
||||||
|
//
|
||||||
|
//输入:s = "12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"
|
||||||
|
//输出:"abcdefghijklmnopqrstuvwxyz"
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//提示:
|
||||||
|
//
|
||||||
|
//1 <= s.length <= 1000
|
||||||
|
//s[i] 只包含数字('0'-'9')和 '#' 字符。
|
||||||
|
//s 是映射始终存在的有效字符串。
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func freqAlphabet(s string) string {
|
||||||
|
var ret []uint8
|
||||||
|
for i := len(s) - 1; i >= 0; i-- {
|
||||||
|
sb := strings.Builder{}
|
||||||
|
sn := s[i]
|
||||||
|
if sn == '#' {
|
||||||
|
i -= 2
|
||||||
|
sb.WriteByte(s[i])
|
||||||
|
sb.WriteByte(s[i+1])
|
||||||
|
ret = append([]uint8{toChar(sb.String())}, ret...)
|
||||||
|
} else {
|
||||||
|
ret = append([]uint8{toChar(string(s[i]))}, ret...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(ret)
|
||||||
|
}
|
||||||
|
|
||||||
|
func toChar(c string) uint8 {
|
||||||
|
i, _ := strconv.Atoi(c)
|
||||||
|
return 'a' + uint8(i) - 1
|
||||||
|
}
|
||||||
111
20210120/main_test.go
Normal file
111
20210120/main_test.go
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_freqAlphabet(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
s string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{
|
||||||
|
"10#11#12",
|
||||||
|
},
|
||||||
|
"jkab",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test2",
|
||||||
|
args{
|
||||||
|
"1326#",
|
||||||
|
},
|
||||||
|
"acz",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test3",
|
||||||
|
args{
|
||||||
|
"25#",
|
||||||
|
},
|
||||||
|
"y",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test4",
|
||||||
|
args{
|
||||||
|
"12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#",
|
||||||
|
},
|
||||||
|
"abcdefghijklmnopqrstuvwxyz",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := freqAlphabet(tt.args.s); got != tt.want {
|
||||||
|
t.Errorf("freqAlphabet() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_toChar(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
c string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want uint8
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"test1",
|
||||||
|
args{
|
||||||
|
"1",
|
||||||
|
},
|
||||||
|
'a',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test2",
|
||||||
|
args{
|
||||||
|
"2",
|
||||||
|
},
|
||||||
|
'b',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test3",
|
||||||
|
args{
|
||||||
|
"9",
|
||||||
|
},
|
||||||
|
'i',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test4",
|
||||||
|
args{
|
||||||
|
"10",
|
||||||
|
},
|
||||||
|
'j',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test5",
|
||||||
|
args{
|
||||||
|
"10",
|
||||||
|
},
|
||||||
|
'j',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"test6",
|
||||||
|
args{
|
||||||
|
"26",
|
||||||
|
},
|
||||||
|
'z',
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := toChar(tt.args.c); got != tt.want {
|
||||||
|
t.Errorf("toChar() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user