Skip to content

Commit 8e2abb2

Browse files
committed
new exercise just for fun
1 parent 0dd18f4 commit 8e2abb2

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "string"
2+
#include <unordered_map>
3+
#include <unordered_set>
4+
5+
using std::string;
6+
using std::unordered_map;
7+
using std::unordered_set;
8+
9+
class Solution {
10+
public:
11+
int romanToInt(string s) {
12+
unordered_map<char, unordered_set<char>> dict(
13+
{{'I', {'V', 'X'}}, {'X', {'L', 'C'}}, {'C', {'D', 'M'}}});
14+
unordered_map<char, int> values({{'I', 1},
15+
{'V', 5},
16+
{'X', 10},
17+
{'L', 50},
18+
{'C', 100},
19+
{'D', 500},
20+
{'M', 1000}});
21+
22+
int result = 0;
23+
24+
for (int index = 0; index < s.size(); index++) {
25+
auto &current_char = s[index];
26+
if (dict.find(current_char) != dict.end()) {
27+
auto &next_char = s[index + 1];
28+
if (dict[current_char].find(next_char) != dict[current_char].end()) {
29+
result += values[next_char] - values[current_char];
30+
index++;
31+
continue;
32+
}
33+
}
34+
35+
result += values[current_char];
36+
}
37+
38+
return result;
39+
}
40+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
impl Solution {
2+
pub fn roman_to_int(s: String) -> i32 {}
3+
}

rust/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

0 commit comments

Comments
 (0)