Given a roman numeral, convert it to an integer.

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

题目大意:把罗马数字转换成的整数

遍历每个字符,前一个字符比后一个字符小,相减

 public class Solution{
public int romanToInt(String s) {
s = s.trim();
if (null == s || "".equals(s))
return 0;
HashMap<Character,Integer> 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);
int cur = s.length()-1;
int sum = map.get(s.charAt(cur));
int tmp1,tmp2;
cur--;
while(cur>=0){
tmp1 = map.get(s.charAt(cur+1));
tmp2 = map.get(s.charAt(cur));
if(tmp2<tmp1)
sum = sum - tmp2;
else
sum = sum + tmp2;
cur--;
}
return sum;
} public static void main(String[] args){
String param = "CMXLVI";
if(args.length==1){
param = args[0];
}
Solution solution = new Solution();
int res = solution.romanToInt(param);
System.out.println(res);
}
}

[LeetCode]-011-Roman_to_Integer的更多相关文章

  1. 【JAVA、C++】LeetCode 011 Container With Most Water

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  2. [Leetcode]011. Container With Most Water

    public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...

  3. leetcode python 011

    ####给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点.##绘制n条垂直线,使得线i的两个端点位于(i,ai)和(i,0).##找到两条线,它们与x轴一起形成一个容器,这 ...

  4. 【LeetCode】011 Container With Most Water

    题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...

  5. [LeetCode] Binary Watch 二进制表

    A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  8. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  9. [LeetCode] Stickers to Spell Word 贴片拼单词

    We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...

  10. [LeetCode] Bulb Switcher II 灯泡开关之二

    There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...

随机推荐

  1. DataFrame.to_dict(orient='dict')

    DataFrame.to_dict(orient=’dict’) >>> df = pd.DataFrame({'name':[1,2,3],"class":[1 ...

  2. Python 经典面试题(一)

    一.浮点数运算 题目 判断浮点数的运行结果是否相等:  a = 0.1 b = 0.2 c = 0.3 assert a + b == c 题目解析: 本题考查的是计算机的浮点运算知识点.不仅是 py ...

  3. ubuntu安装selenium谷歌插件

    爬虫之selenium 安装与 chromedriver安装 今天学到一个有意思的插件,就是chromedriver,在爬虫的时候,如果网站反爬虫做的很好,自己又很想爬去里面的数据,那就可以用这个插件 ...

  4. 动态代理之JDK 和 CGLIB

    方式一:jdk动态代理 通过proxy类的newProxyInstance(ClassLoader loader, Class<?>[] interfaces,InvocationHand ...

  5. phpmyadmin导入大容量.sql文件

    phpmyadmin导入大容量.sql文件 在phpmyadmin目录文件夹下建立一个文件夹,如importSqlFile 将想要导入的sql文件放入importSqlFile文件夹中 打开confi ...

  6. node.js安装后输入“node -v”提示'node' 不是内部或外部命令,也不是可运行的程序的解决方法

    换个电脑,重新搭配环境的时候遇到的问题.node.js已经在官网进行下载安装了,但是VScode里面显示不是内部的命令,也不是可运行的程序 但是在cmd控制台还是能查到的 借助网上的方法进行了测试和调 ...

  7. ArrayList实现原理分析

    ArrayList使用的存储的数据结构 ArrayList的初始化 ArrayList是如何动态增长 ArrayList如何实现元素的移除 ArrayList小结 ArrayList是我们经常使用的一 ...

  8. Haproxy学习总结

    一.Haproxy介绍 1.实现了一种事件驱动,单一进程模型,支持数万计的并发连接,用于为tcp和http应用程序提供高可用,负载均衡和代理服务的解决方案,尤其适用于高负载且需要持久连接或7层处理机制 ...

  9. git 命令解析

    git 补丁 Git 提供了两种补丁方案:   (1)用 git diff 生成的UNIX标准补丁.diff文件:.diff文件只是记录文件改变的内容,不带有commit记录信息,多个commit可以 ...

  10. openCV for python的使用

    一.openCV简介 OpenCV是一个开源的跨平台计算机视觉库.它轻量级而且高效——由一系列 C 函数和少量C++类构成,同时提供了Python.Ruby.MATLAB等语言的接口,实现了图像处理和 ...