13. Roman to Integer ★
题目内容:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题目分析:罗马数字向阿拉伯数字的转换情况如下:
1、M=1000 D=500 C=100 L=50 X=10 V=5 I=1
2、若小的罗马符号出现在大的罗马符号的前面,则小的罗马符号代表的数字改为负。这种情况只能出现有限的情况。
因此目前想到两种方法。
第一种方法是再扫描出I之外的每个符号时都查看这个符号之前的符号,如果是比他小的符号,则要减去小的符号代表数值的两倍。
第二种方法是将数字中每个符合代表的数值都加上,然后查看数字中有没有出息要减去值的那些符号对。
题目代码:

public class Solution {
public static int romanToInt(String s) {
char[] ss = new char[100];
int sum = 0;
for(int i=0; i<s.length();i++)
ss[i]=s.charAt(i);
for(int i=0; i<s.length();i++){
if (ss[i]=='I'){
sum+=1;
}
if (ss[i]=='V'){
sum+=5;
if(i>0&&ss[i-1]=='I'){
sum-=2;
}
}
if (ss[i]=='X'){
sum+=10;
if(i>0&&ss[i-1]=='I'){
sum-=2;
}
if(i>0&&ss[i-1]=='V'){
sum-=10;
}
}
if (ss[i]=='L'){
sum+=50;
if(i>0&&ss[i-1]=='I'){
sum-=2;
}
if(i>0&&ss[i-1]=='V'){
sum-=10;
}
if(i>0&&ss[i-1]=='X'){
sum-=20;
}
}
if (ss[i]=='C'){
sum+=100;
if(i>0&&ss[i-1]=='I'){
sum-=2;
}
if(i>0&&ss[i-1]=='V'){
sum-=10;
}
if(i>0&&ss[i-1]=='X'){
sum-=20;
}
if(i>0&&ss[i-1]=='L'){
sum-=100;
}
}
if (ss[i]=='D'){
sum+=500;
if(i>0&&ss[i-1]=='I'){
sum-=2;
}
if(i>0&&ss[i-1]=='V'){
sum-=10;
}
if(i>0&&ss[i-1]=='X'){
sum-=20;
}
if(i>0&&ss[i-1]=='L'){
sum-=100;
}
if(i>0&&ss[i-1]=='C'){
sum-=200;
}
}
if (ss[i]=='M'){
sum+=1000;
if(i>0&&ss[i-1]=='I'){
sum-=2;
}
if(i>0&&ss[i-1]=='V'){
sum-=10;
}
if(i>0&&ss[i-1]=='X'){
sum-=20;
}
if(i>0&&ss[i-1]=='L'){
sum-=100;
}
if(i>0&&ss[i-1]=='C'){
sum-=200;
}
if(i>0&&ss[i-1]=='D'){
sum-=1000;
}
}
}
return sum;
}
}
13. Roman to Integer ★的更多相关文章
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- C# 写 LeetCode easy #13 Roman to Integer
13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【LeetCode】13. Roman to Integer (2 solutions)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- 13. Roman to Integer[E]罗马数字转整数
题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
随机推荐
- android开发_view和view属性
一.view视图的宽度和高度属性,属性值:固定和浮动两种状态 1属性为固定值 <View android:layout_width="30dp" android:layout ...
- flutter常规错误
https://blog.csdn.net/mo_feng_/article/details/85104013
- appium-android 遇到swipe函数无法使用的问题及解决办法
问题:cannot resolve method swipe() 问题出现原因:File->Project Structure->Modules->Dependencies-> ...
- C# 简单粗暴写日志
public static void WriteLog(string text) { string path = AppDomain.CurrentDomain.BaseDirectory; path ...
- Windows server 2012 install .net core sdk 2.2.103
Windows8.1-KB2919442-x64 Windows8.1-KB2919355-x64 vc_redist.x64 dotnet-sdk-2.2.103-win-x64 dotnet-ho ...
- showdoc 自动脚本安装
========================== showdoc 简介==========================在线文档管理系统很多, 比如阿里的语雀.腾讯的 TAPD 平台也包括文档管 ...
- hdu 6006 Engineer Assignment 状压dp
Engineer Assignment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- leecode第五百五十七题(反转字符串中的单词 III)
class Solution { public: string reverseWords(string s) { string res; stack<char> sta; string:: ...
- 开启BBR
BBR 目的是要尽量跑满带宽, 并且尽量不要有排队的情况, 效果并不比速锐差Linux kernel 4.9+ 已支持 tcp_bbr 下面简单讲述基于KVM架构VPS如何开启附:OpenVZ 架构V ...
- Eclipse+Maven环境下java.lang.OutOfMemoryError: PermGen space及其解决方法
转自 https://blog.csdn.net/qdgengwenfei/article/details/71455432 java.lang.OutOfMemoryError: PermGen ...