Skip to content

Commit 72aa10f

Browse files
authored
1 parent ebb2813 commit 72aa10f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/unresolved/13.roman-to-integer.jl renamed to src/problems/13.roman-to-integer.jl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# ---
22
# title: 13. Roman to Integer
33
# id: problem13
4-
# author: Tian Jun
5-
# date: 2020-10-31
4+
# author: Jerry Ling
5+
# date: 2021-12-13
66
# difficulty: Easy
77
# categories: Math, String
88
# link: <https://leetcode.com/problems/roman-to-integer/description/>
@@ -95,5 +95,25 @@
9595
## @lc code=start
9696
using LeetCode
9797

98+
function roman_to_integer(s::AbstractString)
99+
table = Dict(
100+
'I' => 1,
101+
'V' => 5,
102+
'X' => 10,
103+
'L' => 50,
104+
'C' => 100,
105+
'D' => 500,
106+
'M' => 1000,
107+
)
108+
109+
nums = [table[c] for c in s]
110+
111+
for idx = 1:lastindex(nums)-1
112+
nums[idx] *= nums[idx] >= nums[idx+1] ? 1 : -1
113+
end
114+
115+
return sum(nums)
116+
end
117+
98118
## add your code here:
99119
## @lc code=end

test/problems/13.roman-to-integer.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@testset "13.roman-to-integer.jl" begin
2+
@test roman_to_integer("III") == 3
3+
@test roman_to_integer("IV") == 4
4+
@test roman_to_integer("IX") == 9
5+
@test roman_to_integer("LVIII") == 58
6+
@test roman_to_integer("MCMXCIV") == 1994
7+
end

0 commit comments

Comments
 (0)