LeetCode 题解工作台

十六进制和三十六进制转化

给你一个整数 n 。 返回 n 2 的 十六进制表示 和 n 3 的 三十六进制表示 拼接成的字符串。 十六进制 数定义为使用数字 0 – 9 和大写字母 A - F 表示 0 到 15 的值。 三十六进制 数定义为使用数字 0 – 9 和大写字母 A - Z 表示 0 到 35 的值。 示例 1:…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · 数学·string

bolt

答案摘要

我们定义一个函数 $\textit{f}(x, k)$,它将整数 转换为以 进制表示的字符串。该函数通过不断取模和整除来构建结果字符串。 对于给定的整数 ,我们计算 和 ,然后分别将它们转换为十六进制和三十六进制字符串。最后,将这两个字符串连接起来返回。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 数学·string 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个整数 n

返回 n2 的 十六进制表示n3 的 三十六进制表示 拼接成的字符串。

十六进制 数定义为使用数字 0 – 9 和大写字母 A - F 表示 0 到 15 的值。

三十六进制 数定义为使用数字 0 – 9 和大写字母 A - Z 表示 0 到 35 的值。

 

示例 1:

输入:n = 13

输出: "A91P1"

解释:

  • n2 = 13 * 13 = 169。在十六进制中,它转换为 (10 * 16) + 9 = 169,对应于 "A9"
  • n3 = 13 * 13 * 13 = 2197。在三十六进制中,它转换为 (1 * 362) + (25 * 36) + 1 = 2197,对应于 "1P1"
  • 连接两个结果得到 "A9" + "1P1" = "A91P1"

示例 2:

输入:n = 36

输出:"5101000"

解释:

  • n2 = 36 * 36 = 1296。在十六进制中,它转换为 (5 * 162) + (1 * 16) + 0 = 1296,对应于 "510"
  • n3 = 36 * 36 * 36 = 46656。在三十六进制中,它转换为 (1 * 363) + (0 * 362) + (0 * 36) + 0 = 46656,对应于 "1000"
  • 连接两个结果得到 "510" + "1000" = "5101000"

 

提示:

  • 1 <= n <= 1000
lightbulb

解题思路

方法一:模拟

我们定义一个函数 f(x,k)\textit{f}(x, k),它将整数 xx 转换为以 kk 进制表示的字符串。该函数通过不断取模和整除来构建结果字符串。

对于给定的整数 nn,我们计算 n2n^2n3n^3,然后分别将它们转换为十六进制和三十六进制字符串。最后,将这两个字符串连接起来返回。

时间复杂度 O(logn)O(\log n),空间复杂度 O(1)O(1)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def concatHex36(self, n: int) -> str:
        def f(x: int, k: int) -> str:
            res = []
            while x:
                v = x % k
                if v <= 9:
                    res.append(str(v))
                else:
                    res.append(chr(ord("A") + v - 10))
                x //= k
            return "".join(res[::-1])

        x, y = n**2, n**3
        return f(x, 16) + f(y, 36)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    You notice the problem is really two base conversions glued together, not a tricky math identity.

  • question_mark

    You suggest a generic toBase(x, b) helper with a 0-9 and A-Z digit table instead of hardcoding hexadecimal and base 36 separately.

  • question_mark

    You explicitly guard against the order bug: convert n squared to hexadecimal first, then n cubed to hexatrigesimal.

warning

常见陷阱

外企场景
  • error

    Using lowercase letters or the wrong digit table, which breaks the required uppercase output for both bases.

  • error

    Forgetting to reverse the collected digits after repeated modulus and division, producing the representation backward.

  • error

    Mixing up the powers or output order, such as converting n cubed to base 16 or concatenating the base 36 part first.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Return the two converted strings as separate fields instead of one concatenated result, which removes the final join but keeps the same helper.

  • arrow_right_alt

    Change the bases to another pair such as binary and base 7, which tests whether the conversion logic is truly generic.

  • arrow_right_alt

    Allow n = 0, which adds the classic edge case where toBase must return 0 instead of an empty string.

help

常见问题

外企场景

十六进制和三十六进制转化题解:数学·string | LeetCode #3602 简单