[LeetCode]-011-Roman_to_Integer
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的更多相关文章
- 【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). ...
- [Leetcode]011. Container With Most Water
public class Solution { public int maxArea(int[] height) { int left = 0, right = height.length - 1; ...
- leetcode python 011
####给定n个非负整数a1,a2,...,an,其中每个表示坐标(i,ai)处的点.##绘制n条垂直线,使得线i的两个端点位于(i,ai)和(i,0).##找到两条线,它们与x轴一起形成一个容器,这 ...
- 【LeetCode】011 Container With Most Water
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- [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 ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Restore IP Addresses 复原IP地址
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- [LeetCode] Stickers to Spell Word 贴片拼单词
We are given N different types of stickers. Each sticker has a lowercase English word on it. You wou ...
- [LeetCode] Bulb Switcher II 灯泡开关之二
There is a room with n lights which are turned on initially and 4 buttons on the wall. After perform ...
随机推荐
- CF 1141C Polycarp Restores Permutation
Description An array of integers p1,p2,…,pnp1,p2,…,pn is called a permutation if it contains each nu ...
- CGAL 属性配置
libgmp-10.lib libmpfr-4.lib boost_system-vc120-mt-gd-1_63.lib D:\dev\CGAL-4.9\include D:\dev\CGAL-4. ...
- 绑定异常pom
绑定:. <build> <resources> <resource> <directory>src/main/resources</direct ...
- Delphi 常用系统函数与过程
- Qualcomm_Mobile_OpenCL.pdf 翻译-3
3 在骁龙上使用OpenCL 在今天安卓操作系统和IOT(Internet of Things)市场上,骁龙是性能最强的也是最被广泛使用的芯片.骁龙的手机平台将最好的组件组合在一起放到了单个芯片上,这 ...
- 利用python自动发邮件
工作中有时长时间运行代码时需要监控进度,或者需要定期发送固定格式邮件时,可以使用下面定义的邮件函数. 该函数调用了outlook和qqmail的接口,只需要放置到python的环境目录中即可 impo ...
- EFCore, 输出执行的Sql语句到控制台或者调试窗口
.net core 已经集成的各种日志功能,使用efcore时,只需要按情况引入相应的包即可,如果你用的是.net core调试,那么可以引入 Microsoft.Extensions.Logging ...
- 火车采集用到的access查询命令小结
#For zencart #图片网址路径替换 UPDATE Content SET v_products_image=replace(v_products_image, '<img src=&q ...
- VS2015加载错误的解决办法
在开始菜单中找到vs2015 的 vs2015 开发人员命令提示. 在命令提示符中输入 devenv /setup 然后回车,大概几分钟后可以执行完成 重启vs2015 发现问题解决.
- Vue的css动画原理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...