Skip to content

Commit 7f00e2f

Browse files
committed
feat: update go solution to lc problem: No.0012
No.0012.Integer to Roman
1 parent 5c80387 commit 7f00e2f

File tree

3 files changed

+49
-47
lines changed

3 files changed

+49
-47
lines changed

solution/0000-0099/0012.Integer to Roman/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,12 @@ M 1000</pre>
7777

7878
<!-- 这里可写通用的实现逻辑 -->
7979

80+
**方法一:模拟**
81+
8082
贪心算法实现。
8183

84+
时间复杂度为 $O(1)$,空间复杂度为 $O(1)$。
85+
8286
<!-- tabs:start -->
8387

8488
### **Python3**
@@ -153,6 +157,22 @@ public:
153157
};
154158
```
155159
160+
### **Go**
161+
162+
```go
163+
func intToRoman(num int) string {
164+
ans := ""
165+
values := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
166+
romans := []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
167+
for i, value := range values {
168+
for value <= num {
169+
ans, num = ans+romans[i], num-value
170+
}
171+
}
172+
return ans
173+
}
174+
```
175+
156176
### **...**
157177

158178
```

solution/0000-0099/0012.Integer to Roman/README_EN.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ M 1000</pre>
6262

6363
## Solutions
6464

65+
**Approach 1: Simulation**
66+
67+
Time complexity $O(1)$, Space complexity $O(1)$.
68+
6569
<!-- tabs:start -->
6670

6771
### **Python3**
@@ -132,6 +136,22 @@ public:
132136
};
133137
```
134138
139+
### **Go**
140+
141+
```go
142+
func intToRoman(num int) string {
143+
ans := ""
144+
values := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
145+
romans := []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
146+
for i, value := range values {
147+
for value <= num {
148+
ans, num = ans+romans[i], num-value
149+
}
150+
}
151+
return ans
152+
}
153+
```
154+
135155
### **...**
136156

137157
```
Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,11 @@
1-
// 字符 数值
2-
// I 1
3-
// V 5
4-
// X 10
5-
// L 50
6-
// C 100
7-
// D 500
8-
// M 1000
9-
var table = make(map[int]string)
10-
var keys = []int{1,4,5,9,10,40,50,90,100,400,500,900,1000}
11-
12-
func init() {
13-
table[1] = "I"
14-
table[4] = "IV"
15-
table[5] = "V"
16-
table[9] = "IX"
17-
table[10] = "X"
18-
table[40] = "XL"
19-
table[50] = "L"
20-
table[90] = "XC"
21-
table[100] = "C"
22-
table[400] = "CD"
23-
table[500] = "D"
24-
table[900] = "CM"
25-
table[1000] = "M"
26-
}
27-
281
func intToRoman(num int) string {
29-
if n, exist := table[num]; exist {
30-
return n
2+
ans := ""
3+
values := []int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
4+
romans := []string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
5+
for i, value := range values {
6+
for value <= num {
7+
ans, num = ans+romans[i], num-value
8+
}
319
}
32-
var result string
33-
lenKeys := len(keys)
34-
for i:=lenKeys-1; i>=0; i-- {
35-
d := keys[i]
36-
count := num/d
37-
result += genreateRoman(d, count)
38-
num = num - d * count
39-
}
40-
return result
41-
}
42-
43-
func genreateRoman(n, count int) string {
44-
var r string
45-
for i:=0; i<count; i++ {
46-
r += table[n]
47-
}
48-
return r
49-
}
10+
return ans
11+
}

0 commit comments

Comments
 (0)