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的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  4. 5.Integer to Roman && Roman to Integer

    Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...

  5. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  6. 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

    [本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...

  7. No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

  8. 【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 ...

  9. 3月3日(5) Roman to Integer

    原题 Roman to Integer 题意很简单,把Roman字母翻译成int. 实现方式也不难,针对每个字符转成int,从右往左,依次判断,如果当前值比上一个值大则相加,小则相减. 什么,你问我怎 ...

随机推荐

  1. 两个对象值相同 (x.equals(y) == true),但却可有不同的 hash code,这句话对不对?

    不对,如果两个对象x和y满足x.equals(y) == true,它们的哈希码(hash code)应当相同.Java对于eqauls方法和hashCode方法是这样规定的: (1)如果两个对象相同 ...

  2. Windows exit

    退出 CMD.EXE 程序(命令解释器)或当前批处理脚本. EXIT [/B] [exitCode] /B          指定要退出当前批处理脚本而不是 CMD.EXE.如果从一个         ...

  3. Oracle大数据查询优化

    1.对于像状态之类的列,不是很多的,就可以加位图索引,对于唯一的列,就加唯一索引,其余的创建普通索引. 2.尽量不要使用select * 这样的查询,指定需要查询的列. 3.使用hits  selec ...

  4. Django项目:堡垒机(Linux服务器主机管理系统)--03--03堡垒机在Linux系统里记录会话日志02/02

    #main.py #本文件写所有的连接交互动作程序 # ————————————————03堡垒机在Linux系统里记录会话日志 开始———————————————— from Fortress im ...

  5. Java-Maven-pom.xml-project-packaging:packaging(war/jar)

    ylbtech-Java-Maven-pom.xml-project-packaging:packaging(war/jar) 1.返回顶部 1.packaging 1.1 war <!-- 打 ...

  6. 第十三篇:一点一滴学ibatis(二)映射文件

       首先给出一个常见的映射文件局部模板 <?xml version="1.0" encoding="utf-8" ?><!DOCTYPE s ...

  7. 19.SimLogin_case08

    # 模拟登录微博 import time import base64 import rsa import binascii import requests import re import rando ...

  8. <Python基础>字典的基本操作

    ''' 小知识 1.字典的键只能是不可变数据类型:int 元组 bool str(可哈希) 字典查找数据会使用二分查找,会先用哈希表将键转化为数字然后进行查找 ''' s = { "name ...

  9. P1280 尼克的任务 /// DP(选择性地)

    题目大意: https://www.luogu.org/problemnew/show/P1280 题解 手推一遍思路更清晰 #include <bits/stdc++.h> using ...

  10. printk函数

    一个不同是 printk 允许你根据消息的严重程度对其分类, 通过附加不同的记录级别或者 优先级在消息上. 你常常用一个宏定义来指示记录级别. 例如, KERN_INFO, 我们之前曾 在一些打印语句 ...