能力有限,这道题采用的就是暴力方法,也只超过了39%的用户。需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式。

  Given a roman numeral, convert it to an integer.

  Input is guaranteed to be within the range from 1 to 3999.

  给定一个罗马数字,将其转换为整数。

  输入保证在1到3999之间。


  

 class Solution {
public int romanToInt(String s) {
int length=s.length();
int num=0;
for(int i=0;i<length;i++){
switch (s.charAt(i)) {
case 'I':{
if((i+1!=length)&&s.charAt(i+1)!='I'){
num-=1;
break;
}
else {
num+=1;
break;
}
}
case 'X':
if((i+1!=length)&&((s.charAt(i+1)=='M')||(s.charAt(i+1)=='D')||(s.charAt(i+1)=='C')||(s.charAt(i+1)=='L'))){
num-=10;
break;
}
else {
num+=10;
break;
}
case 'C':
if((i+1!=length)&&((s.charAt(i+1)=='M')||(s.charAt(i+1)=='D'))){
num-=100;
break;
}
else {
num+=100;
break;
}
case 'M':
num+=1000;
break;
case 'V':
num+=5;
break;
case 'L':
num+=50;
break;
case 'D':
num+=500;
break;
default:
break;
}
}
return num;
}
}

LeetCode记录之13——Roman to Integer的更多相关文章

  1. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  2. [LeetCode&Python] Problem 13. Roman to Integer

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  3. 【leetcode❤python】13. Roman to Integer

    #-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...

  4. 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 ,即 ...

  5. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

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

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

  8. 13. Roman to Integer【leetcode】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  9. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. jquery触发两次onchange事件

    在项目中需要给select绑定改变事件,来触发动作,但是发现改变一次select,onchange方法执行两遍 //这是那个标签,用的是我们公司内部的selectlist <select cla ...

  2. string基本字符系列容器(一)

    C++STL提供了string基本字符系列容器来处理字符串,可以把string理解成字符串类,它提供了添加,删除,替换,查找和比较等丰富的方法. 使用string容器,需要头文件包含声明#includ ...

  3. c++策略模式(Strategy Method)

    别人的博客再讲策略模式时都会讲三国,策略类就是赵云的锦囊,锦囊里装着若干妙计.在打仗时想要用什么妙计,直接从锦囊里去取. 锦囊类: class context { public: context(IS ...

  4. Angular23 loading组件、路由配置、子路由配置、路由懒加载配置

    1 需求 由于Angular是单页面的应用,所以在进行数据刷新是进行的局部刷新:在进行数据刷新时从浏览器发出请求到后台响应数据是有时间延迟的,所以在这段时间就需要进行遮罩处理来提示用户系统正在请求数据 ...

  5. if else的执行流程

    int main(void) { int a, b; char op; float ans; scanf_s("%d%c%d",&a,&op,1,&b); ...

  6. 17.SQL 约束

    约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). 我们将主要探讨以下几种约束: NOT ...

  7. 用conda创建一个tensorflow 虚拟环境

    创建your——user——name = tensorflow 的虚拟环境 xinpingdeMacBook-Pro:~ xinpingbao$ conda create -n tensorflow ...

  8. 用JS实现点击TreeView根节点复选框全选

    以下两种方法哪个不报错就用哪个.用法都是在TreeView标签中加入OnClick="",然后引入函数名即可 第一种方法:(摘自:http://www.cnblogs.com/fr ...

  9. vue父子通信

    首先在组件创建中创建子组件Todos.vue <template> <div class="hello"> <h1>todos show< ...

  10. jQuery对象与DOM对象及互相转化

    <p id=‘’hello”></p> 普通处理,通过标准JavaScript处理: var p = document.getElementById('hello'); p.i ...