From 951308014cf1c6fa1ba70aa919b022650d8b8a7a Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Sat, 2 Oct 2021 08:17:03 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20405?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\344\275\215\350\277\220\347\256\227.md" | 1 + "Index/\346\250\241\346\213\237.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 99 +++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 "LeetCode/401-410/405. \346\225\260\345\255\227\350\275\254\346\215\242\344\270\272\345\215\201\345\205\255\350\277\233\345\210\266\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\344\275\215\350\277\220\347\256\227.md" "b/Index/\344\275\215\350\277\220\347\256\227.md" index 06e46734..8885b6e9 100644 --- "a/Index/\344\275\215\350\277\220\347\256\227.md" +++ "b/Index/\344\275\215\350\277\220\347\256\227.md" @@ -7,6 +7,7 @@ | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 10f86552..a0046c40 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -24,6 +24,7 @@ | [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/) | 困难 | 🤩🤩🤩🤩 | | [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/) | 简单 | 🤩🤩 | | [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/) | 简单 | 🤩🤩🤩 | +| [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/) | 简单 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | | [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/) | 中等 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/401-410/405. \346\225\260\345\255\227\350\275\254\346\215\242\344\270\272\345\215\201\345\205\255\350\277\233\345\210\266\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/401-410/405. \346\225\260\345\255\227\350\275\254\346\215\242\344\270\272\345\215\201\345\205\255\350\277\233\345\210\266\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..c766400a --- /dev/null +++ "b/LeetCode/401-410/405. \346\225\260\345\255\227\350\275\254\346\215\242\344\270\272\345\215\201\345\205\255\350\277\233\345\210\266\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -0,0 +1,99 @@ +### 题目描述 + +这是 LeetCode 上的 **[405. 数字转换为十六进制数](https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/solution/gong-shui-san-xie-yi-ti-shuang-jie-jin-z-d93o/)** ,难度为 **简单**。 + +Tag : 「位运算」、「模拟」 + +给定一个整数,编写一个算法将这个数转换为十六进制数。对于负整数,我们通常使用 补码运算 方法。 + +注意: +1. 十六进制中所有字母(a-f)都必须是小写。 +2. 十六进制字符串中不能包含多余的前导零。如果要转化的数为0,那么以单个字符'0'来表示;对于其他情况,十六进制字符串中的第一个字符将不会是0字符。  +3. 给定的数确保在32位有符号整数范围内。 +4. 不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。 + +示例 1: +``` +输入:26 + +输出:"1a" +``` +示例 2: +``` +输入:-1 + +输出:"ffffffff" +``` + +--- + +### 模拟 + 进制转换 + +首先,我们可以利用通用的进制转换思路来做,不断循环 `num % k` 和 `num / k` 的操作来构造出 $k$ 进制每一位。 + +但需要处理「补码」问题:对于负数的 $num$,我们需要先在 $num$ 基础上加上 $2^{32}$ 的偏移量,再进行进制转换。 + +代码: +```Java +class Solution { + public String toHex(int _num) { + if (_num == 0) return "0"; + long num = _num; + StringBuilder sb = new StringBuilder(); + if(num < 0) num = (long)(Math.pow(2, 32) + num); + while (num != 0) { + long u = num % 16; + char c = (char)(u + '0'); + if (u >= 10) c = (char)(u - 10 + 'a'); + sb.append(c); + num /= 16; + } + return sb.reverse().toString(); + } +} +``` +* 时间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$ +* 空间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$ + +--- + +### 位运算 + 分组换算 + +将长度为 $32$ 的二进制转换为 $16$ 进制数,本质是对长度为 $32$ 的二进制数进行分组,每 $4$ 个一组(二进制 $(1111)_2$ 表示 $15$,则使用长度为 $4$ 的二进制可以表示 `0-15`)。 + +同时,由于我们是直接对长度为 $32$ 的二进制进行分组转算($4$ 个为一组,共 $8$ 组),而长度为 $32$ 的二进制本身就是使用补码规则来表示的,因此我们无须额外处理「补码」问题。 + +具体的,我们将 $num$ 与 $15$ = $(1111)_2$ 进行 `&` 运算,然后对 $num$ 进行无符号右移 $4$ 位来实现每 $4$ 位处理。 + +代码: +```Java +class Solution { + public String toHex(int num) { + if (num == 0) return "0"; + StringBuilder sb = new StringBuilder(); + while (num != 0) { + int u = num & 15; + char c = (char)(u + '0'); + if (u >= 10) c = (char)(u - 10 + 'a'); + sb.append(c); + num >>>= 4; + } + return sb.reverse().toString(); + } +} +``` +* 时间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$ +* 空间复杂度:复杂度取决于构造的十六进制数的长度,固定为 $C = 8$。整体复杂度为 $O(C)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.405` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 +