Skip to content

Commit 0f5ed3f

Browse files
authored
feat: add solutions to lc problem: No.3602 (#4555)
No.3602.Hexadecimal and Hexatrigesimal Conversion
1 parent b2220d0 commit 0f5ed3f

File tree

7 files changed

+316
-8
lines changed

7 files changed

+316
-8
lines changed

solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README.md

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,137 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3602.He
7070

7171
<!-- solution:start -->
7272

73-
### 方法一
73+
### 方法一:模拟
74+
75+
我们定义一个函数 $\textit{f}(x, k)$,它将整数 $x$ 转换为以 $k$ 进制表示的字符串。该函数通过不断取模和整除来构建结果字符串。
76+
77+
对于给定的整数 $n$,我们计算 $n^2$ 和 $n^3$,然后分别将它们转换为十六进制和三十六进制字符串。最后,将这两个字符串连接起来返回。
78+
79+
时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。
7480

7581
<!-- tabs:start -->
7682

7783
#### Python3
7884

7985
```python
80-
86+
class Solution:
87+
def concatHex36(self, n: int) -> str:
88+
def f(x: int, k: int) -> str:
89+
res = []
90+
while x:
91+
v = x % k
92+
if v <= 9:
93+
res.append(str(v))
94+
else:
95+
res.append(chr(ord("A") + v - 10))
96+
x //= k
97+
return "".join(res[::-1])
98+
99+
x, y = n**2, n**3
100+
return f(x, 16) + f(y, 36)
81101
```
82102

83103
#### Java
84104

85105
```java
86-
106+
class Solution {
107+
public String concatHex36(int n) {
108+
int x = n * n;
109+
int y = n * n * n;
110+
return f(x, 16) + f(y, 36);
111+
}
112+
113+
private String f(int x, int k) {
114+
StringBuilder res = new StringBuilder();
115+
while (x > 0) {
116+
int v = x % k;
117+
if (v <= 9) {
118+
res.append((char) ('0' + v));
119+
} else {
120+
res.append((char) ('A' + v - 10));
121+
}
122+
x /= k;
123+
}
124+
return res.reverse().toString();
125+
}
126+
}
87127
```
88128

89129
#### C++
90130

91131
```cpp
92-
132+
class Solution {
133+
public:
134+
string concatHex36(int n) {
135+
int x = n * n;
136+
int y = n * n * n;
137+
return f(x, 16) + f(y, 36);
138+
}
139+
140+
private:
141+
string f(int x, int k) {
142+
string res;
143+
while (x > 0) {
144+
int v = x % k;
145+
if (v <= 9) {
146+
res += char('0' + v);
147+
} else {
148+
res += char('A' + v - 10);
149+
}
150+
x /= k;
151+
}
152+
reverse(res.begin(), res.end());
153+
return res;
154+
}
155+
};
93156
```
94157
95158
#### Go
96159
97160
```go
161+
func concatHex36(n int) string {
162+
x := n * n
163+
y := n * n * n
164+
return f(x, 16) + f(y, 36)
165+
}
166+
167+
func f(x, k int) string {
168+
res := []byte{}
169+
for x > 0 {
170+
v := x % k
171+
if v <= 9 {
172+
res = append(res, byte('0'+v))
173+
} else {
174+
res = append(res, byte('A'+v-10))
175+
}
176+
x /= k
177+
}
178+
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
179+
res[i], res[j] = res[j], res[i]
180+
}
181+
return string(res)
182+
}
183+
```
98184

185+
#### TypeScript
186+
187+
```ts
188+
function concatHex36(n: number): string {
189+
function f(x: number, k: number): string {
190+
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
191+
let res = '';
192+
while (x > 0) {
193+
const v = x % k;
194+
res = digits[v] + res;
195+
x = Math.floor(x / k);
196+
}
197+
return res;
198+
}
199+
200+
const x = n * n;
201+
const y = n * n * n;
202+
return f(x, 16) + f(y, 36);
203+
}
99204
```
100205

101206
<!-- tabs:end -->

solution/3600-3699/3602.Hexadecimal and Hexatrigesimal Conversion/README_EN.md

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,32 +68,137 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3602.He
6868

6969
<!-- solution:start -->
7070

71-
### Solution 1
71+
### Solution 1: Simulation
72+
73+
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.
74+
75+
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.
76+
77+
The time complexity is $O(\log n)$, and the space complexity is $O(1)$.
7278

7379
<!-- tabs:start -->
7480

7581
#### Python3
7682

7783
```python
78-
84+
class Solution:
85+
def concatHex36(self, n: int) -> str:
86+
def f(x: int, k: int) -> str:
87+
res = []
88+
while x:
89+
v = x % k
90+
if v <= 9:
91+
res.append(str(v))
92+
else:
93+
res.append(chr(ord("A") + v - 10))
94+
x //= k
95+
return "".join(res[::-1])
96+
97+
x, y = n**2, n**3
98+
return f(x, 16) + f(y, 36)
7999
```
80100

81101
#### Java
82102

83103
```java
84-
104+
class Solution {
105+
public String concatHex36(int n) {
106+
int x = n * n;
107+
int y = n * n * n;
108+
return f(x, 16) + f(y, 36);
109+
}
110+
111+
private String f(int x, int k) {
112+
StringBuilder res = new StringBuilder();
113+
while (x > 0) {
114+
int v = x % k;
115+
if (v <= 9) {
116+
res.append((char) ('0' + v));
117+
} else {
118+
res.append((char) ('A' + v - 10));
119+
}
120+
x /= k;
121+
}
122+
return res.reverse().toString();
123+
}
124+
}
85125
```
86126

87127
#### C++
88128

89129
```cpp
90-
130+
class Solution {
131+
public:
132+
string concatHex36(int n) {
133+
int x = n * n;
134+
int y = n * n * n;
135+
return f(x, 16) + f(y, 36);
136+
}
137+
138+
private:
139+
string f(int x, int k) {
140+
string res;
141+
while (x > 0) {
142+
int v = x % k;
143+
if (v <= 9) {
144+
res += char('0' + v);
145+
} else {
146+
res += char('A' + v - 10);
147+
}
148+
x /= k;
149+
}
150+
reverse(res.begin(), res.end());
151+
return res;
152+
}
153+
};
91154
```
92155
93156
#### Go
94157
95158
```go
159+
func concatHex36(n int) string {
160+
x := n * n
161+
y := n * n * n
162+
return f(x, 16) + f(y, 36)
163+
}
164+
165+
func f(x, k int) string {
166+
res := []byte{}
167+
for x > 0 {
168+
v := x % k
169+
if v <= 9 {
170+
res = append(res, byte('0'+v))
171+
} else {
172+
res = append(res, byte('A'+v-10))
173+
}
174+
x /= k
175+
}
176+
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
177+
res[i], res[j] = res[j], res[i]
178+
}
179+
return string(res)
180+
}
181+
```
96182

183+
#### TypeScript
184+
185+
```ts
186+
function concatHex36(n: number): string {
187+
function f(x: number, k: number): string {
188+
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
189+
let res = '';
190+
while (x > 0) {
191+
const v = x % k;
192+
res = digits[v] + res;
193+
x = Math.floor(x / k);
194+
}
195+
return res;
196+
}
197+
198+
const x = n * n;
199+
const y = n * n * n;
200+
return f(x, 16) + f(y, 36);
201+
}
97202
```
98203

99204
<!-- tabs:end -->
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
string concatHex36(int n) {
4+
int x = n * n;
5+
int y = n * n * n;
6+
return f(x, 16) + f(y, 36);
7+
}
8+
9+
private:
10+
string f(int x, int k) {
11+
string res;
12+
while (x > 0) {
13+
int v = x % k;
14+
if (v <= 9) {
15+
res += char('0' + v);
16+
} else {
17+
res += char('A' + v - 10);
18+
}
19+
x /= k;
20+
}
21+
reverse(res.begin(), res.end());
22+
return res;
23+
}
24+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func concatHex36(n int) string {
2+
x := n * n
3+
y := n * n * n
4+
return f(x, 16) + f(y, 36)
5+
}
6+
7+
func f(x, k int) string {
8+
res := []byte{}
9+
for x > 0 {
10+
v := x % k
11+
if v <= 9 {
12+
res = append(res, byte('0'+v))
13+
} else {
14+
res = append(res, byte('A'+v-10))
15+
}
16+
x /= k
17+
}
18+
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
19+
res[i], res[j] = res[j], res[i]
20+
}
21+
return string(res)
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public String concatHex36(int n) {
3+
int x = n * n;
4+
int y = n * n * n;
5+
return f(x, 16) + f(y, 36);
6+
}
7+
8+
private String f(int x, int k) {
9+
StringBuilder res = new StringBuilder();
10+
while (x > 0) {
11+
int v = x % k;
12+
if (v <= 9) {
13+
res.append((char) ('0' + v));
14+
} else {
15+
res.append((char) ('A' + v - 10));
16+
}
17+
x /= k;
18+
}
19+
return res.reverse().toString();
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def concatHex36(self, n: int) -> str:
3+
def f(x: int, k: int) -> str:
4+
res = []
5+
while x:
6+
v = x % k
7+
if v <= 9:
8+
res.append(str(v))
9+
else:
10+
res.append(chr(ord("A") + v - 10))
11+
x //= k
12+
return "".join(res[::-1])
13+
14+
x, y = n**2, n**3
15+
return f(x, 16) + f(y, 36)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function concatHex36(n: number): string {
2+
function f(x: number, k: number): string {
3+
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
4+
let res = '';
5+
while (x > 0) {
6+
const v = x % k;
7+
res = digits[v] + res;
8+
x = Math.floor(x / k);
9+
}
10+
return res;
11+
}
12+
13+
const x = n * n;
14+
const y = n * n * n;
15+
return f(x, 16) + f(y, 36);
16+
}

0 commit comments

Comments
 (0)