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 ...
随机推荐
- 工作中常用的 Linux 命令
awk 示例: env变量值如下,需要获得pkg_url的链接值: {"name": "michael", "sex": "mal ...
- XSS漏洞学习笔记
XSS漏洞学习 简介 xss漏洞,英文名为cross site scripting. xss最大的特点就是能注入恶意的代码到用户浏览器的网页上,从而达到劫持用户会话的目的. 说白了就是想尽办法让你加载 ...
- 论文笔记:Decoders Matter for Semantic Segmentation: Data-Dependent Decoding Enables Flexible Feature Aggregation
Decoders Matter for Semantic Segmentation: Data-Dependent Decoding Enables Flexible Feature Aggregat ...
- 【我的前端自学之路】【HTML5】.html和.htm的区别
以下为自学笔记内容,仅供参考. 转发请保留原文链接:https://www.cnblogs.com/it-dennis/p/10508171.html .htm 和 .html 的区别 .htm 和 ...
- 【HNOI 2017】影魔
Problem Description 影魔,奈文摩尔,据说有着一个诗人的灵魂.事实上,他吞噬的诗人灵魂早已成千上万.千百年来,他收集了各式各样的灵魂,包括诗人.牧师.帝王.乞丐.奴隶.罪人,当然,还 ...
- 《Java8实战》读书笔记
个人感悟: 1.lambda表达式,补充了JAVA在面向对象之外,面向过程的一面.在写面向过程代码的时候更方面了,甚至可以利用代码来做类似数学公式的运算(P64) 2.流,对集合的操作,就像用SQL对 ...
- JQuery 数组获取和删除元素
<script> var target = []; target [0] = "aaa000"; target [1] = "bbb111"; ta ...
- lua---研究 c-api
c-api 参考手册:http://www.leeon.me/a/lua-c-api-manual
- webdirver.Chrom() selenium webdirver调用谷歌浏览器的问题解决
第一个坑: 没有将谷歌驱动放到 /usr/local/bin 目录下 第二个坑: 没有将谷歌浏览器.exe放在 /usr/local/bin 目录下(注意:驱动和浏览器在一个路径下才能使 ...
- nginx的前世今生
Nginx最初的设计是称为一个http服务器,一个能够解决C10K问题的http服务器. 那么问题来了,什么是C10K呢?C10K问题即(单机10万个并发链接问题),这个概念最早是由Dan Kegel ...