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

 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. Cannot initialize Cluster. Please check your configuration for mapreduce.framework.name and the co

    log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFact ...

  2. ios开发网络知识 TCP,IP,HTTP,SOCKET区别和联系

    TCP,IP,HTTP,SOCKET区别和联系 网络由下往上分为:        对应 物理层-- 数据链路层-- 网络层--                       IP协议 传输层--     ...

  3. 让Web页面中的编辑器支持黏贴或直接拖拽来添加图片

    基本原理是将剪贴板中的图片二进制数据转为Base64编码 代码: <html> <head> </head> <body> <script src ...

  4. Base64原理

    一.Base64编码由来 为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASCII码的控制字符就 不能通过邮件传送.这样用途就受到了很大的 ...

  5. ecshop 导出exl表格

    // 导出订单 if(isset($_POST['export'])){ // 统计金额 $sl = "SELECT SUM(goods_amount) as total from" ...

  6. c++11 中成员变量初始化的顺序

    参考C++11FAQ https://www.chenlq.net/cpp11-faq-chs 11以后可以直接在类里面初始化成员变量,类似这样 class A { int a=1; const in ...

  7. can't open a connection to site 'syb_backup'

    sp_configure "allow update",1 go update sysservers  set srvname='SYB_BACKUP', srvnetname=' ...

  8. CSS3实现加载中效果

    代码: <!doctype html> <html> <head> <meta charset="utf-8" /> <tit ...

  9. AP(affinity propagation)研究

    待补充…… AP算法,即Affinity propagation,是Brendan J. Frey* 和Delbert Dueck于2007年在science上提出的一种算法(文章链接,维基百科) 现 ...

  10. C#中Trim()、TrimStart()、TrimEnd()的用法

    string s = " from dual union all ";          s = s.Trim().TrimEnd("union all".To ...