int toNumber(char ch) {
switch (ch) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
}
return 0;
}

利用上述对应规则,实现罗马数字到整数的转换
 
罗马数字按照从后向前进行一次遍历,当后面的单位小于前面的单位时,直接加上该字符对应的数值;当后面的单位大于前面的单位时,需要减去该字符对应的数值。
 
参考代码:
package leetcode_50;

/***
*
* @author pengfei_zheng
* 罗马数字转为整数
*/
public class Solution13 {
public int romanToInt(String s) {
int len = s.length();
int ans = toNumber(s.charAt(len-1));
for(int i=len-2;i>=0;i--){
int last = toNumber(s.charAt(i+1));
int first = toNumber(s.charAt(i));
if(first>=last){//后面字符单位小于前面字符单位,加上对应数字
ans+=first;
}
else//减去对应数字
ans-=first;
}
return ans;
}
int toNumber(char ch) {//对应转换规则
switch (ch) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
}
return 0;
}
}
 
 
 

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. Xshell的常用命令

    常用的命令: suse linux 常用命令 (1) 命令ls——列出文件 ls  显示当前目录文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当 ...

  2. iOS: crush when save Core Data

    如果一个页面拥有一个 NSFetchedResultsController 的引用,那么在这个界面将要结束时,一定要 remove 它的 observer - (void)dealloc { //.. ...

  3. SharePoint 2013 处理Promoted Links类型的List下的Tiles View的默认Webpart展示方式

    问题: 为了去掉Photo Gallery的水平滚动效果,更改为根据内容进行自适应宽度多行显示效果 Promoter link --> photo gallery Not horizontal ...

  4. Android Studio使用技巧小记

    1.Android Studio中查看genymotion模拟器中的文件的方法: Tools-->Android Device Moniter 2.快速定位开源代码某功能的实现方法 右击项目-- ...

  5. xml文件加密上传和sftp下载解密基本思路

    AES对称加密效率高,对大文件加密适合.RSA非对称加密效率低,更安全,对小文件加密适合. 整理 11:12 2016/8/4 加密:1.xml xml.md5摘要 2.(xml)aes加密 (xml ...

  6. linux环境中安装ftp服务

    需求说明: 今天项目中有一个新的需求,需要在linux环境中搭建一个ftp服务,在此记录下. 操作过程: 1.通过yum的方式安装ftp服务对应的软件包 [root@testvm01 ~]# yum ...

  7. selenium +chrome headless Adhoc模式渲染网页

    mannual和adhoc模式比较 Manual vs. Adhoc In the script above, we start the ChromeDriver server process whe ...

  8. [原]unity3D 移动平台崩溃信息收集

    http://m.blog.csdn.net/blog/catandrat111/8534287http://m.blog.csdn.net/blog/catandrat111/8534287

  9. C 语言与动态库相关基础知识

    1.导入文件<>和“”的区别 #include <xxx.h>导入的是标准目录下的.h文件,所谓的标准目录指的是:/use/local/include(一般是第三方头文件)以及 ...

  10. Go之简单并发

    func Calculate(id int) { fmt.Println(id) } 使用go来实现并发 func main() { for i := 0; i < 100; i++ { go ...