[LeetCode Java] 13 Roman to Integer
来源:互联网 发布:人工智能公司 编辑:程序博客网 时间:2024/06/16 14:23
/** * Given a roman numeral, convert it to an integer. * * Input is guaranteed to be within the range from 1 to 3999. */import java.util.HashMap;public class RomanToInteger {static int RomanToInt(String s) {HashMap<String, Integer> m = new HashMap<String, Integer>();m.put("I", 1);m.put("V", 5);m.put("X", 10);m.put("L", 50);m.put("C", 100);m.put("D", 500);m.put("M", 1000);int currentRadix = 1;int result = 0;for (int i = s.length() - 1; i >= 0; i--) {int radix = m.get(s.substring(i, i + 1));if (radix >= currentRadix) {result += radix;currentRadix = radix;} else {result -= radix;}}return result;}public static void main(String[] args) {System.out.println(RomanToInt("I"));System.out.println(RomanToInt("X"));System.out.println(RomanToInt("C"));System.out.println(RomanToInt("MCMLXXVI"));System.out.println(RomanToInt("MMMCMXCIX"));}}
0 0
- Leetcode[12-13] Roman to Integer & Integer to Roman(Java)
- [LeetCode Java] 13 Roman to Integer
- LeetCode 13 -Roman to Integer ( JAVA )
- [Leetcode] Roman to Integer (Java)
- [Leetcode] Integer to Roman (Java)
- 【Leetcode】Roman to Integer JAVA
- [LeetCode][Java] Integer to Roman
- [LeetCode][Java] Roman to Integer
- LeetCode (Java) Roman to Integer
- 【leetcode】Roman to Integer【java】
- LeetCode : Integer to Roman [java]
- LeetCode : Roman to Integer [java]
- leetcode:Integer to Roman 【Java】
- leetcode Integer to Roman(Java)
- leetcode Roman to Integer(Java)
- [LeetCode] Roman to Integer || Integer to Roman (JAVA)
- leetcode-13:Roman to Integer
- leetcode 13 Roman to Integer
- android+Servlet
- 简单方法求解丑陋数
- BZOJ 2176 Strange string 最小表示法
- 迅雷面试题
- 2-2-r-阅读程序(1)
- [LeetCode Java] 13 Roman to Integer
- hdu 5056 Boring count ( 窗口转移法)
- 李白打酒问题--三个参数的递归
- H3C Secpath 1000-S-G SSL VPN启动CLI配置方法(官方网站没有,自己总结的)
- android开发-3设置操作系统
- 使用chrome获取上网信息
- 在solidworks装配体中更改一个子装配体或零件的Configuration的方法
- Cell的重用机制
- zabbix通过脚本发短信