Skip to content

Commit adc0b0e

Browse files
committed
feat: add solutions to lc problem: No.0800
No.0800.Similar RGB Color
1 parent 4147252 commit adc0b0e

File tree

6 files changed

+120
-6
lines changed

6 files changed

+120
-6
lines changed

solution/0500-0599/0591.Tag Validator/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ def check(tag):
99
while i < n:
1010
if i and not stk:
1111
return False
12-
if code[i: i + 9] == '<![CDATA[':
12+
if code[i : i + 9] == '<![CDATA[':
1313
i = code.find(']]>', i + 9)
1414
if i < 0:
1515
return False
1616
i += 2
17-
elif code[i: i + 2] == '</':
17+
elif code[i : i + 2] == '</':
1818
j = i + 2
1919
i = code.find('>', j)
2020
if i < 0:
2121
return False
22-
t = code[j: i]
22+
t = code[j:i]
2323
if not check(t) or not stk or stk.pop() != t:
2424
return False
2525
elif code[i] == '<':
2626
j = i + 1
2727
i = code.find('>', j)
2828
if i < 0:
2929
return False
30-
t = code[j: i]
30+
t = code[j:i]
3131
if not check(t):
3232
return False
3333
stk.append(t)

solution/0800-0899/0800.Similar RGB Color/README.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,54 @@
5858
<!-- 这里可写当前语言的特殊实现逻辑 -->
5959

6060
```python
61-
61+
class Solution:
62+
def similarRGB(self, color: str) -> str:
63+
def f(x):
64+
y, z = divmod(int(x, 16), 17)
65+
if z > 8:
66+
y += 1
67+
return '{:02x}'.format(17 * y)
68+
69+
a, b, c = color[1:3], color[3:5], color[5:7]
70+
return f'#{f(a)}{f(b)}{f(c)}'
6271
```
6372

6473
### **Java**
6574

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

6877
```java
78+
class Solution {
79+
public String similarRGB(String color) {
80+
String a = color.substring(1, 3), b = color.substring(3, 5), c = color.substring(5, 7);
81+
return "#" + f(a) + f(b) + f(c);
82+
}
83+
84+
private String f(String x) {
85+
int q = Integer.parseInt(x, 16);
86+
q = q / 17 + (q % 17 > 8 ? 1 : 0);
87+
return String.format("%02x", 17 * q);
88+
}
89+
}
90+
```
6991

92+
### **Go**
93+
94+
```go
95+
func similarRGB(color string) string {
96+
f := func(x string) string {
97+
q, _ := strconv.ParseInt(x, 16, 64)
98+
if q%17 > 8 {
99+
q = q/17 + 1
100+
} else {
101+
q = q / 17
102+
}
103+
return fmt.Sprintf("%02x", 17*q)
104+
105+
}
106+
a, b, c := color[1:3], color[3:5], color[5:7]
107+
return "#" + f(a) + f(b) + f(c)
108+
}
70109
```
71110

72111
### **...**

solution/0800-0899/0800.Similar RGB Color/README_EN.md

+40-1
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,52 @@ This is the highest among any shorthand color.
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def similarRGB(self, color: str) -> str:
55+
def f(x):
56+
y, z = divmod(int(x, 16), 17)
57+
if z > 8:
58+
y += 1
59+
return '{:02x}'.format(17 * y)
60+
61+
a, b, c = color[1:3], color[3:5], color[5:7]
62+
return f'#{f(a)}{f(b)}{f(c)}'
5463
```
5564

5665
### **Java**
5766

5867
```java
68+
class Solution {
69+
public String similarRGB(String color) {
70+
String a = color.substring(1, 3), b = color.substring(3, 5), c = color.substring(5, 7);
71+
return "#" + f(a) + f(b) + f(c);
72+
}
73+
74+
private String f(String x) {
75+
int q = Integer.parseInt(x, 16);
76+
q = q / 17 + (q % 17 > 8 ? 1 : 0);
77+
return String.format("%02x", 17 * q);
78+
}
79+
}
80+
```
5981

82+
### **Go**
83+
84+
```go
85+
func similarRGB(color string) string {
86+
f := func(x string) string {
87+
q, _ := strconv.ParseInt(x, 16, 64)
88+
if q%17 > 8 {
89+
q = q/17 + 1
90+
} else {
91+
q = q / 17
92+
}
93+
return fmt.Sprintf("%02x", 17*q)
94+
95+
}
96+
a, b, c := color[1:3], color[3:5], color[5:7]
97+
return "#" + f(a) + f(b) + f(c)
98+
}
6099
```
61100

62101
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func similarRGB(color string) string {
2+
f := func(x string) string {
3+
q, _ := strconv.ParseInt(x, 16, 64)
4+
if q%17 > 8 {
5+
q = q/17 + 1
6+
} else {
7+
q = q / 17
8+
}
9+
return fmt.Sprintf("%02x", 17*q)
10+
11+
}
12+
a, b, c := color[1:3], color[3:5], color[5:7]
13+
return "#" + f(a) + f(b) + f(c)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public String similarRGB(String color) {
3+
String a = color.substring(1, 3), b = color.substring(3, 5), c = color.substring(5, 7);
4+
return "#" + f(a) + f(b) + f(c);
5+
}
6+
7+
private String f(String x) {
8+
int q = Integer.parseInt(x, 16);
9+
q = q / 17 + (q % 17 > 8 ? 1 : 0);
10+
return String.format("%02x", 17 * q);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def similarRGB(self, color: str) -> str:
3+
def f(x):
4+
y, z = divmod(int(x, 16), 17)
5+
if z > 8:
6+
y += 1
7+
return '{:02x}'.format(17 * y)
8+
9+
a, b, c = color[1:3], color[3:5], color[5:7]
10+
return f'#{f(a)}{f(b)}{f(c)}'

0 commit comments

Comments
 (0)