File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change 1
1
# ---
2
2
# title: 13. Roman to Integer
3
3
# id: problem13
4
- # author: Tian Jun
5
- # date: 2020-10-31
4
+ # author: Jerry Ling
5
+ # date: 2021-12-13
6
6
# difficulty: Easy
7
7
# categories: Math, String
8
8
# link: <https://leetcode.com/problems/roman-to-integer/description/>
95
95
# # @lc code=start
96
96
using LeetCode
97
97
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
+
98
118
# # add your code here:
99
119
# # @lc code=end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments