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. unity知识点思维导图

    写了个思维导图,总结了下学习unity的知识点感觉还有其他很多的没写到,等我慢慢在工作中完善它,这是下面的链接,后续会根据他的每一个细节来丰富我的博客. 详细地址: http://naotu.baid ...

  2. webpackJsonp is not defined?

    用了CommonsChunkPlugin生成了公共文件,但是页面还没有引用这个公共文件 比如下面这个配置 var webpack = require('webpack'); var path = re ...

  3. ectouch 常用功能

    1. ectouch销量文件mobile\include\apps\default\common\function.php function get_goods_count($goods_id) { ...

  4. OPenCL

    OpenCLhttp://baike.baidu.com/link?url=7uHWCVUYB3Sau_xh3OOKP-A08_IvmT1SJixdAXKezCuCfkzeSQDiSmesGyVGk8 ...

  5. VBA笔记(二)——基础语法

    数据类型 VBA提供了15种标准数据类型,具体见下表: 变量 Sub 变量学习() 'Dim 变量名 As 数据类型 Dim str1 As String '声明定长的String变量 '使用变量类型 ...

  6. PHP保留2位小数 格式化小数、浮点数

    JS保留两位小数例子 四舍五入使用函数 toFixed() <html> <head> </head> <script language="java ...

  7. 利用Nginx实现域名转发 不修改主机头

    在conf下 新建一个 文件 格式 : 域名.conf  例如:www.test.com.conf 文件里配置: server{ listen 80; server_name www.test.com ...

  8. column css3 列宽

    column-count 属性规定元素应该被分隔的列数: div { -moz-column-count:3; /* Firefox */ -webkit-column-count:3; /* Saf ...

  9. OpenCV 2.4.13 编译使用(VS2015下)

    OpenCV2.4.13编译(VS2015) 这里给出已经编译好的的下载路径.包括Win64的debug和release版本. OpenCV for MSVC14 Win64 1.下载OpenCV源码 ...

  10. C#夯实基础系列之字符串

    string作为我们在编程当中用的最多的数据类型,同时又由于它的特殊性,怎么强调它的重要性都不为过,理解string的一些类型和存储机制,有助于我们写出正确且高效的代码. 一.string类型 1.s ...