leetcode

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

commit bd1b6300fe62f8abbe7c13e5baf6568f0201a546
parent 50c7d1e8e5143091fcf96f1a34d3510db621355c
Author: AndrewLockVI <andrewlaack1@gmail.com>
Date:   Sun,  9 Apr 2023 22:58:57 -0500

Completed integer to roman using hastable

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

diff --git a/integer-to-roman/a.out b/integer-to-roman/a.out Binary files differ. diff --git a/integer-to-roman/integer-to-roman.cpp b/integer-to-roman/integer-to-roman.cpp @@ -0,0 +1,54 @@ +#include <iostream> +#include <map> +using namespace std; + +//Leet Ratings +// Speed Memory +//Total 32ms 12.9MB +//Beats 5.2% 7.2% +// + + +string intToRoman(int num) { + //Create hashmap + map <int ,string> symbols; + symbols.insert({1, "I"}); + symbols.insert({4, "IV"}); + symbols.insert({5, "V"}); + symbols.insert({9, "IX"}); + symbols.insert({10, "X"}); + symbols.insert({40, "XL"}); + symbols.insert({50, "L"}); + symbols.insert({90, "XC"}); + symbols.insert({100, "C"}); + symbols.insert({400, "CD"}); + symbols.insert({500, "D"}); + symbols.insert({900, "CM"}); + symbols.insert({1000, "M"}); + + string numeral = ""; + while(num > 0){ + for(auto it = symbols.rbegin() ; it != symbols.rend() ; ++it ){ + if(num >= it->first){ + num -= it->first; + numeral += it->second; + //This is inefficient because it needs to reloop each time it finds a val to make sure it does not iterate past a repeated value. + break; + } + } + } + return numeral; +} + + + + +int main(){ + + + + + cout << intToRoman(134) << endl; + return 0; + +}