Roman numerals and integer

  • Roman numerals to Integer

Roman numeral characters comprising the following seven: I, V, X, L, C, D and M.

Numerical character
the I. 1
V. 5
X-10
L 50
C 100
D 500
M 1000
, for example, 2 written as Roman numerals II, namely 1.12 written as two parallel XII, namely X + II. 27 written as XXVII, namely XX + V + II.

Typically, small numbers of Roman numerals to the right in large numbers. But there are exceptions, for example, do not write 4 IIII, but IV. In the left number 5 number 1, the number of large numbers equal to the value represented by a reduced number 5 obtained 4. Likewise, the number 9 is represented as IX. This special rule applies only to the following six cases:

I may be on the left V (5) and X (10), and to represent 4 and 9.
X L can be placed on the left (50) and C (100), and 40 and 90 are represented. 
C may be placed on D (500) and M (1000) to the left, to 400 and 900 represent.
Given a Roman numeral, to convert it to an integer. To ensure that the input is in the range of 1 to 3999.

Example 1:

Input: "III"
Output: 3
Example 2:

Input: "IV"
Output: 4
Example 3:

Input: "IX"
Output: 9
Example 4:

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

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

 1 public class T13 {
 2     public int romanToInt(String s) {
 3         HashMap<String, Integer> map = new LinkedHashMap<>();
 4         map.put("M",1000);
 5         map.put("CM",900);
 6         map.put("D",500);
 7         map.put("CD",400);
 8         map.put("C",100);
 9         map.put("XC",90);
10         map.put("L",50);
11         map.put("XL",40);
12         map.put("X",10);
13         map.put("IX",9);
14         map.put("V",5);
15         map.put("IV",4);
16         map.put("I",1);
17 
18         int res = 0;
19         for (int i = 0; i < s.length() - 1; i++) {
20             String str1 = s.charAt(i) + "";
21             String str2 = s.charAt(i + 1) + "";
22             if (map.get(str1) < map.get(str2)) {
23                 res -= map.get(str1);
24             } else {
25                 res += map.get(str1);
26             }
27         }
28         res += map.get(s.charAt(s.length() - 1) + "");
29 
30         return res;
31     }
32 
33 }
  • Integer to Roman numerals

Roman numeral characters comprising the following seven: I, V, X, L, C, D and M.

Numerical character
the I. 1
V. 5
X-10
L 50
C 100
D 500
M 1000
, for example, 2 written as Roman numerals II, namely 1.12 written as two parallel XII, namely X + II. 27 written as XXVII, namely XX + V + II.

Typically, small numbers of Roman numerals to the right in large numbers. But there are exceptions, for example, do not write 4 IIII, but IV. In the left number 5 number 1, the number of large numbers equal to the value represented by a reduced number 5 obtained 4. Likewise, the number 9 is represented as IX. This special rule applies only to the following six cases:

I may be on the left V (5) and X (10), and to represent 4 and 9.
X L can be placed on the left (50) and C (100), and 40 and 90 are represented. 
C may be placed on D (500) and M (1000) to the left, to 400 and 900 represent.
Given an integer, convert it to a Roman numeral. To ensure that the input is in the range of 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, IV = 4.

 1 public class T12 {
 2 
 3     public String intToRoman(int num) {
 4         int[] nums = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
 5         String[] strs = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
 6         String res = "";
 7         for (int i = 0;i < nums.length;i++) {
 8             int count = num / nums[i];
 9             if (count == 0) {
10                 continue;
11             }
12             for (int j = 0; j < count; j++) {
13                 res += strs[i];
14             }
15             num -= count * nums[i];
16             if (num == 0) {
17                 break;
18             }
19         }
20         return res;
21 
22     }
23 
24 }

 

Guess you like

Origin www.cnblogs.com/zzytxl/p/12501530.html