1. 程式人生 > >12-integer-to-roman

12-integer-to-roman

題目描述:

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII

, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX

. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: 3
Output: "III"

Example 2:

Input: 4
Output: "IV"

Example 3:

Input: 9
Output: "IX"

Example 4:

Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

程式碼解答:

package com.jack.algorithm;

import java.util.HashMap;
import java.util.Map;

/**
 * create by jack 2018/10/26
 *
 * @author jack
 * @date: 2018/10/26 22:57
 * @Description:
 * 整數轉換為羅馬數字
 */
public class IntegerToRoman {

    /**
     * 題目描述:
     * https://leetcode.com/problems/integer-to-roman/
     *
     * @param num
     * @return
     */
    public static String intToRoman(int num) {
        if (num <= 0) {
            return "";
        }
        int[] n = new int[4];
        int x = num;
        int i = 0;
        while (x != 0) {
            n[i] = x%10;
            x = x/10;
            i++;
        }
        StringBuilder rs = new StringBuilder();
        Map<Integer,Character> intCharMap = getIntChar();
        int bit = 1000;
        for (i=3;i>=0;i--) {
            String str = getStr(n[i], bit, intCharMap);
            rs.append(str);
            bit = bit/10;
        }
        return rs.toString();
    }

    public static  String getStr(int i,int bit,Map<Integer,Character> intCharMap) {
        if (i == 0) {
            return "";
        }
        String rs = "";
        int j=0;
        if (i == 4) {
            rs = rs + intCharMap.get(bit) + intCharMap.get(5 * bit);
        } else if (i == 5) {
            rs = rs + intCharMap.get(bit * 5);
        }
        if (i != 4 && i < 5) {
            j=1;
            for (j=1;j<=i && i!=0;j++) {
                rs = rs +intCharMap.get(bit);
            }
        }
        if (i > 5 && i != 9) {
            rs += intCharMap.get(5 * bit);
            j=1;
            for (j=1;j<=i-5 && i!=0;j++) {
                rs += intCharMap.get(bit);
            }
        } else if (i == 9) {
            rs += intCharMap.get(bit);
            rs += intCharMap.get(bit*10);
        }

        return rs;
    }


    public static   Map<Integer,Character> getIntChar(){
        Map<Integer, Character> intCharMap = new HashMap<Integer, Character>(16);
        intCharMap.put(1, 'I');
        intCharMap.put(5, 'V');
        intCharMap.put(10, 'X');
        intCharMap.put(50, 'L');
        intCharMap.put(100, 'C');
        intCharMap.put(500, 'D');
        intCharMap.put(1000, 'M');
        return intCharMap;
    }

    public static void main(String[] args) {
        String roman = intToRoman(58);
        System.out.println("roman ="+roman);
    }
}

原始碼:

原始碼