File tree Expand file tree Collapse file tree 2 files changed +118
-0
lines changed Expand file tree Collapse file tree 2 files changed +118
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ func reformatNumber (number string ) string {
4
+ str , count := "" , 0
5
+ for i := 0 ; i < len (number ); i ++ {
6
+ if number [i ] != '-' && number [i ] != ' ' {
7
+ str += string (number [i ])
8
+ // str = append(str, number[i])
9
+ }
10
+ }
11
+ if len (str ) == 4 {
12
+ str = str [:2 ] + "-" + str [2 :]
13
+ return str
14
+ }
15
+ if len (str ) > 3 {
16
+ if (len (str )- 4 )% 3 == 0 {
17
+ for i := len (str ) - 5 ; i >= 0 ; i -- {
18
+ count ++
19
+ if count % 3 == 0 && i != 0 {
20
+ str = str [:i ] + "-" + str [i :]
21
+ }
22
+ }
23
+ str = str [:len (str )- 2 ] + "-" + str [len (str )- 2 :]
24
+ str = str [:len (str )- 5 ] + "-" + str [len (str )- 5 :]
25
+ return str
26
+ }
27
+ if (len (str )- 2 )% 3 == 0 {
28
+ for i := len (str ) - 3 ; i >= 0 ; i -- {
29
+ count ++
30
+ if count % 3 == 0 && i != 0 {
31
+ str = str [:i ] + "-" + str [i :]
32
+ }
33
+ }
34
+ str = str [:len (str )- 2 ] + "-" + str [len (str )- 2 :]
35
+ return str
36
+ }
37
+ length := len (str )
38
+ count = 0
39
+ for j := 0 ; j < len (str ); j ++ {
40
+ count ++
41
+ if count % 3 == 0 && count != length {
42
+ // head :=
43
+ // tail :=
44
+ str = str [:j + 1 ] + "-" + str [j + 1 :]
45
+ j ++
46
+ }
47
+ }
48
+
49
+ }
50
+ return str
51
+ }
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ import (
4
+ "fmt"
5
+ "testing"
6
+ )
7
+
8
+ type question1690 struct {
9
+ para1690
10
+ ans1690
11
+ }
12
+
13
+ // para 是参数
14
+ // one 代表第一个参数
15
+ type para1690 struct {
16
+ number string
17
+ }
18
+
19
+ // ans 是答案
20
+ // one 代表第一个答案
21
+ type ans1690 struct {
22
+ one string
23
+ }
24
+
25
+ func Test_Problem1690 (t * testing.T ) {
26
+
27
+ qs := []question1690 {
28
+
29
+ {
30
+ para1690 {"1-23-45 6" },
31
+ ans1690 {"123-456" },
32
+ },
33
+
34
+ {
35
+ para1690 {"123 4-567" },
36
+ ans1690 {"123-45-67" },
37
+ },
38
+
39
+ {
40
+ para1690 {"123 4-5678" },
41
+ ans1690 {"123-456-78" },
42
+ },
43
+
44
+ {
45
+ para1690 {"12" },
46
+ ans1690 {"12" },
47
+ },
48
+
49
+ {
50
+ para1690 {"--17-5 229 35-39475 " },
51
+ ans1690 {"175-229-353-94-75" },
52
+ },
53
+
54
+ {
55
+ para1690 {"9964-" },
56
+ ans1690 {"99-64" },
57
+ },
58
+ }
59
+
60
+ fmt .Printf ("------------------------Leetcode Problem 1690------------------------\n " )
61
+
62
+ for _ , q := range qs {
63
+ _ , p := q .ans1690 , q .para1690
64
+ fmt .Printf ("【input】:%v 【output】:%v\n " , p , reformatNumber (p .number ))
65
+ }
66
+ fmt .Printf ("\n \n \n " )
67
+ }
You can’t perform that action at this time.
0 commit comments