[Leetcode] 012. Integer to Roman (Medium) (C++/java/python)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.