1. int toNumber(char ch) {
  2. switch (ch) {
  3. case 'I': return 1;
  4. case 'V': return 5;
  5. case 'X': return 10;
  6. case 'L': return 50;
  7. case 'C': return 100;
  8. case 'D': return 500;
  9. case 'M': return 1000;
  10. }
  11. return 0;
  12. }

利用上述对应规则,实现罗马数字到整数的转换
 
罗马数字按照从后向前进行一次遍历,当后面的单位小于前面的单位时,直接加上该字符对应的数值;当后面的单位大于前面的单位时,需要减去该字符对应的数值。
 
参考代码:
  1. package leetcode_50;
  2.  
  3. /***
  4. *
  5. * @author pengfei_zheng
  6. * 罗马数字转为整数
  7. */
  8. public class Solution13 {
  9. public int romanToInt(String s) {
  10. int len = s.length();
  11. int ans = toNumber(s.charAt(len-1));
  12. for(int i=len-2;i>=0;i--){
  13. int last = toNumber(s.charAt(i+1));
  14. int first = toNumber(s.charAt(i));
  15. if(first>=last){//后面字符单位小于前面字符单位,加上对应数字
  16. ans+=first;
  17. }
  18. else//减去对应数字
  19. ans-=first;
  20. }
  21. return ans;
  22. }
  23. int toNumber(char ch) {//对应转换规则
  24. switch (ch) {
  25. case 'I': return 1;
  26. case 'V': return 5;
  27. case 'X': return 10;
  28. case 'L': return 50;
  29. case 'C': return 100;
  30. case 'D': return 500;
  31. case 'M': return 1000;
  32. }
  33. return 0;
  34. }
  35. }
 
 
 

LeetCode 13 Roman to Integer(罗马数字转为整数)的更多相关文章

  1. [leetcode]13. Roman to Integer罗马数字转整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  2. [LeetCode] 13. Roman to Integer 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  3. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  4. 【LeetCode】Roman to Integer(罗马数字转整数)

    这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...

  5. Leetcode#13. Roman to Integer(罗马数字转整数)

    题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...

  6. leetcode 13 Roman to Integer 罗马数组转整型

    描述: 将一个字符串表示的罗马数字转为整数,范围0~3999 解决: 如果后一个比前一个大,则表示减,没什么技巧. map<}, {}, {}, {}, {}, {}, {}}; int rom ...

  7. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  8. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  9. LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告

    1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

随机推荐

  1. Java项目性能持续优化中……

     尽量使用StringBuilder和StringBuffer进行字符串连接, 参考链接: Java编程中“为了性能”尽量要做到的一些地方

  2. Dominoserver 安装

    domino安装及语言包安装 http://wenku.baidu.com/view/f473600d581b6bd97f19ea9b.html dominoserver 安装后启动配置: http: ...

  3. 限制 Text Field 输入的内容类型:只允许输入数字

    效果如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController< ...

  4. level 6 - unit4 - 强调句

    强调句 强调实义动词 范围: 一般现在时/一般过去式:肯定句 方法:v.前面加do/does/did 例子: i love you --> i do love you i loved you - ...

  5. AdoConnect-获取连接字符串 (工具)

    使用ADO访问数据库时需要设置正确的连接字符串,为此特提供一个获取连接字符串的小工具,方便编程使用. 使用方法: 1.点击“连接字符串”,弹出数据链接属性对话框 2.可以使用“提供程序”新建数据源,也 ...

  6. js将json数据以csv格式下载

    摘要: 最近有一个非项目的小需求,就是将项目开发分工文件化,方便后期管理维护.但是开发时,分工安排都是以json格式记录的,所以就做了一个将json数据以csv格式下载到本地. 代码: <!DO ...

  7. Uva--11324--The Largest Clique【有向图强连通分量】

    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&am ...

  8. 开源一个简易轻量的reactor网络框架

    github https://github.com/sea-boat/net-reactor net-reactor it's a simple and easy net framework with ...

  9. InsertSql

    declare @hobby table(hobbyID int,hName nvarchar(100));insert into @hobby(hobbyID,hName)Select 1,'爬山' ...

  10. 有人在贴吧问phpmyadmin如何设置插入的时候默认插入1条记录

    在新版phpmyadmin中(我的版本是3.5.1) 插入的时候会提示插入两条,能够方便操作,让你多录入几条数据,如图 然而有人不想要这个界面默认插入两条,如何改为1条或者其他呢? 我审查了这个元素标 ...