Index: [Leetcode] leetcode key index (C++/JAVA/PYTHON/SQL)
Github:https://github.com/illuz/leetcode
012.integer_to_roman (Medium)
links:
Title: https://oj.leetcode.com/problems/integer-to-roman/
Code (GitHub): Https://github.com/illuz/leetcode
Test Instructions:
Convert decimal to Roman number.
Analysis:
Simulation can be.
"The basic symbol of the Roman numeral has I (denotes decimal number 1), V (denotes 5), X (denotes ten), L (denoted by a), C (for the millions), D (denotes a), M (denotes 1000). "– Roman numeral (Baidu Encyclopedia)
Code:
C++:
Class Solution {Private:int val[13] = {$, 5, 9, 400,100, +, +, 40,10,,, 4,1};string syb[13] = {"M", "CM", "D" , "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};p ublic:string inttoroman (int num) {string Roman;int I = 0, K;while ( num > 0) {k = Num/val[i];while (k--) {Roman + Syb[i];num-= Val[i];} i++;} return Roman;};
Java:
public class Solution { private int[] val = {$, 5, 9,, 4, 1
}; Private string[] Syb = new string[] { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", " I " }; Public String inttoroman (int num) { StringBuilder Roman = new StringBuilder (); int i = 0, K; while (num > 0) { k = num/val[i]; while (k--> 0) { roman.append (syb[i]); num-= Val[i]; } i++; } return roman.tostring (); }}
Python:
Class solution: # @return A string def inttoroman (self, num): val = [ 100, 90, 50 , Max, 9, 5, 4, 1 ] syb = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", " IV ", " I " ] roman = ' i = 0 while num > 0: For _ in range (num//val[i]): Roma n + = syb[i] num-= val[i] i + = 1 return Roman
[Leetcode] 012. Integer to Roman (Medium) (C++/java/python)