Skip to content

Commit

Permalink
Add 5629
Browse files Browse the repository at this point in the history
  • Loading branch information
halfrost committed Dec 30, 2020
1 parent 354c802 commit b9c64a1
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
51 changes: 51 additions & 0 deletions leetcode/777777/5629. Reformat Phone Number.go
Original file line number Diff line number Diff line change
@@ -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
}
67 changes: 67 additions & 0 deletions leetcode/777777/5629. Reformat Phone Number_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit b9c64a1

Please sign in to comment.