Skip to content

Commit 568f2da

Browse files
committed
Completed '13. Roman to Int'
1 parent 2a1afab commit 568f2da

10 files changed

+102
-39
lines changed

Readme.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- Solutions Visualized – [@CoffeelessProgrammer | Instagram](https://www.instagram.com/coffeelessprogrammer/)
55

66
## Environment
7-
- <span>Rust v1.72.1</span>
7+
- <span>Rust v1.73.0</span>
88

99
## Problems Completed
1010

@@ -16,6 +16,7 @@
1616
- <span>190. Reverse Bits</span>
1717
- <span>191. Number of 1 Bits</span>
1818
- **Strings**
19+
- <span>13. Roman to Integer</span>
1920
- <span>67. Add Binary</span>
2021

2122
## Resources

src/bit_manipulation/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
pub mod count_set_bits;
2-
pub mod reverse_bits;
1+
pub mod p0190_reverse_bits;
2+
pub mod p0191_count_set_bits;
33

44
pub fn run() {
55
}

src/bit_manipulation/reverse_bits.rs src/bit_manipulation/p0190_reverse_bits.rs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub fn solve(mut x: u32) -> u32 {
1313
for i in 0..32 {
1414
// println!("\n({i}) x={:#32b}\nresult={:#32b}", x, result);
1515
result <<= 1;
16-
1716
if (x & mask == 1) { result += 1 }
1817
x >>= 1;
1918
}

src/math/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod sqrt;
1+
pub mod p0069_sqrt;
22

33
pub fn run() {
44
}
File renamed without changes.

src/pXXX_template.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Problem: XXX. PROBLEM_NAME (DIFFICULTY)
3+
* URL: LINK_HERE
4+
*
5+
* Runtime: 50 ms (100%)
6+
* Memory Usage: 10 MB (100%)
7+
*/
8+
9+
pub fn solve() -> u32 {}
10+
11+
#[cfg(test)]
12+
mod template_tests {
13+
use super::*;
14+
15+
#[test]
16+
fn happy_path() {
17+
assert_eq!(solve(), 0b00111001_01111000_00101001_01000000);
18+
}
19+
}

src/string/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
pub mod roman_to_int;
2-
pub mod add_binary;
1+
pub mod p0013_roman_to_int;
2+
pub mod p0067_add_binary;
33

44
pub fn run() {
55
display_roman_to_int("XIV");
66
display_roman_to_int("XLII");
77
}
88

99
fn display_roman_to_int(s: &str) {
10-
println!("{} -> {}", s, roman_to_int::solve(String::from(s)));
10+
println!("{} -> {}", s, p0013_roman_to_int::solve(s));
1111
}
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,60 @@
11
// #![allow(dead_code)]
22

3+
use std::collections::HashMap;
34
use phf::phf_map;
45

56
/*
67
* Problem: 13. Roman to Integer (Easy)
78
* URL: https://leetcode.com/problems/roman-to-integer/
89
*
9-
* Runtime: ? ms (%)
10-
* Memory Usage: ? MB (%)
10+
* Runtime: 0 ms (100%)
11+
* Memory Usage: 2.32 MB (10.58%)
1112
*/
1213

13-
static ROMAN_NUMERALS_STR: phf::Map<&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-
};
14+
const NUMERALS: [(char, u32); 7] = [
15+
('I', 1),
16+
('V', 5),
17+
('X', 10),
18+
('L', 50),
19+
('C', 100),
20+
('D', 500),
21+
('M', 1000),
22+
];
2223

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-
};
24+
pub fn solve(s: &str) -> i32 {
25+
let map: HashMap<char, i32> = HashMap::from([
26+
('I', 1),
27+
('V', 5),
28+
('X', 10),
29+
('L', 50),
30+
('C', 100),
31+
('D', 500),
32+
('M', 1000)
33+
]);
34+
let mut result = 0;
35+
let mut subtract_previous = false;
3236

33-
pub fn solve(s: String) -> i32 {
37+
for (i, char) in s.chars().enumerate() {
38+
let val = map.get(&char).unwrap();
39+
40+
if subtract_previous {
41+
result += val - map.get(&(s.get(i-1..i).unwrap().as_bytes()[0] as char)).unwrap();
42+
subtract_previous = false;
43+
continue;
44+
}
45+
46+
if i+1 < s.len() && val < map.get(&(s.get(i+1..i+2).unwrap().as_bytes()[0] as char)).unwrap() {
47+
subtract_previous = true;
48+
} else {
49+
result += val;
50+
}
51+
52+
}
53+
54+
result
55+
}
56+
57+
pub fn solve_phf(s: String) -> i32 {
3458
let mut result: i32 = 0;
3559

3660
let mut buf = [0; 1];
@@ -57,15 +81,15 @@ pub fn solve(s: String) -> i32 {
5781
return result;
5882
}
5983

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-
];
84+
static ROMAN_NUMERALS_STR: phf::Map<&str, i32> = phf_map! {
85+
"I" => 1,
86+
"V" => 5,
87+
"X" => 10,
88+
"L" => 50,
89+
"C" => 100,
90+
"D" => 500,
91+
"M" => 1000
92+
};
6993

7094
/*
7195
Symbol Value
@@ -80,4 +104,24 @@ const NUMERALS: [(char, u32); 7] = [
80104
I can be placed before V (5) and X (10) to make 4 and 9.
81105
X can be placed before L (50) and C (100) to make 40 and 90.
82106
C can be placed before D (500) and M (1000) to make 400 and 900.
83-
*/
107+
*/
108+
109+
#[cfg(test)]
110+
mod roman_to_int_tests {
111+
use super::*;
112+
113+
#[test]
114+
fn happy_path() {
115+
assert_eq!(solve("LVIII"), 58);
116+
}
117+
118+
#[test]
119+
fn subtraction_before_additions() {
120+
assert_eq!(solve("XLII"), 42);
121+
}
122+
123+
#[test]
124+
fn multiple_subtractions() {
125+
assert_eq!(solve("MCMXCIV"), 1994);
126+
}
127+
}
File renamed without changes.

0 commit comments

Comments
 (0)