-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Hactoberfest Contribution Problem#13 * Hactoberfest Contribution Problem#13
- Loading branch information
1 parent
11d7ed8
commit 3a573db
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//Name: Raza Mohayyuddin | ||
|
||
//Username: RazaKhan9639 | ||
|
||
//Approach: First create a hashmap to store values of each key, this can be optimized to use string array or any other method. After that create a variable named sum to store the sum of the. After that start iterating over the array & check if the character is less than the next character then add the values to sum till checked all characters. After that return the value of the sum variable. | ||
|
||
var ro = new Map(); | ||
ro.set("I", 1); | ||
ro.set("V", 5); | ||
ro.set("X", 10); | ||
ro.set("L", 50); | ||
ro.set("C", 100); | ||
ro.set("D", 500); | ||
ro.set("M", 1000); | ||
var romanToInt = function (s) { | ||
var sum = 0; | ||
var n = s.length; | ||
|
||
for (i = 0; i < n; i++) { | ||
if (i != n - 1 && ro.get(s.charAt(i)) < ro.get(s.charAt(i + 1))) { | ||
sum += ro.get(s.charAt(i + 1)) - ro.get(s.charAt(i)); | ||
i++; | ||
} else { | ||
sum += ro.get(s.charAt(i)); | ||
} | ||
} | ||
return sum; | ||
}; |