Roman Numerals Helper(4Kyu) - 图1

题目

Create a RomanNumerals class that can convert a roman numeral to and from an integer value. It should follow the API demonstrated in the examples below. Multiple roman numeral values will be tested for each helper method. Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI. 创建一个RomanNumerics类,该类可以将罗马数字与整数值进行转换。它应该遵循下面示例中演示的API。将为每个助手测试多个罗马数字值方法。现代罗马数字的书写方式是,从最左边的数字开始,分别表示每个数字,并跳过任何值为零的数字。在罗马数字中,1990表示为:1000=M,900=CM,90=XC;结果是MCMXC。2008写为2000=MM,8=VIII;或MMVIII。1666年使用的每个罗马符号降序:MDCLXVI

例子

RomanNumerals.toRoman(1000) // should return ‘M’ RomanNumerals.fromRoman(“M”) // should return 1000

帮助

Symbol Value

| I | 1 |

| V | 5 |

| X | 10 |

| L | 50 |

| C | 100 |

| D | 500 |

| M | 1000 |

原题链接

分析

十进制数字转罗马数字需要特殊判断4,9这两种情况,剩下的就比较简单了。
罗马数字转十进制数字可以对字符串进行切分,然后加起来就可以了。

我的解法

  1. import java.util.*;
  2. public class RomanNumerals {
  3. public static String toRoman(int n) {
  4. int[] arrNum = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
  5. String[] arrRoman = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
  6. String res = "";
  7. int index = arrNum.length-1;
  8. while (n>0){
  9. if (n>=arrNum[index]){
  10. n -= arrNum[index];
  11. res += arrRoman[index];
  12. }else {
  13. index--;
  14. }
  15. }
  16. return res;
  17. }
  18. public static int fromRoman(String romanNumeral) {
  19. HashMap<String, Integer> hashMap = new HashMap<>();
  20. hashMap.put("I",1);
  21. hashMap.put("IV",4);
  22. hashMap.put("V",5);
  23. hashMap.put("IX",9);
  24. hashMap.put("X",10);
  25. hashMap.put("XL",40);
  26. hashMap.put("L",50);
  27. hashMap.put("XC",90);
  28. hashMap.put("C",100);
  29. hashMap.put("CD",400);
  30. hashMap.put("D",500);
  31. hashMap.put("CM",900);
  32. hashMap.put("M",1000);
  33. int sum=0;
  34. for (int i = 0; i < romanNumeral.length(); i++) {
  35. if (i<romanNumeral.length()-1 && hashMap.containsKey(romanNumeral.charAt(i)+""+romanNumeral.charAt(i+1))){
  36. sum += hashMap.get(romanNumeral.charAt(i)+""+romanNumeral.charAt(++i));
  37. }else {
  38. sum += hashMap.get(romanNumeral.charAt(i)+"");
  39. }
  40. }
  41. return sum;
  42. }
  43. }

参考解法

  1. import java.util.Collections;
  2. import java.util.Map;
  3. import java.util.TreeMap;
  4. public class RomanNumerals {
  5. private static final Map<Integer, String> CONVERSIONS;
  6. static {
  7. TreeMap<Integer, String> map = new TreeMap<>();
  8. map.put(1000, "M");
  9. map.put(900, "CM");
  10. map.put(500, "D");
  11. map.put(400, "CD");
  12. map.put(100, "C");
  13. map.put(90, "XC");
  14. map.put(50, "L");
  15. map.put(40, "XL");
  16. map.put(10, "X");
  17. map.put(9, "IX");
  18. map.put(5, "V");
  19. map.put(4, "IV");
  20. map.put(1, "I");
  21. CONVERSIONS = Collections.unmodifiableMap(map.descendingMap());
  22. }
  23. public static String toRoman(int n) {
  24. StringBuilder result = new StringBuilder();
  25. for (Map.Entry<Integer, String> entry : CONVERSIONS.entrySet()) {
  26. while (n >= entry.getKey()) {
  27. result.append(entry.getValue());
  28. n -= entry.getKey();
  29. }
  30. }
  31. return result.toString();
  32. }
  33. public static int fromRoman(String romanNumeral) {
  34. int result = 0;
  35. for (Map.Entry<Integer, String> entry : CONVERSIONS.entrySet()) {
  36. while (romanNumeral.startsWith(entry.getValue())) {
  37. result += entry.getKey();
  38. romanNumeral = romanNumeral.substring(entry.getValue().length());
  39. }
  40. }
  41. return result;
  42. }
  43. }
  1. public class RomanNumerals {
  2. private static final String[] ROMAN_NUMBERS = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  3. private static final int[] ARABIC_NUMBERS = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  4. public static String toRoman(int n) {
  5. int remainingValue = n;
  6. StringBuilder result = new StringBuilder();
  7. for (int i = 0; i < ARABIC_NUMBERS.length; i++) {
  8. while (remainingValue >= ARABIC_NUMBERS[i]) {
  9. remainingValue -= ARABIC_NUMBERS[i];
  10. result.append(ROMAN_NUMBERS[i]);
  11. }
  12. }
  13. return result.toString();
  14. }
  15. public static int fromRoman(String romanNumeral) {
  16. String remainingValue = romanNumeral;
  17. int result = 0;
  18. for(int i = 0; i<ROMAN_NUMBERS.length; i++) {
  19. while(remainingValue.startsWith(ROMAN_NUMBERS[i])) {
  20. remainingValue = remainingValue.substring(ROMAN_NUMBERS[i].length(), remainingValue.length());
  21. result += ARABIC_NUMBERS[i];
  22. }
  23. }
  24. return result;
  25. }
  26. }

提升

  1. 基本上注意到4 ,9 这两个特殊处理的情况,解决这个问题就比较简单了