Content-Length: 1299 | pFad | http://github.com/fishercoder1534/Leetcode/pull/164.diff

thub.com diff --git a/cpp/_12.cpp b/cpp/_12.cpp new file mode 100644 index 0000000000..c8fa52707e --- /dev/null +++ b/cpp/_12.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + string intToRoman(int num) { + // Prepare ordered dictionary with array and pair + pair rmap[] = { + {1000,"M"}, + {900,"CM"}, + {500, "D"}, + {400, "CD"}, + {100, "C"}, + {90, "XC"}, + {50, "L"}, + {40, "XL"}, + {10,"X"}, + {9,"IX"}, + {5, "V"}, + {4,"IV"}, + {1, "I"} + }; + + // Initialize pointer to the beginning of the array + int rptr = 0; + string str=""; + // Loop till the number is above 0 + while(num){ + // If numeric value at pointer is >= the number + if(num>=(rmap[rptr]).first){ + // Add numeral to the answer + str += (rmap[rptr]).second; + // subtract numeric value from the number + num -= (rmap[rptr]).first; + } + else{ + // Move pointer to the next biggest numeric value + rptr++; + } + } + return str; + } +};








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/fishercoder1534/Leetcode/pull/164.diff

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy