LeetCode 13. Roman to Integer 罗马数字转整数
2017-04-19 12:11
477 查看
题目
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
按照说明书写代码:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
按照说明书写代码:
public class Solution { // 即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。记数的方法: // 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3; // 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12; // 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9; public int romanToInt(String s) { if(s==null||s.length()==0) return 0; int total = 0; char[] input = s.toCharArray(); int loc = 0; while(loc<input.length){ int mid = loc; if(input[loc]=='M'){ total += 1000; loc++; if(loc==input.length) break; }if(input[loc]=='V'){ total += 5; loc++; if(loc==input.length) break; }if(input[loc]=='L'){ total += 50; loc++; if(loc==input.length) break; }if(input[loc]=='D'){ total += 500; loc++; if(loc==input.length) break; } if(input[loc]=='I'){ int repeat = 1; loc++; while(loc<input.length&&input[loc]=='I'){ repeat++; loc++; } if(loc<input.length&&(input[loc]=='X'||input[loc]=='C'||input[loc]=='M'||input[loc]=='V'||input[loc]=='L'||input[loc]=='D')){ total -= repeat; }else{ total += repeat; if(loc==input.length) break; } } if(input[loc]=='X'){ int repeat = 1; loc++; while(loc<input.length&&input[loc]=='X'){ repeat++; loc++; } if(loc<input.length&&(input[loc]=='C'||input[loc]=='M'||input[loc]=='L'||input[loc]=='D')){ total -= repeat*10; }else{ total += repeat*10; if(loc==input.length) break; } } if(input[loc]=='C'){ int repeat = 1; loc++; while(loc<input.length&&input[loc]=='C'){ repeat++; loc++; } if(loc<input.length&&(input[loc]=='M'||input[loc]=='D')){ total -= repeat*100; }else{ total += repeat*100; if(loc==input.length) break; } } if(loc==mid) break; //没有移动,退出循环 } return total; }另一种解释,小的在大的左面就是减去小的
public int romanToInt(String s) { int nums[]=new int[s.length()]; for(int i=0;i<s.length();i++){ switch (s.charAt(i)){ case 'M': nums[i]=1000; break; case 'D': nums[i]=500; break; case 'C': nums[i]=100; break; case 'L': nums[i]=50; break; case 'X' : nums[i]=10; break; case 'V': nums[i]=5; break; case 'I': nums[i]=1; break; } } int sum=0; for(int i=0;i<nums.length-1;i++){ if(nums[i]<nums[i+1]) sum-=nums[i]; else sum+=nums[i]; } return sum+nums[nums.length-1]; }
相关文章推荐
- [LeetCode] 13. Roman to Integer 罗马数字转为整数
- LeetCode 13. Roman to Integer -- 罗马数字转整数
- leetcode:Integer to Roman(整数转化为罗马数字)
- [Leetcode] Roman to integer 罗马数字转成整数
- LeetCode | Integer to Roman(整数转换成罗马数字)
- python解leetcode——13.Roman to Integer (罗马数字转为整数)
- LeetCode 13. Roman to Integer(罗马数字转阿拉伯数字)
- leetcode-12. Integer to Roman(整数转换成罗马数字)
- 【LeetCode-面试算法经典-Java实现】【013-Roman to Integer (罗马数字转成整数)】
- [LeetCode]12. Integer to Roman(整数转化为罗马数字)
- LeetCode--Roman to Integer 罗马数字转化成整数
- leetCode 12.Integer to Roman (整数转罗马数字) 解题思路和方法
- LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)
- LeetCode 13. Roman to Integer(阿拉伯数字转罗马字符)
- Leetcode 13 Roman to Integer 罗马数字转整数
- [C++]LeetCode 12: Integer to Roman(将整数转换为罗马数字)
- [LeetCode] Roman to Integer 罗马数字转整数
- LeetCode 12 Integer to Roman (整数转罗马数字)
- [LeetCode] Integer to Roman 整数转化成罗马数字
- (LeetCode) Roman to Integer --- 罗马数字转整数