LeetCode013:Roman to Integer

来源:互联网 发布:大数据与企业战略 编辑:程序博客网 时间:2024/06/05 22:05
原题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
将罗马数字转换成阿拉伯数字

算法分析:

算法1:
从前往后扫描,用一个临时变量记录分段数字。如果当前比前一个大,说明这一段的值应该是当前这个值减去上一个值。比如IV = 5 – 1;否则,将当前值加入到结果中,然后开始下一段记录。比如VI = 5 + 1, II=1+1

算法2:
采用HashMap来存储各种罗马数字,而不是利用 switch case来判断,这样能够节约时间复杂度。

LeetCode提交源码:

算法1:

  1. public int romanToInt(String s){
  2. int result = 0;
  3. for(int i = 0; i < s.length(); i++){
  4. if(i > 0 && map(s.charAt(i)) > map(s.charAt(i-1))){
  5. //因为在下一个else可能会多加一个map(s.charAt(i - 1)),所以此处要减2倍的
  6. //比如IV,第一次i=0,result=s(0)=I=1,第二次i = 1,result = s(1)-2*s(0)=5-2*1=4;
  7. result += (map(s.charAt(i)) - 2*map(s.charAt(i - 1)));
  8. }
  9. else{
  10. result += map(s.charAt(i));
  11. }
  12. }
  13. return result;
  14. }
  15. private static int map(char c){
  16. switch(c){
  17. case 'I' : return 1;
  18. case 'V' : return 5;
  19. case 'X' : return 10;
  20. case 'L' : return 50;
  21. case 'C' : return 100;
  22. case 'D' : return 500;
  23. case 'M' : return 1000;
  24. default : return 0;
  25. }
  26. }
 


算法2:
  1. public int romanToInt(String s){
  2. if(s == null || s.length() == 0){
  3. return 0;
  4. }
  5. Map<Character,Integer> m = new HashMap<Character,Integer>();
  6. m.put('I',1);
  7. m.put('V',5);
  8. m.put('X',10);
  9. m.put('L',50);
  10. m.put('C',100);
  11. m.put('D',500);
  12. m.put('M',1000);
  13. int length = s.length();
  14. int result = m.get(s.charAt(length - 1));
  15. for(int i = length - 2; i >= 0;i--){
  16. if(m.get(s.charAt(i + 1)) <= m.get(s.charAt(i))){
  17. result += m.get(s.charAt(i));
  18. }
  19. else{
  20. result -= m.get(s.charAt(i));
  21. }
  22. }
  23. return result;
  24. }

 

完整运行程序:
  1. /**************************************************************
  2. * Copyright (c) 2016
  3. * All rights reserved.
  4. * 版 本 号:v1.0
  5. * 题目描述: Given a roman numeral, convert it to an integer.
  6. *Input is guaranteed to be within the range from 1 to 3999.
  7. *将罗马数字转化成阿拉伯数字
  8. * 输入描述:请输入一个罗马数字:
  9. *IV
  10. * 程序输出:输入的罗马数字转化为阿拉伯数字的结果为:
  11. *4
  12. *算法2的结果为:
  13. *3586
  14. * 问题分析:无
  15. * 算法描述:算法1:从前往后扫描,用一个临时变量记录分段数字。如果当前比前一个大,
  16. * 说明这一段的值应该是当前这个值减去上一个值。比如IV = 5 – 1;否则,
  17. * 将当前值加入到结果中,然后开始下一段记录。比如VI = 5 + 1, II=1+1
  18. * 算法2:利用Hashmap存储,而不是用switch case 来判断,但判断数字的原理相同
  19. * 完成时间:2016-11-27
  20. ***************************************************************/
  21. package org.GuoGuoFighting.LeetCode013;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. import java.util.Scanner;
  25. /*
  26. 算法1:算法正确,但LeetCode运行时超时,无法提交
  27. */
  28. class SolutionMethod1{
  29. public int romanToInt(String s){
  30. int result = 0;
  31. for(int i = 0; i < s.length(); i++){
  32. if(i > 0 && map(s.charAt(i)) > map(s.charAt(i-1))){
  33. //因为在下一个else可能会多加一个map(s.charAt(i - 1)),所以此处要减2倍的
  34. //比如IV,第一次i=0,result=s(0)=I=1,第二次i = 1,result = s(1)-2*s(0)=5-2*1=4;
  35. result += (map(s.charAt(i)) - 2*map(s.charAt(i - 1)));
  36. }
  37. else{
  38. result += map(s.charAt(i));
  39. }
  40. }
  41. return result;
  42. }
  43. private static int map(char c){
  44. switch(c){
  45. case 'I' : return 1;
  46. case 'V' : return 5;
  47. case 'X' : return 10;
  48. case 'L' : return 50;
  49. case 'C' : return 100;
  50. case 'D' : return 500;
  51. case 'M' : return 1000;
  52. default : return 0;
  53. }
  54. }
  55. }
  56. /*
  57. 算法2:运用HashMap来存储*/
  58. class SolutionMethod2{
  59. public int romanToInt(String s){
  60. if(s == null || s.length() == 0){
  61. return 0;
  62. }
  63. Map<Character,Integer> m = new HashMap<Character,Integer>();
  64. m.put('I',1);
  65. m.put('V',5);
  66. m.put('X',10);
  67. m.put('L',50);
  68. m.put('C',100);
  69. m.put('D',500);
  70. m.put('M',1000);
  71. int length = s.length();
  72. int result = m.get(s.charAt(length - 1));
  73. for(int i = length - 2; i >= 0;i--){
  74. if(m.get(s.charAt(i + 1)) <= m.get(s.charAt(i))){
  75. result += m.get(s.charAt(i));
  76. }
  77. else{
  78. result -= m.get(s.charAt(i));
  79. }
  80. }
  81. return result;
  82. }
  83. }
  84. public class RomantoInteger {
  85. public static void main(String[] args){
  86. Scanner scanner = new Scanner(System.in);
  87. System.out.println("请输入一个罗马数字:");
  88. String str = scanner.nextLine();
  89. scanner.close();
  90. System.out.println("输入的罗马数字转化为阿拉伯数字的结果为:");
  91. SolutionMethod1 solution1 = new SolutionMethod1();
  92. System.out.println(solution1.romanToInt(str));
  93. System.out.println("算法2的结果为:");
  94. SolutionMethod2 solution2 = new SolutionMethod2();
  95. System.out.println(solution2.romanToInt(str));
  96. }
  97. }

程序运行结果:
 

0 0
原创粉丝点击