diff --git a/leetcode/777777/5629. Reformat Phone Number.go b/leetcode/777777/5629. Reformat Phone Number.go new file mode 100644 index 000000000..70795214f --- /dev/null +++ b/leetcode/777777/5629. Reformat Phone Number.go @@ -0,0 +1,51 @@ +package leetcode + +func reformatNumber(number string) string { + str, count := "", 0 + for i := 0; i < len(number); i++ { + if number[i] != '-' && number[i] != ' ' { + str += string(number[i]) + // str = append(str, number[i]) + } + } + if len(str) == 4 { + str = str[:2] + "-" + str[2:] + return str + } + if len(str) > 3 { + if (len(str)-4)%3 == 0 { + for i := len(str) - 5; i >= 0; i-- { + count++ + if count%3 == 0 && i != 0 { + str = str[:i] + "-" + str[i:] + } + } + str = str[:len(str)-2] + "-" + str[len(str)-2:] + str = str[:len(str)-5] + "-" + str[len(str)-5:] + return str + } + if (len(str)-2)%3 == 0 { + for i := len(str) - 3; i >= 0; i-- { + count++ + if count%3 == 0 && i != 0 { + str = str[:i] + "-" + str[i:] + } + } + str = str[:len(str)-2] + "-" + str[len(str)-2:] + return str + } + length := len(str) + count = 0 + for j := 0; j < len(str); j++ { + count++ + if count%3 == 0 && count != length { + // head := + // tail := + str = str[:j+1] + "-" + str[j+1:] + j++ + } + } + + } + return str +} diff --git a/leetcode/777777/5629. Reformat Phone Number_test.go b/leetcode/777777/5629. Reformat Phone Number_test.go new file mode 100644 index 000000000..8aba62c99 --- /dev/null +++ b/leetcode/777777/5629. Reformat Phone Number_test.go @@ -0,0 +1,67 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question1690 struct { + para1690 + ans1690 +} + +// para 是参数 +// one 代表第一个参数 +type para1690 struct { + number string +} + +// ans 是答案 +// one 代表第一个答案 +type ans1690 struct { + one string +} + +func Test_Problem1690(t *testing.T) { + + qs := []question1690{ + + { + para1690{"1-23-45 6"}, + ans1690{"123-456"}, + }, + + { + para1690{"123 4-567"}, + ans1690{"123-45-67"}, + }, + + { + para1690{"123 4-5678"}, + ans1690{"123-456-78"}, + }, + + { + para1690{"12"}, + ans1690{"12"}, + }, + + { + para1690{"--17-5 229 35-39475 "}, + ans1690{"175-229-353-94-75"}, + }, + + { + para1690{"9964-"}, + ans1690{"99-64"}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 1690------------------------\n") + + for _, q := range qs { + _, p := q.ans1690, q.para1690 + fmt.Printf("【input】:%v 【output】:%v\n", p, reformatNumber(p.number)) + } + fmt.Printf("\n\n\n") +}