Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions leetcode/401-500/0405.Convert-a-Number-to-Hexadecimal/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
# [405.Convert a Number to Hexadecimal][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given an integer `num`, return a string representing its hexadecimal representation. For negative integers, `two’s complement` method is used.

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.

**Note**: You are not allowed to use any built-in library method to directly solve this problem.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: num = 26
Output: "1a"
```

## 题意
> ...
**Example 2**

## 题解

### 思路1
> ...
Convert a Number to Hexadecimal
```go
```

Input: num = -1
Output: "ffffffff"
```

## 结语

Expand Down
83 changes: 81 additions & 2 deletions leetcode/401-500/0405.Convert-a-Number-to-Hexadecimal/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,84 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(num int) string {
n2b := map[int]byte{
0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7',
8: '8', 9: '9', 10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e', 15: 'f',
}
if num >= 0 {
bs := make([]byte, 0)
for num >= 16 {
mod := num % 16
num /= 16
bs = append(bs, n2b[mod])
}
bs = append(bs, n2b[num])
for s, e := 0, len(bs)-1; s < e; s, e = s+1, e-1 {
bs[s], bs[e] = bs[e], bs[s]
}
return string(bs)
}
num = -num
bs := make([]uint8, 32)
bs[0] = 1
end := 31
for num >= 2 {
mod := num % 2
bs[end] = uint8(mod)
num /= 2
end--
}
bs[end] = uint8(num)
cf := uint8(1)
for idx := 31; idx > 0; idx-- {
bs[idx] ^= uint8(1)
bs[idx] += cf
cf = bs[idx] / 2
bs[idx] %= 2
}
power := [4]uint8{1, 2, 4, 8}
result := make([]byte, 8)
bsIdx, idx := 7, 31
for idx > 0 {
base := uint8(0)
for i := 0; i < 4; i++ {
base += bs[idx-i] * power[i]
}
result[bsIdx] = n2b[int(base)]
bsIdx--
idx -= 4
}
// ^=1
return string(result)
}

func Solution2(num int) string {
maxInt := 2147483647 // 2**31-1
minInt := -maxInt - 1 // -2**31
compare := 4294967295 // 2**32-1
if num > maxInt || num < minInt {
return ""
}
if num == 0 {
return "0"
}
if num < 0 {
num = num + 1 + compare
}
numMap := map[int]byte{
0: '0', 1: '1', 2: '2', 3: '3', 4: '4',
5: '5', 6: '6', 7: '7', 8: '8', 9: '9',
10: 'a', 11: 'b', 12: 'c', 13: 'd', 14: 'e',
15: 'f',
}
res := make([]byte, 0)
for num > 0 {
remainer := num % 16
num = num / 16
res = append(res, numMap[remainer])
}
for s, e := 0, len(res)-1; s < e; s, e = s+1, e-1 {
res[s], res[e] = res[e], res[s]
}
return string(res)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs int
expect string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 26, "1a"},
{"TestCase2", -1, "ffffffff"},
{"TestCase3", -10, "fffffff6"},
{"TestCase4", -23478, "ffffa44a"},
{"TestCase5", -2, "fffffffe"},
}

// 开始测试
Expand All @@ -26,6 +28,11 @@ func TestSolution(t *testing.T) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
got = Solution2(c.inputs)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
}
})
}
}
Expand Down