Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

------------------------

题是比较简单,但是解法中用了static block。

public class Solution {

    private static Map<Character, Integer> map;
static {
map = new HashMap<Character, Integer>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
} public int romanToInt(String s) {
if(s == null || s.length() == 0)
return 0;
int res = 0;
for (int i = 0; i < s.length() - 1; ++i){
int cur = map.get(s.charAt(i));
int next = map.get(s.charAt(i + 1));
if(cur < next) {
res -= cur;
} else {
res +=cur;
}
}
res += map.get(s.charAt(s.length()-1));
return res;
}
}

------------------------------

Static block: http://stackoverflow.com/questions/2943556/static-block-in-java

Notice: the order of execution is: static initializer, instance initializer, constructor

[Leetcode] Roman to Integer的更多相关文章

  1. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  2. LeetCode: Roman to Integer 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

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

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

  4. [Leetcode] Roman to integer 罗马数字转成整数

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

  5. leetcode Roman to Integer python

    class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int "& ...

  6. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

  7. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

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

  8. LeetCode 13. 罗马数字转整数(Roman to Integer)

    13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符        数值  I           1  V  ...

  9. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

随机推荐

  1. Node.js入门笔记(6):web开发方法

    使用node进行web开发 用户上网流程: 表面上看:打开浏览器--输入网址--跳转--上网. 背后的过程是什么呢? http请求网址到指定的主机--服务器接收请求--服务器响应内容到用户浏览器--浏 ...

  2. CSS3 时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. hzwer模拟赛 感冒病毒

    题目描述 Description 一种感冒病毒正在学校里传播,这所学校有n个学生,m个学生社团,每个学生可能参加了多个社团,因为同一个社团的学生交流较多,所以如果一个学生感染上感冒病毒,那么他所在的社 ...

  4. 将十六进制色值转换成Color

    在给Background赋值时,除了自带的Red,Blue,Black等,可以通过以下方法赋予其他颜色. 主要是将Hex转换成ARGB(A:alpha,表示透明度.R:Red.G:Green.B:Bl ...

  5. Linux进程间通信(四):命名管道 mkfifo()、open()、read()、close()

    在前一篇文章—— Linux进程间通信 -- 使用匿名管道 中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关 ...

  6. 进阶系列二【绝对干货】---Quartz.Net的入门

    一.Quartz.Net是什么? Quartz.Net是一个开源的作业调度框架,OpenSymphony的开源项目,是Quartz的C#移植项目.非常适合在平时的工作中,定时轮询数据库同步,定时邮件通 ...

  7. Windows7 + Ubuntu双系统安装过程记录

    本文为在已安装Windows7系统的前提下安装Ubuntu Kylin 14.10系统的过程以及期间出现的各种问题的记录. Ubuntu系统下载 Ubuntu Kylin中文官方网站:http://w ...

  8. MySQL5.0安装图解

    打开下载的mysql安装文件mysql-5.0.27-win32.zip,双击解压缩,运行"setup.exe",出现如下界面: 按"Next"继续 选择安装类 ...

  9. Linux服务器jps报process information unavailable

    在Linux下执行 jps 是快速查看Java程序进程的命令,一般情况下hadoop,hbase,storm等进程都是通过jps查看,有些时候因为进程没有被正常结束,比如资源占用过大时挂掉或者没有结束 ...

  10. Redis学习笔记8--Redis发布/订阅

    发布订阅(pub/sub)是一种消息通信模式,主要的目的是解耦消息发布者和消息订阅者之间的耦合,这点和设计模式中的观察者模式比较相似.pub /sub不仅仅解决发布者和订阅者直接代码级别耦合也解决两者 ...