Skip to content

Commit e2d7405

Browse files
authored
Merge pull request 6boris#240 from 0xff-dev/405
Add solution and test-cases for problem 405
2 parents 35799fb + f901e3d commit e2d7405

File tree

3 files changed

+104
-21
lines changed

3 files changed

+104
-21
lines changed

leetcode/401-500/0405.Convert-a-Number-to-Hexadecimal/README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [405.Convert a Number to Hexadecimal][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given an integer `num`, return a string representing its hexadecimal representation. For negative integers, `two’s complement` method is used.
5+
6+
All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.
7+
8+
**Note**: You are not allowed to use any built-in library method to directly solve this problem.
79

810
**Example 1:**
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: num = 26
14+
Output: "1a"
1315
```
1416

15-
## 题意
16-
> ...
17+
**Example 2**
1718

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Convert a Number to Hexadecimal
23-
```go
2419
```
25-
20+
Input: num = -1
21+
Output: "ffffffff"
22+
```
2623

2724
## 结语
2825

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,84 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(num int) string {
4+
n2b := map[int]byte{
5+
0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7',
6+
8: '8', 9: '9', 10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f',
7+
}
8+
if num >= 0 {
9+
bs := make([]byte, 0)
10+
for num >= 16 {
11+
mod := num % 16
12+
num /= 16
13+
bs = append(bs, n2b[mod])
14+
}
15+
bs = append(bs, n2b[num])
16+
for s, e := 0, len(bs)-1; s < e; s, e = s+1, e-1 {
17+
bs[s], bs[e] = bs[e], bs[s]
18+
}
19+
return string(bs)
20+
}
21+
num = -num
22+
bs := make([]uint8, 32)
23+
bs[0] = 1
24+
end := 31
25+
for num >= 2 {
26+
mod := num % 2
27+
bs[end] = uint8(mod)
28+
num /= 2
29+
end--
30+
}
31+
bs[end] = uint8(num)
32+
cf := uint8(1)
33+
for idx := 31; idx > 0; idx-- {
34+
bs[idx] ^= uint8(1)
35+
bs[idx] += cf
36+
cf = bs[idx] / 2
37+
bs[idx] %= 2
38+
}
39+
power := [4]uint8{1, 2, 4, 8}
40+
result := make([]byte, 8)
41+
bsIdx, idx := 7, 31
42+
for idx > 0 {
43+
base := uint8(0)
44+
for i := 0; i < 4; i++ {
45+
base += bs[idx-i] * power[i]
46+
}
47+
result[bsIdx] = n2b[int(base)]
48+
bsIdx--
49+
idx -= 4
50+
}
51+
// ^=1
52+
return string(result)
53+
}
54+
55+
func Solution2(num int) string {
56+
maxInt := 2147483647 // 2**31-1
57+
minInt := -maxInt - 1 // -2**31
58+
compare := 4294967295 // 2**32-1
59+
if num > maxInt || num < minInt {
60+
return ""
61+
}
62+
if num == 0 {
63+
return "0"
64+
}
65+
if num < 0 {
66+
num = num + 1 + compare
67+
}
68+
numMap := map[int]byte{
69+
0: '0', 1: '1', 2: '2', 3: '3', 4: '4',
70+
5: '5', 6: '6', 7: '7', 8: '8', 9: '9',
71+
10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e',
72+
15: 'f',
73+
}
74+
res := make([]byte, 0)
75+
for num > 0 {
76+
remainer := num % 16
77+
num = num / 16
78+
res = append(res, numMap[remainer])
79+
}
80+
for s, e := 0, len(res)-1; s < e; s, e = s+1, e-1 {
81+
res[s], res[e] = res[e], res[s]
82+
}
83+
return string(res)
584
}

leetcode/401-500/0405.Convert-a-Number-to-Hexadecimal/Solution_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 26, "1a"},
17+
{"TestCase2", -1, "ffffffff"},
18+
{"TestCase3", -10, "fffffff6"},
19+
{"TestCase4", -23478, "ffffa44a"},
20+
{"TestCase5", -2, "fffffffe"},
1921
}
2022

2123
// 开始测试
@@ -26,6 +28,11 @@ func TestSolution(t *testing.T) {
2628
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
2729
c.expect, got, c.inputs)
2830
}
31+
got = Solution2(c.inputs)
32+
if !reflect.DeepEqual(got, c.expect) {
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
34+
c.expect, got, c.inputs)
35+
}
2936
})
3037
}
3138
}

0 commit comments

Comments
 (0)