Given a roman numeral, convert it to an integer.

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

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

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

  1. public class Solution {
  2.  
  3. private static Map<Character, Integer> map;
  4. static {
  5. map = new HashMap<Character, Integer>();
  6. map.put('I', 1);
  7. map.put('V', 5);
  8. map.put('X', 10);
  9. map.put('L', 50);
  10. map.put('C', 100);
  11. map.put('D', 500);
  12. map.put('M', 1000);
  13. }
  14.  
  15. public int romanToInt(String s) {
  16. if(s == null || s.length() == 0)
  17. return 0;
  18. int res = 0;
  19. for (int i = 0; i < s.length() - 1; ++i){
  20. int cur = map.get(s.charAt(i));
  21. int next = map.get(s.charAt(i + 1));
  22. if(cur < next) {
  23. res -= cur;
  24. } else {
  25. res +=cur;
  26. }
  27. }
  28. res += map.get(s.charAt(s.length()-1));
  29. return res;
  30. }
  31. }

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

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. JVM Management API

    JVM本身提供了一组管理的API,通过该API,我们可以获取得到JVM内部主要运行信息,包括内存各代的数据.JVM当前所有线程及其栈相关信 息等等.各种JDK自带的剖析工具,包括jps.jstack. ...

  2. Angular-Chart.js 初接触;;;

    可以先看下下面的链接,了解下, 推荐链接 准备工作 JS文件{angular.js.Chart.js.angular-chart.js} 这3个文件我的获取难易程度:Chart.js > ang ...

  3. [Hadoop] 在Ubuntu系统上一步步搭建Hadoop(单机模式)

    1 Hadoop的三种创建模式 单机模式操作是Hadoop的默认操作模式,当首次解压Hadoop的源码包时,Hadoop无法了解硬件安装环境,会保守地选择最小配置,即单机模式.该模式主要用于开发调试M ...

  4. Mac Pro 资源管理器 Double Commander“个性化设置” 备份

    操作系统自带的资源管理器,总是有些别扭的地方,在 Windows 系统下,我一般用 Total Commander(破解版)来作为替代品.现在换为 Mac 系统,自带的 Finer 也不怎么好用,连最 ...

  5. Put-Me-Down项目Postmortem2

    一.设想和目标 二.计划 三.资源 四.变更管理 五.设计/实现 六.测试/发布 总结 一.设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的 ...

  6. H5案例分享:html5移动开发细微之美

    html5移动开发细微之美 1.H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 <meta name="viewport" content="width=dev ...

  7. ReflectionToStringBuilder类

    ReflectionToStringBuilder类是用来实现类中的toString()方法的类,它采用Java反射机制(Reflection),通过reflection包中的AccessibleOb ...

  8. Yslow-23条规则

    1. 减少HTTP请求次数 合并图片.CSS.JS,减少首次访问用户等待时间. 2. 使用CDN就近缓存==>智能路由==>负载均衡==>WSA全站动态加速 3. 避免空的src和h ...

  9. 单节点下多个Tomcat服务器并存的端口号配置

    一个服务器节点同时安装多个tomcat服务器时,如果仅仅修改访问端口号则会提示端口冲突启动失败,还需要修改另外端口号解决,一共需要修改3处地方,修改如下: 编辑配置文件:server.xml 1.首先 ...

  10. Spring读写xml文件

    一.如果只是读取 新建一个 xml 文件,需要满足Spring格式: <?xml version="1.0" encoding="UTF-8"?> ...