leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 7c2b5c79e49bb50b9728c960cd6575c93eb9f71d
parent 5949e4fdf0713a88920373e856f0729ffc2e7ea1
Author: AndrewLockVI <andrew.laack@gmail.com>
Date:   Wed,  5 Apr 2023 19:23:40 -0500

Completed Integer to Roman

Diffstat:
Aroman-to-integer/a.out | 0
Aroman-to-integer/roman-to-integer.cpp | 48++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/roman-to-integer/a.out b/roman-to-integer/a.out Binary files differ. diff --git a/roman-to-integer/roman-to-integer.cpp b/roman-to-integer/roman-to-integer.cpp @@ -0,0 +1,48 @@ +#include <string> +#include <iostream> +#include <vector> +#include <map> + + +//Leet Ratings +// Speed Memory +//Total 41ms 8.2MB +//Beats 6.1% 40.21% +// + + + +using namespace std; + + +int romanToInt(string s) { + + map <char, int> vals = {}; + vals.insert({'I', 1}); + vals.insert({'V', 5}); + vals.insert({'X', 10}); + vals.insert({'L', 50}); + vals.insert({'C', 100}); + vals.insert({'D', 500}); + vals.insert({'M', 1000}); + + int return_val; + int lv = 1000; + int cv; + for(int i = 0 ; i < s.length() ; ++i ){ + cv = vals.find(s[i])->second; + if(lv < cv){ + return_val -= lv * 2; + } + return_val += cv; + lv = cv; + } + return (return_val); +} + + +int main(){ + cout << romanToInt("MCMXCIV") << endl; + return 0; +} +