LeetCode #12 – Integer to Roman

The next problem that I will attempt to solve is:

The approach I took for this problem is pretty much the same as the “Roman to Integer” problem, as they are very similar. Hashmaps are pretty much tailor-made for problems such as these, so once again, I will utilize one. Here is how it looks:

My idea for this solution is to continually subtract the largest possible value available as a roman numeral away from the integer you are trying to convert, and then add the corresponding roman numeral to the output. Once you put all that together, you get this:

While it was a very “brute force” way of approaching this problem, it clearly works:

Here are the logistics of my solution:

Anyways, that is all from me for now. I hope you guys at least learned something today, and that you all have a great day!

By:

Posted in:


Leave a comment