From 25a75b97ec457b8ada2e56d87edca05eae66f94a Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 7 Jul 2025 07:14:30 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3602 No.3602.Hexadecimal and Hexatrigesimal Conversion --- .../README.md | 113 +++++++++++++++++- .../README_EN.md | 113 +++++++++++++++++- .../Solution.cpp | 24 ++++ .../Solution.go | 22 ++++ .../Solution.java | 21 ++++ .../Solution.py | 15 +++ .../Solution.ts | 16 +++ 7 files changed, 316 insertions(+), 8 deletions(-) create mode 100644 solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.cpp create mode 100644 solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.go create mode 100644 solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.java create mode 100644 solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.py create mode 100644 solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.ts diff --git a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README.md b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README.md index c9b4883337243..46ae3239856ef 100644 --- a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README.md +++ b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README.md @@ -70,32 +70,137 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3602.He -### 方法一 +### 方法一:模拟 + +我们定义一个函数 $\textit{f}(x, k)$,它将整数 $x$ 转换为以 $k$ 进制表示的字符串。该函数通过不断取模和整除来构建结果字符串。 + +对于给定的整数 $n$,我们计算 $n^2$ 和 $n^3$,然后分别将它们转换为十六进制和三十六进制字符串。最后,将这两个字符串连接起来返回。 + +时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。 #### Python3 ```python - +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) ``` #### Java ```java - +class Solution { + public String concatHex36(int n) { + int x = n * n; + int y = n * n * n; + return f(x, 16) + f(y, 36); + } + + private String f(int x, int k) { + StringBuilder res = new StringBuilder(); + while (x > 0) { + int v = x % k; + if (v <= 9) { + res.append((char) ('0' + v)); + } else { + res.append((char) ('A' + v - 10)); + } + x /= k; + } + return res.reverse().toString(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string concatHex36(int n) { + int x = n * n; + int y = n * n * n; + return f(x, 16) + f(y, 36); + } + +private: + string f(int x, int k) { + string res; + while (x > 0) { + int v = x % k; + if (v <= 9) { + res += char('0' + v); + } else { + res += char('A' + v - 10); + } + x /= k; + } + reverse(res.begin(), res.end()); + return res; + } +}; ``` #### Go ```go +func concatHex36(n int) string { + x := n * n + y := n * n * n + return f(x, 16) + f(y, 36) +} + +func f(x, k int) string { + res := []byte{} + for x > 0 { + v := x % k + if v <= 9 { + res = append(res, byte('0'+v)) + } else { + res = append(res, byte('A'+v-10)) + } + x /= k + } + for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 { + res[i], res[j] = res[j], res[i] + } + return string(res) +} +``` +#### TypeScript + +```ts +function concatHex36(n: number): string { + function f(x: number, k: number): string { + const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + let res = ''; + while (x > 0) { + const v = x % k; + res = digits[v] + res; + x = Math.floor(x / k); + } + return res; + } + + const x = n * n; + const y = n * n * n; + return f(x, 16) + f(y, 36); +} ``` diff --git a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README_EN.md b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README_EN.md index 1373f60f41fe6..2cf126db83147 100644 --- a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README_EN.md +++ b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README_EN.md @@ -68,32 +68,137 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3602.He -### Solution 1 +### Solution 1: Simulation + +We define a function $\textit{f}(x, k)$, which converts an integer $x$ to its string representation in base $k$. This function constructs the result string by repeatedly taking the modulus and dividing. + +For a given integer $n$, we compute $n^2$ and $n^3$, then convert them to hexadecimal and base-36 strings, respectively. Finally, we concatenate these two strings and return the result. + +The time complexity is $O(\log n)$, and the space complexity is $O(1)$. #### Python3 ```python - +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) ``` #### Java ```java - +class Solution { + public String concatHex36(int n) { + int x = n * n; + int y = n * n * n; + return f(x, 16) + f(y, 36); + } + + private String f(int x, int k) { + StringBuilder res = new StringBuilder(); + while (x > 0) { + int v = x % k; + if (v <= 9) { + res.append((char) ('0' + v)); + } else { + res.append((char) ('A' + v - 10)); + } + x /= k; + } + return res.reverse().toString(); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string concatHex36(int n) { + int x = n * n; + int y = n * n * n; + return f(x, 16) + f(y, 36); + } + +private: + string f(int x, int k) { + string res; + while (x > 0) { + int v = x % k; + if (v <= 9) { + res += char('0' + v); + } else { + res += char('A' + v - 10); + } + x /= k; + } + reverse(res.begin(), res.end()); + return res; + } +}; ``` #### Go ```go +func concatHex36(n int) string { + x := n * n + y := n * n * n + return f(x, 16) + f(y, 36) +} + +func f(x, k int) string { + res := []byte{} + for x > 0 { + v := x % k + if v <= 9 { + res = append(res, byte('0'+v)) + } else { + res = append(res, byte('A'+v-10)) + } + x /= k + } + for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 { + res[i], res[j] = res[j], res[i] + } + return string(res) +} +``` +#### TypeScript + +```ts +function concatHex36(n: number): string { + function f(x: number, k: number): string { + const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + let res = ''; + while (x > 0) { + const v = x % k; + res = digits[v] + res; + x = Math.floor(x / k); + } + return res; + } + + const x = n * n; + const y = n * n * n; + return f(x, 16) + f(y, 36); +} ``` diff --git a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.cpp b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.cpp new file mode 100644 index 0000000000000..1bbe88ac4d91d --- /dev/null +++ b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + string concatHex36(int n) { + int x = n * n; + int y = n * n * n; + return f(x, 16) + f(y, 36); + } + +private: + string f(int x, int k) { + string res; + while (x > 0) { + int v = x % k; + if (v <= 9) { + res += char('0' + v); + } else { + res += char('A' + v - 10); + } + x /= k; + } + reverse(res.begin(), res.end()); + return res; + } +}; diff --git a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.go b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.go new file mode 100644 index 0000000000000..4c81f74a18789 --- /dev/null +++ b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.go @@ -0,0 +1,22 @@ +func concatHex36(n int) string { + x := n * n + y := n * n * n + return f(x, 16) + f(y, 36) +} + +func f(x, k int) string { + res := []byte{} + for x > 0 { + v := x % k + if v <= 9 { + res = append(res, byte('0'+v)) + } else { + res = append(res, byte('A'+v-10)) + } + x /= k + } + for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 { + res[i], res[j] = res[j], res[i] + } + return string(res) +} diff --git a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.java b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.java new file mode 100644 index 0000000000000..3e369d16c1c0d --- /dev/null +++ b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.java @@ -0,0 +1,21 @@ +class Solution { + public String concatHex36(int n) { + int x = n * n; + int y = n * n * n; + return f(x, 16) + f(y, 36); + } + + private String f(int x, int k) { + StringBuilder res = new StringBuilder(); + while (x > 0) { + int v = x % k; + if (v <= 9) { + res.append((char) ('0' + v)); + } else { + res.append((char) ('A' + v - 10)); + } + x /= k; + } + return res.reverse().toString(); + } +} diff --git a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.py b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.py new file mode 100644 index 0000000000000..691d16e9a718d --- /dev/null +++ b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.py @@ -0,0 +1,15 @@ +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) diff --git a/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.ts b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.ts new file mode 100644 index 0000000000000..98eeb88a2bf20 --- /dev/null +++ b/solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/Solution.ts @@ -0,0 +1,16 @@ +function concatHex36(n: number): string { + function f(x: number, k: number): string { + const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + let res = ''; + while (x > 0) { + const v = x % k; + res = digits[v] + res; + x = Math.floor(x / k); + } + return res; + } + + const x = n * n; + const y = n * n * n; + return f(x, 16) + f(y, 36); +}