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. 10月23日——作业1——while循环练习

    while循环'''此类编程题,注意带进去试一试1.九九乘法表row=1while row<=9: col=1 while col<=row: print(col,"*" ...

  2. Jmeter使用:JSON返回数据处理

    想要解决的问题: 通过查询接口,获取response数据,作为下个请求post的参数值 后置处理器:JSON Extractor 先下载一个插件:JSONPathExtractor Names of ...

  3. 几个 GetHashCode 函数

    几个 GetHashCode 函数: DBTables.pas Delphi/Pascal code   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  4. SPSS Modeler数据挖掘:回归分析

    SPSS Modeler数据挖掘:回归分析 1 模型定义 回归分析法是最基本的数据分析方法,回归预测就是利用回归分析方法,根据一个或一组自变量的变动情况预测与其相关的某随机变量的未来值. 回归分析是研 ...

  5. springmvc 拦截器不拦截jsp,只拦截控制器的访问

    spring是鼓励把jsp放到WEB-INF文件夹中,然后通过控制器进行访问

  6. JavaScript基础的一些小总结

    一.js变量 1.变量声明: var 关键字来进行变量声明  变量是弱类型 1.数字   2.小数    3.boolean   4.字符串     4.字符 验证数据类型:1.整数,小数是numbe ...

  7. ajax无刷新上传

    我们在使用上传控件的时候,会遇到刷新的问题,最近使用ajax做的上传,觉得效果还是很不错. 首先,我们需要在页面上放上上传控件:需要注意的是,我们必须放在form里面,实现表单上传.  <for ...

  8. 筛法求欧拉函数(poj2478

    求1-n的欧拉函数的值 #include <iostream> #include <cstdio> #include <queue> #include <al ...

  9. 2.vue插件总结——总有你能用上的插件

    UI组件 框架 element - 饿了么出品的Vue2的web UI工具套件 mint-ui - Vue 2的移动UI元素 iview - 基于 Vuejs 的开源 UI 组件库 Keen-UI - ...

  10. zabbix 自动发现端口服务监控教程

    目录 创建数据表(收集haproxy服务的信息) 针对生成的数据表做监控 在haproxy服务机器上配置 在zabbix上添加监控 前言: 1.线上业务使用了几十上百台haproxy服务,需要针对这些 ...