Roman to Integer
来源:互联网 发布:怎么买备案的域名 编辑:程序博客网 时间:2024/06/05 14:50
题目
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
方法
public int romanToInt(String s) { HashMap<String, Integer> hm = new HashMap<String, Integer>(); hm.put("I", 1); hm.put("II", 2); hm.put("III", 3); hm.put("IV", 4); hm.put("V", 5); hm.put("VI", 6); hm.put("VII", 7); hm.put("VIII", 8); hm.put("IX", 9); hm.put("X", 10); hm.put("XX", 20); hm.put("XXX", 30); hm.put("XL", 40); hm.put("L", 50); hm.put("LX", 60); hm.put("LXX", 70); hm.put("LXXX", 80); hm.put("XC", 90); hm.put("C", 100); hm.put("CC", 200); hm.put("CCC", 300); hm.put("CD", 400); hm.put("D", 500); hm.put("DC", 600); hm.put("DCC", 700); hm.put("DCCC", 800); hm.put("CM", 900); hm.put("M", 1000); hm.put("MM", 2000); hm.put("MMM", 3000); int len = s.length(); int num = 0; String currentStr = ""; String preStr = ""; for(int i = 0; i < len; i ++){ char ch = s.charAt(i); currentStr = preStr + ch; if(hm.containsKey(currentStr)){ preStr = currentStr; }else{ num = num + hm.get(preStr); preStr = "" + ch; } } num = num + hm.get(preStr); return num; }
0 0
- Integer to Roman & Roman to Integer
- Integer to Roman & Roman to Integer
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- Integer to Roman and Roman to Integer
- LeetCode Integer to Roman & Roman to Integer
- Roman to Integer/Integer to Roman
- leetcode - roman to integer & integer to roman
- "Roman to Integer" and "Integer to Roman"
- Roman to Integer &&Integer to Roman
- Roman to Integer and Integer to Roman
- Roman to Integer/Integer to Roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- Postgresql生成大量测试数据
- 经理人下命令的学问
- stm32+CC3000移植到ucosii之外部中断服务函数
- iOS7设计指南:轻松上手
- 利用Spring的AbstractRoutingDataSource解决多数据源的问题
- Roman to Integer
- XSuperNEST七大亮点
- APU3 学习笔记博客分享
- 基于html5 localStorage , web SQL, websocket的简单聊天程序
- windows 下配置mongodb方法和注意事项
- 在CTime类中重载<<和>>
- 刘思喆:R 语言在电子商务领域的应用
- HTTP协议header标头详解
- 成都新会展中心招聘会——IT软件行业缺口大