Skip to content

Commit e81e8f8

Browse files
committed
First commit
0 parents  commit e81e8f8

File tree

7 files changed

+221
-0
lines changed

7 files changed

+221
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

+108
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "leetcode_rust"
3+
version = "0.1.0"
4+
edition = "2021"
5+
authors = ["Ryo Light <[email protected]>"]
6+
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
phf = { version = "0.11.2", features = ["macros"] }

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Leetcode Solutions in Rust

src/easy/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub mod roman_to_int;
2+
3+
pub fn run() {
4+
display_roman_to_int("XIV");
5+
display_roman_to_int("XLII");
6+
}
7+
8+
fn display_roman_to_int(s: &str) {
9+
println!("{} -> {}", s, roman_to_int::solve(String::from(s)));
10+
}

src/easy/roman_to_int.rs

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#![allow(dead_code)]
2+
3+
use phf::phf_map;
4+
5+
/*
6+
* Problem: 13. Roman to Integer (Easy)
7+
* URL: https://leetcode.com/problems/roman-to-integer/
8+
*
9+
* Runtime: ? ms (%)
10+
* Memory Usage: ? MB (%)
11+
*/
12+
13+
static ROMAN_NUMERALS_STR: phf::Map<&'static str, i32> = phf_map! {
14+
"I" => 1,
15+
"V" => 5,
16+
"X" => 10,
17+
"L" => 50,
18+
"C" => 100,
19+
"D" => 500,
20+
"M" => 1000
21+
};
22+
23+
static ROMAN_NUMERALS: phf::Map<char, i32> = phf_map! {
24+
'I' => 1,
25+
'V' => 5,
26+
'X' => 10,
27+
'L' => 50,
28+
'C' => 100,
29+
'D' => 500,
30+
'M' => 1000
31+
};
32+
33+
pub fn solve(s: String) -> i32 {
34+
let mut result: i32 = 0;
35+
36+
let mut buf = [0; 1];
37+
38+
let mut subtract_previous = false;
39+
40+
for (i, char) in s.chars().enumerate() {
41+
let val = ROMAN_NUMERALS_STR.get(char.encode_utf8(&mut buf)).unwrap();
42+
43+
if subtract_previous {
44+
result += val - ROMAN_NUMERALS_STR.get(s.get(i-1..i).unwrap()).unwrap();
45+
subtract_previous = false;
46+
continue;
47+
}
48+
49+
if i+1 < s.len() && val < ROMAN_NUMERALS_STR.get(s.get(i+1..i+2).unwrap()).unwrap() {
50+
subtract_previous = true;
51+
} else {
52+
result += val;
53+
}
54+
55+
}
56+
57+
return result;
58+
}
59+
60+
const NUMERALS: [(char, u32); 7] = [
61+
('I', 1),
62+
('V', 5),
63+
('X', 10),
64+
('L', 50),
65+
('C', 100),
66+
('D', 500),
67+
('M', 1000),
68+
];
69+
70+
/*
71+
Symbol Value
72+
I 1
73+
V 5
74+
X 10
75+
L 50
76+
C 100
77+
D 500
78+
M 1000
79+
80+
I can be placed before V (5) and X (10) to make 4 and 9.
81+
X can be placed before L (50) and C (100) to make 40 and 90.
82+
C can be placed before D (500) and M (1000) to make 400 and 900.
83+
*/

src/main.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// #![allow(dead_code)]
2+
3+
pub mod easy;
4+
5+
fn main() {
6+
easy::run();
7+
}

0 commit comments

Comments
 (0)