Skip to content

Commit 52b62c0

Browse files
committed
feat: add solutions to lc problem: No.1271
No.1271.Hexspeak
1 parent d516e22 commit 52b62c0

File tree

6 files changed

+159
-2
lines changed

6 files changed

+159
-2
lines changed

solution/1200-1299/1271.Hexspeak/README.md

+60-1
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,81 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
**方法一:模拟**
45+
46+
将数字转换为十六进制字符串,然后遍历字符串,将数字 0 转换为字母 O,将数字 1 转换为字母 I,最后判断转换后的字符串是否合法。
47+
48+
时间复杂度 $O(\log n)$,其中 $n$ 为 `num` 的长度。
49+
4450
<!-- tabs:start -->
4551

4652
### **Python3**
4753

4854
<!-- 这里可写当前语言的特殊实现逻辑 -->
4955

5056
```python
51-
57+
class Solution:
58+
def toHexspeak(self, num: str) -> str:
59+
s = set('ABCDEFIO')
60+
t = hex(int(num))[2:].upper().replace('0', 'O').replace('1', 'I')
61+
return t if all(c in s for c in t) else 'ERROR'
5262
```
5363

5464
### **Java**
5565

5666
<!-- 这里可写当前语言的特殊实现逻辑 -->
5767

5868
```java
69+
class Solution {
70+
private static final Set<Character> S = Set.of('A', 'B', 'C', 'D', 'E', 'F', 'I', 'O');
71+
72+
public String toHexspeak(String num) {
73+
String t = Long.toHexString(Long.valueOf(num)).toUpperCase().replace("0", "O").replace("1", "I");
74+
for (char c : t.toCharArray()) {
75+
if (!S.contains(c)) {
76+
return "ERROR";
77+
}
78+
}
79+
return t;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
string toHexspeak(string num) {
90+
stringstream ss;
91+
ss << hex << stol(num);
92+
string t = ss.str();
93+
for (int i = 0; i < t.size(); ++i) {
94+
if (t[i] >= '2' && t[i] <= '9') return "ERROR";
95+
if (t[i] == '0') t[i] = 'O';
96+
else if (t[i] == '1') t[i] = 'I';
97+
else t[i] = t[i] - 32;
98+
}
99+
return t;
100+
}
101+
};
102+
```
59103
104+
### **Go**
105+
106+
```go
107+
func toHexspeak(num string) string {
108+
x, _ := strconv.Atoi(num)
109+
t := strings.ToUpper(fmt.Sprintf("%x", x))
110+
t = strings.ReplaceAll(t, "0", "O")
111+
t = strings.ReplaceAll(t, "1", "I")
112+
for _, c := range t {
113+
if c >= '2' && c <= '9' {
114+
return "ERROR"
115+
}
116+
}
117+
return t
118+
}
60119
```
61120

62121
### **...**

solution/1200-1299/1271.Hexspeak/README_EN.md

+54-1
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,66 @@
4040
### **Python3**
4141

4242
```python
43-
43+
class Solution:
44+
def toHexspeak(self, num: str) -> str:
45+
s = set('ABCDEFIO')
46+
t = hex(int(num))[2:].upper().replace('0', 'O').replace('1', 'I')
47+
return t if all(c in s for c in t) else 'ERROR'
4448
```
4549

4650
### **Java**
4751

4852
```java
53+
class Solution {
54+
private static final Set<Character> S = Set.of('A', 'B', 'C', 'D', 'E', 'F', 'I', 'O');
55+
56+
public String toHexspeak(String num) {
57+
String t = Long.toHexString(Long.valueOf(num)).toUpperCase().replace("0", "O").replace("1", "I");
58+
for (char c : t.toCharArray()) {
59+
if (!S.contains(c)) {
60+
return "ERROR";
61+
}
62+
}
63+
return t;
64+
}
65+
}
66+
```
67+
68+
### **C++**
69+
70+
```cpp
71+
class Solution {
72+
public:
73+
string toHexspeak(string num) {
74+
stringstream ss;
75+
ss << hex << stol(num);
76+
string t = ss.str();
77+
for (int i = 0; i < t.size(); ++i) {
78+
if (t[i] >= '2' && t[i] <= '9') return "ERROR";
79+
if (t[i] == '0') t[i] = 'O';
80+
else if (t[i] == '1') t[i] = 'I';
81+
else t[i] = t[i] - 32;
82+
}
83+
return t;
84+
}
85+
};
86+
```
4987
88+
### **Go**
89+
90+
```go
91+
func toHexspeak(num string) string {
92+
x, _ := strconv.Atoi(num)
93+
t := strings.ToUpper(fmt.Sprintf("%x", x))
94+
t = strings.ReplaceAll(t, "0", "O")
95+
t = strings.ReplaceAll(t, "1", "I")
96+
for _, c := range t {
97+
if c >= '2' && c <= '9' {
98+
return "ERROR"
99+
}
100+
}
101+
return t
102+
}
50103
```
51104

52105
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string toHexspeak(string num) {
4+
stringstream ss;
5+
ss << hex << stol(num);
6+
string t = ss.str();
7+
for (int i = 0; i < t.size(); ++i) {
8+
if (t[i] >= '2' && t[i] <= '9') return "ERROR";
9+
if (t[i] == '0') t[i] = 'O';
10+
else if (t[i] == '1') t[i] = 'I';
11+
else t[i] = t[i] - 32;
12+
}
13+
return t;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
func toHexspeak(num string) string {
2+
x, _ := strconv.Atoi(num)
3+
t := strings.ToUpper(fmt.Sprintf("%x", x))
4+
t = strings.ReplaceAll(t, "0", "O")
5+
t = strings.ReplaceAll(t, "1", "I")
6+
for _, c := range t {
7+
if c >= '2' && c <= '9' {
8+
return "ERROR"
9+
}
10+
}
11+
return t
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
private static final Set<Character> S = Set.of('A', 'B', 'C', 'D', 'E', 'F', 'I', 'O');
3+
4+
public String toHexspeak(String num) {
5+
String t = Long.toHexString(Long.valueOf(num)).toUpperCase().replace("0", "O").replace("1", "I");
6+
for (char c : t.toCharArray()) {
7+
if (!S.contains(c)) {
8+
return "ERROR";
9+
}
10+
}
11+
return t;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def toHexspeak(self, num: str) -> str:
3+
s = set('ABCDEFIO')
4+
t = hex(int(num))[2:].upper().replace('0', 'O').replace('1', 'I')
5+
return t if all(c in s for c in t) else 'ERROR'

0 commit comments

Comments
 (0)