forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |