Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad1fc5d

Browse files
Merge pull request SharingSource#162 from SharingSource/ac_oier
✨feat: Add 405
2 parents 420a44e + 9513080 commit ad1fc5d

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

‎Index/位运算.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
| [338. 比特位计数](https://leetcode-cn.com/problems/counting-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/counting-bits/solution/po-su-jie-fa-dong-tai-gui-hua-jie-fa-by-vvail/) | 简单 | 🤩🤩🤩 |
88
| [342. 4的幂](https://leetcode-cn.com/problems/power-of-four/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/power-of-four/solution/gong-shui-san-xie-zhuan-hua-wei-2-de-mi-y21lq/) | 简单 | 🤩🤩🤩 |
99
| [371. 两整数之和](https://leetcode-cn.com/problems/sum-of-two-integers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-two-integers/solution/gong-shui-san-xie-shi-yong-wei-yun-suan-4hpb7/) | 中等 | 🤩🤩🤩🤩 |
10+
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
1011
| [461. 汉明距离](https://leetcode-cn.com/problems/hamming-distance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/hamming-distance/solution/gong-shui-san-xie-tong-ji-liang-shu-er-j-987a/) | 简单 | 🤩🤩🤩🤩 |
1112
| [477. 汉明距离总和](https://leetcode-cn.com/problems/total-hamming-distance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/total-hamming-distance/solution/gong-shui-san-xie-ying-yong-cheng-fa-yua-g21t/) | 简单 | 🤩🤩🤩🤩 |
1213
| [526. 优美的排列](https://leetcode-cn.com/problems/beautiful-arrangement/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/beautiful-arrangement/solution/gong-shui-san-xie-xiang-jie-liang-chong-vgsia/) | 中等 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| [233. 数字 1 的个数](https://leetcode-cn.com/problems/number-of-digit-one/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-digit-one/solution/gong-shui-san-xie-jiang-shu-wei-dp-wen-t-c9oi/) | 困难 | 🤩🤩🤩🤩 |
2525
| [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ugly-number/solution/gong-shui-san-xie-jian-dan-de-fen-qing-k-dlvg/) | 简单 | 🤩🤩 |
2626
| [345. 反转字符串中的元音字母](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-vowels-of-a-string/solution/gong-shui-san-xie-note-bie-pian-shuang-z-c8ii/) | 简单 | 🤩🤩🤩 |
27+
| [405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/) | 简单 | 🤩🤩🤩🤩 |
2728
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |
2829
| [443. 压缩字符串](https://leetcode-cn.com/problems/string-compression/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/string-compression/solution/gong-shui-san-xie-shuang-zhi-zhen-yuan-d-bppu/) | 中等 | 🤩🤩🤩🤩 |
2930
| [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-characters-by-frequency/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-gst9/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/)** ,难度为 **简单**
4+
5+
Tag : 「位运算」、「模拟」
6+
7+
给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。
8+
9+
注意:
10+
1. 十六进制中所有字母(a-f)都必须是小写。
11+
2. 十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符'0'来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。
12+
3. 给定的数确保在32位有符号整数范围内。
13+
4. 不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。
14+
15+
示例 1:
16+
```
17+
输入:26
18+
19+
输出:"1a"
20+
```
21+
示例 2:
22+
```
23+
输入:-1
24+
25+
输出:"ffffffff"
26+
```
27+
28+
---
29+
30+
### 模拟 + 进制转换
31+
32+
首先,我们可以利用通用的进制转换思路来做,不断循环 `num % k``num / k` 的操作来构造出 $k$ 进制每一位。
33+
34+
但需要处理「补码」问题:对于负数的 $num,ドル我们需要先在 $num$ 基础上加上 2ドル^{32}$ 的偏移量,再进行进制转换。
35+
36+
代码:
37+
```Java
38+
class Solution {
39+
public String toHex(int _num) {
40+
if (_num == 0) return "0";
41+
long num = _num;
42+
StringBuilder sb = new StringBuilder();
43+
if(num < 0) num = (long)(Math.pow(2, 32) + num);
44+
while (num != 0) {
45+
long u = num % 16;
46+
char c = (char)(u + '0');
47+
if (u >= 10) c = (char)(u - 10 + 'a');
48+
sb.append(c);
49+
num /= 16;
50+
}
51+
return sb.reverse().toString();
52+
}
53+
}
54+
```
55+
* 时间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$
56+
* 空间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$
57+
58+
---
59+
60+
### 位运算 + 分组换算
61+
62+
将长度为 32ドル$ 的二进制转换为 16ドル$ 进制数,本质是对长度为 32ドル$ 的二进制数进行分组,每 4ドル$ 个一组(二进制 $(1111)_2$ 表示 15ドル,ドル则使用长度为 4ドル$ 的二进制可以表示 `0-15`)。
63+
64+
同时,由于我们是直接对长度为 32ドル$ 的二进制进行分组转算(4ドル$ 个为一组,共 8ドル$ 组),而长度为 32ドル$ 的二进制本身就是使用补码规则来表示的,因此我们无须额外处理「补码」问题。
65+
66+
具体的,我们将 $num$ 与 15ドル$ = $(1111)_2$ 进行 `&` 运算,然后对 $num$ 进行无符号右移 4ドル$ 位来实现每 4ドル$ 位处理。
67+
68+
代码:
69+
```Java
70+
class Solution {
71+
public String toHex(int num) {
72+
if (num == 0) return "0";
73+
StringBuilder sb = new StringBuilder();
74+
while (num != 0) {
75+
int u = num & 15;
76+
char c = (char)(u + '0');
77+
if (u >= 10) c = (char)(u - 10 + 'a');
78+
sb.append(c);
79+
num >>>= 4;
80+
}
81+
return sb.reverse().toString();
82+
}
83+
}
84+
```
85+
* 时间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$
86+
* 空间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$
87+
88+
---
89+
90+
### 最后
91+
92+
这是我们「刷穿 LeetCode」系列文章的第 `No.405` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
93+
94+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
95+
96+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
97+
98+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
99+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /