Skip to content

Commit 179d82e

Browse files
author
edboffical
committed
add 482 166 405
1 parent 749a6de commit 179d82e

3 files changed

+137
-0
lines changed

convert-a-number-to-hexadecimal.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
// 405 https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/
4+
// 进制转换
5+
func toHex(num int) string {
6+
if num == 0 {
7+
return "0"
8+
}
9+
arr := make([]byte, 0, 8)
10+
for i := 7; i >= 0; i-- {
11+
n := byte(num >> (4 * i) & 0xf)
12+
if n > 0 || len(arr) > 0 {
13+
if n < 10 {
14+
arr = append(arr, n+'0')
15+
} else {
16+
arr = append(arr, n-10+'a')
17+
}
18+
}
19+
}
20+
return string(arr)
21+
}

faction-to-recurring-decimal.go

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import "strconv"
4+
5+
// 166 https://leetcode-cn.com/problems/fraction-to-recurring-decimal/
6+
// 除法模拟
7+
func fractionToDecimal(numerator int, denominator int) string {
8+
result := ""
9+
flag := "" // 符号位
10+
// 将符号位和数字分离开
11+
if denominator == 0 {
12+
return ""
13+
}
14+
if numerator == 0 {
15+
return "0"
16+
}
17+
if numerator*denominator > 0 {
18+
flag = ""
19+
if numerator < 0 {
20+
numerator = numerator * (-1)
21+
denominator = denominator * (-1)
22+
}
23+
} else {
24+
if numerator > 0 {
25+
flag = "-"
26+
denominator = denominator * (-1)
27+
} else {
28+
flag = "-"
29+
numerator = numerator * (-1)
30+
}
31+
}
32+
if (numerator % denominator) == 0 {
33+
return flag + strconv.Itoa(numerator/denominator)
34+
} else {
35+
// 定义整数和小数
36+
integer := 0
37+
// 若是被除数 > 除数,则算出商的整数部分
38+
for i := 1; numerator > i*denominator; i++ {
39+
integer++
40+
}
41+
result += strconv.Itoa(integer)
42+
result += "."
43+
numerator -= integer * denominator
44+
// 现在,被除数小于除数了,商的整数部分也算出来了,该算小数部分了
45+
hashMap := make(map[int]int) // 用于记录余数出现的位置,key是数字,value是其在quotient数组里的index
46+
var quotient []int // 用于记录每次出现的小数部分商
47+
j := 0
48+
for numerator%denominator != 0 {
49+
if numerator < denominator {
50+
numerator *= 10
51+
}
52+
quotientRes := numerator / denominator
53+
numerator %= denominator
54+
if _, ok := hashMap[numerator]; !ok {
55+
// 表明没有循环,保存商和余数
56+
quotient = append(quotient, quotientRes)
57+
hashMap[numerator] = j
58+
j++
59+
} else {
60+
// 表明有循环
61+
if quotient[hashMap[numerator]] == quotientRes {
62+
for k := 0; k < hashMap[numerator] && k < len(quotient); k++ {
63+
result += strconv.Itoa(quotient[k])
64+
}
65+
result += "("
66+
for k := hashMap[numerator]; k < len(quotient); k++ {
67+
result += strconv.Itoa(quotient[k])
68+
}
69+
result += ")"
70+
} else {
71+
// 下面这段代码用于防止1/6这种情况的出现,即:余数重复出现,不一定代表就此循环
72+
for k := 0; k <= hashMap[numerator] && k < len(quotient); k++ {
73+
result += strconv.Itoa(quotient[k])
74+
}
75+
result += "("
76+
for k := hashMap[numerator] + 1; k < len(quotient); k++ {
77+
result += strconv.Itoa(quotient[k])
78+
}
79+
result += strconv.Itoa(quotientRes)
80+
result += ")"
81+
}
82+
break
83+
}
84+
// 表明除尽了
85+
if numerator == 0 {
86+
for _, item := range quotient {
87+
result += strconv.Itoa(item)
88+
}
89+
break
90+
}
91+
}
92+
}
93+
result = flag + result
94+
return result
95+
}

license-key-formatting.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "strings"
4+
5+
// 482 https://leetcode-cn.com/problems/license-key-formatting/
6+
func licenseKeyFormatting(s string, k int) string {
7+
s = strings.ReplaceAll(s, "-", "")
8+
if s == "" {
9+
return ""
10+
}
11+
firstCount := len(s) % k
12+
if firstCount == 0 {
13+
firstCount = k
14+
}
15+
var res = []string{s[:firstCount]}
16+
17+
for i := 0; i < (len(s)-firstCount)/k; i++ {
18+
res = append(res, s[firstCount+i*k:firstCount+(i+1)*k])
19+
}
20+
return strings.ToUpper(strings.Join(res, "-"))
21+
}

0 commit comments

Comments
 (0)