如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字。

 public class Solution {
private static char[][] chars = {{'I', 'V'}, {'X', 'L'}, {'C', 'D'}, {'M', 'O'}};
private static HashMap<Character, Integer> map = null;
private static void init() {
map = new HashMap<Character, Integer>();
for (int i = 0; i < 4; i++) {
map.put(chars[i][0], (int) Math.pow(10, i));
map.put(chars[i][1], 5 * (int) Math.pow(10, i));
}
}
public static int romanToInt(String s) {
if (map == null) {
init();
}
int length = s.length();
int result = 0;
boolean flag = true;
int prev = map.get(s.charAt(0));
for (int i = 1; i < length; i++) {
int x = map.get(s.charAt(i));
if (prev >= x) {
result += prev;
} else {
result -= prev;
}
prev = x;
}
result += prev;
return result;
}
}

[leetcode] 13. Roman to Integer的更多相关文章

  1. 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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

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

  3. [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 ...

  4. 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 ...

  5. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  6. LeetCode 13. Roman to Integer(c语言版)

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

  7. LeetCode 13 Roman to Integer 解题报告

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

  8. [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 ...

  9. [LeetCode] 13. Roman to Integer ☆☆

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. C#的访问级别

    可访问性级别有 public                       访问不受限制.  protected                 访问仅限于包含类或从包含类派生的类型.  interna ...

  2. Beta阶段第四次Scrum Meeting

    情况简述 Beta阶段第四次Scrum Meeting 敏捷开发起始时间 2016/12/13 24:00 敏捷开发终止时间 2016/12/14 24:00 会议基本内容摘要 进度平稳推进,分配新任 ...

  3. Acer-宏碁电脑BOIS

    进入电脑BOIS界面; 1.开机(一闪而过)注意第一屏左下角,会有进入BIOS按键提示. 2.如一开机没有进入BIOS的键值提示,取而代之的是品牌机的Logo,可参阅以下列表:不是品牌机可参阅主板设置 ...

  4. 清北学堂模拟赛day7 错排问题

    /* 考虑一下已经放回m本书的情况,已经有书的格子不要管他,考虑没有书的格子,不考虑错排有(n-m)!种,在逐步考虑有放回原来位置的情况,已经放出去和已经被占好的格子,不用考虑,剩下全都考虑,设t=x ...

  5. android学习链接

    Android studio/Gradle学习资源:http://www.cnblogs.com/licheetec/p/4475426.html

  6. java生产者/消费者模式实现——一生产者一消费者(操作值)

    胶多不粘话多不甜,直接上代码: 生产者类: /** * Created by 51304 on 2016/2/28. */ public class P { private String lock; ...

  7. PHP如何通过Http Post请求发送Json对象数据?

    因项目的需要,PHP调用第三方 Java/.Net 写好的 Restful Api,其中有些接口,需要 在发送 POST 请求时,传入对象. Http中传输对象,最好的表现形式莫过于JSON字符串了, ...

  8. C/C++的开发环境安装

    sudo apt-get install gcc sudo apt-get install g++ sudo apt-get install cmake sudo apt-get install ma ...

  9. Linux进程间通信(一): 信号 signal()、sigaction()

    一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...

  10. MongoDB Windows环境安装及配置

    MongoDB一般安装 1.首先到官网(http://www.mongodb.org/downloads )下载合适的安装包,目前的最新版本为2.6 安装包有zip和msi格式的,这里推荐下载zip格 ...