Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Roman to Integer - Leetcode 13/Roman to Integer - Leetcode 13.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
#include <string>
#include <unordered_map>
#include <stdexcept>
#include <iostream>

using namespace std;

class Solution {
public:
int romanToInt(string s) {
Expand All @@ -7,6 +14,9 @@ class Solution {
int i = 0;

while (i < n) {
if (i > 1 && d[s[i - 2]] < d[s[i - 1]] && d[s[i]] <= d[s[i - 1]]) {
throw invalid_argument("Invalid Roman numeric sequence " + s);
}
if (i < n - 1 && d[s[i]] < d[s[i + 1]]) {
summ += d[s[i + 1]] - d[s[i]];
i += 2;
Expand All @@ -19,3 +29,10 @@ class Solution {
return summ;
}
};

int main() {
Solution sol;
// cout << sol.romanToInt("IIXX") << endl; // Throws invalid_argument
// cout << sol.romanToInt("MMD") << endl; // Output: 2500
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public int romanToInt(String s) {
int i = 0;

while (i < n) {
if (i > 1 && d.get(s.charAt(i - 2)) < d.get(s.charAt(i - 1)) && d.get(s.charAt(i)) <= d.get(s.charAt(i - 1))) {
throw new IllegalArgumentException("Invalid Roman numeric sequence " + s);
}
if (i < n - 1 && d.get(s.charAt(i)) < d.get(s.charAt(i + 1))) {
summ += d.get(s.charAt(i + 1)) - d.get(s.charAt(i));
i += 2;
Expand All @@ -28,4 +31,10 @@ public int romanToInt(String s) {

return summ;
}

public static void main(String[] args) {
Solution sol = new Solution();
// System.out.println(sol.romanToInt("IIXX")); // Throws IllegalArgumentException
// System.out.println(sol.romanToInt("MMD")); // Output: 2500
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ function romanToInt(s) {
let i = 0;

while (i < n) {
if (i > 1 && d[s[i - 2]] < d[s[i - 1]] && d[s[i]] <= d[s[i - 1]]) {
throw new Error(`Invalid Roman numeric sequence ${s}`);
}
if (i < n - 1 && d[s[i]] < d[s[i + 1]]) {
summ += d[s[i + 1]] - d[s[i]];
i += 2;
Expand All @@ -16,3 +19,7 @@ function romanToInt(s) {

return summ;
}

// Example Test Cases
// console.log(romanToInt('IIXX')); // Throws Error
// console.log(romanToInt('MMD')); // Output: 2500
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ def romanToInt(self, s: str) -> int:
i = 0

while i < n:
if i > 1 and d[s[i - 2]] < d[s[i - 1]] and d[s[i]] <= d[s[i - 1]]:
raise ValueError(f"Invalid Roman numeric sequence {s}")

if i < n - 1 and d[s[i]] < d[s[i+1]]:
summ += d[s[i+1]] - d[s[i]]
i += 2
Expand All @@ -16,3 +19,8 @@ def romanToInt(self, s: str) -> int:
return summ
# Time: O(n)
# Space: O(1)

# Example Test Cases
# sol = Solution()
# print(sol.romanToInt('IIXX')) # Raises ValueError
# print(sol.romanToInt('MMD')) # Output: 2500