public class Solution { public int romanToInt(String s) { 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
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4. classSolution {publicintromanToInt(String s) { String sym= "MDCLXVI";int[] val = {1000,500,100,50,10,5,1};intnum = 0;intp;//array pointerfor(inti = 0; i < s.length(); i++){ p=sym.indexOf(s.charAt(i));if(...
the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used: ...
publicintromanToInt(String s) { Map<Character,Integer> map =newHashMap<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); char[] c = s.toCharArray(); intresult =0;...
13. Roman to Integer Question Description My Key package LeetCode; import java.util.HashMap; import java.util.Map; public class Test { public static void main(String[] args) { System.out.println(romanToInt("IIII")); } public static int romanToInt(String s) {...
* 功能说明:LeetCode 13 - Roman to Integer * 开发人员:Tsybius * 开发时间:2015年8月3日 */ public class Solution { /** * 罗马数字转换为阿拉伯数字 * @param s 被转换的罗马数字 * @return 转换后的阿拉伯数字 */ public int romanToInt(String s) { ...
public int romanToInt(String s) { int total = charToInt(s.charAt(0)); for(int i = 1; i < s.length(); i++){ int prev = charToInt(s.charAt(i-1)); int curr = charToInt(s.charAt(i)); if(curr <= prev){ total += curr; ...
(r == 'X') { switch (r_after) { case 'L': return true; case 'C': return true; } } if (r == 'C') { switch (r_after) { case 'D': return true; case 'M': return true; } } return false; } public int romanToInt(String s) { int ans = 0; for (int i = 0; i...
Roman to Integer(罗马数字转整数) * 给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。 */ public class Solution13 { public static void main(String[] args) { Solution13 solution13 = new Solution13(); String s = "IV"; System.out.println(solution13.romanToInt(s)); } ...
java: public int romanToInt(String s) { LinkedHashMap<String, Integer> roman = new LinkedHashMap< String,Integer>(); roman.put("I", 1); roman.put("V", 5); roman.put("X", 10); roman.put("L", 50); roman.put("C", 100); ...