lc13 Roman to Integer
lc13 Roman to Integer
遇到那六种特殊情况分别-2,-20,-200,
按照罗马数字的规则,每种只可能出现一次。所以只需要考虑一次,用indexOf()即可判断是否出现这几种特殊情况
然后遍历s,按照每个字符的定义,加上value即可
class Solution {
public int romanToInt(String s) {
int res = 0; if(s.indexOf("IV") != -1)
res -= 2;
if(s.indexOf("IX") != -1)
res -= 2;
if(s.indexOf("XL") != -1)
res -= 20;
if(s.indexOf("XC") != -1)
res -= 20;
if(s.indexOf("CD") != -1)
res -= 200;
if(s.indexOf("CM") != -1)
res -= 200; for(char i : s.toCharArray()){
if(i == 'I')
res += 1;
if(i == 'V')
res += 5;
if(i == 'X')
res += 10;
if(i == 'L')
res += 50;
if(i == 'C')
res += 100;
if(i == 'D')
res += 500;
if(i == 'M')
res += 1000;
} return res;
}
}
lc13 Roman to Integer的更多相关文章
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- 5.Integer to Roman && Roman to Integer
Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- 3月3日(5) Roman to Integer
原题 Roman to Integer 题意很简单,把Roman字母翻译成int. 实现方式也不难,针对每个字符转成int,从右往左,依次判断,如果当前值比上一个值大则相加,小则相减. 什么,你问我怎 ...
随机推荐
- JS预解析与变量提升
预解析 JavaScript代码的执行是由浏览器中的JavaScript解析器来执行的.JavaScript解析器执行JavaScript代码的时候,分为两个过程:预解析过程和代码执行过程 预解析过程 ...
- spring Cache + Redis 开发数据字典以及自定义标签
一.数据库表结构 1. 分类表:dict_type 2. 子项表:dict_entry 二.页面维护功能示意图: 1. 分类管理 点击子项管理进入子项管理页面 2.子项管理 三.数据字典添加到缓 ...
- TopCoder[SRM513 DIV 1]:PerfectMemory(500)
Problem Statement You might have played the game called Memoria. In this game, there is a board ...
- jquery学习笔记(三):事件和应用
内容来自[汇智网]jquery学习课程 3.1 页面加载事件 在jQuery中页面加载事件是ready().ready()事件类似于就JavaScript中的onLoad()事件,但前者只要页面的DO ...
- spring源码读书笔记
如果我们在web项目里面使用spring的话,通常会在web.xml里面配置一个listener. <listener> <listener-class> org.spring ...
- Spring Cloud Config的配置中心使用非对称性加密
首先,我们需要通过keytool工具来生成密钥对. keytool是JDK中的一个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认 ...
- 《DSP using MATLAB》Problem 8.41
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- 【学术篇】oj.jzxx.net2701 无根树
这是一道来自OIerBBS的题目.. 原帖地址:http://www.oierbbs.com/forum.php?mod=viewthread&tid=512?fromuid=71 (似乎是个 ...
- C++ 系列:static
C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用.一.面向过程设计中的sta ...
- WPF 多语言
1.http://www.cnblogs.com/bear831204/archive/2009/03/17/1414026.html 2.http://www.cnblogs.com/horan/a ...