13. Roman to Integer
来源:互联网 发布:linux下的echo命令 编辑:程序博客网 时间:2024/05/16 10:05
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) { Map<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 result = 0; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (i == 0) { result += map.get(ch); } else { char prev = s.charAt(i - 1); if (map.get(ch) > map.get(prev)) { result += map.get(ch); result -= map.get(prev) * 2; } else { result += map.get(ch); } } } return result; }}
0 0
- 13. Roman to Integer
- 13.Roman to Integer
- 13. Roman to Integer
- 13.Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- POJ 1681 Painter's Problem (高斯消元)
- centos 局域网时间同步设置
- c++第四次作业
- Python处理Excel(二):个性化Excel表格
- 指针数组和数组指针
- 13. Roman to Integer
- Jquery实现的Tabs标签页简洁版
- 冒泡排序法
- #leetcode#21. Merge Two Sorted Lists
- bat批处理
- 我自己用的AHK脚本
- 常见的解析方式(java)
- 操作系统典型调度算法
- 12. Integer to Roman