题目内容:

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

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

  2. Leetcode 13. Roman to Integer(水)

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

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

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

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

  5. 13. Roman to Integer【leetcode】

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

  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. 《LeetBook》leetcode题解(13):Roman to Integer[E]

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

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

  9. 13. Roman to Integer[E]罗马数字转整数

    题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

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

随机推荐

  1. oracle 增加大字段项

    --不同类型增加大字段项 alter table 表名 add 新增一个字段B clob; --将需要改成大字段的项内容copy到大字段中 update 表名 set 新增一个字段B=字段A; --将 ...

  2. 手写JavaScript常用的函数

    一.bind.call.apply函数的实现 改变函数的执行上下文中的this指向,但不执行该函数(位于Function构造函数的原型对象上的方法) Function.prototype.myBind ...

  3. 无法启动iis express web 服务器

    删除项目文件夹下的隐藏文件夹 (.vs)文件

  4. react的this.setState没有触发render

    一.浅比较 出现情况: 明明改变了值, 并且回调函数也触发了, 但是就是不触发render import React, { PureComponent } from 'react' import { ...

  5. “妄”眼欲穿之CSS 居中问题

    妄:狂妄: 不会的东西只有怀着一颗狂妄的心,假装能把它看穿吧. 作为一个什么都不会的小白,为了学习,特别在拿来主义之后写一些对于某些css布局的总结,进一步加深对知识的记忆.知识是人类的共同财富,中华 ...

  6. vue_全局注册过滤器

    在一个项目中, 某些过滤器全局都有可能用的到, 统一管理并自动化全局注册是很方便的. 代码如下, 后续只需要在src/filters/index.js中添加方法就可以全局使用过滤器了. // src/ ...

  7. Asp.net core Identity + identity server + angular 学习笔记 (第一篇)

    用了很长一段时间了, 但是一直没有做过任何笔记,感觉 identity 太多东西要写了, 提不起劲. 但是时间一久很多东西都记不清了. 还是写一轮吧. 加深记忆. 这是 0-1 的笔记, 会写好多篇. ...

  8. koa源码之delegate使用

    koa中context可以直接调用request和response属性的重要原因是使用了delegate将req和res的属性代理到context, Delegator.prototype.gette ...

  9. 1GB pages can only be allocated at boot time using hugepages= and not freed afterwards

    2018-6-27 9:12:38 https://stackoverflow.com/questions/26385554/error-setting-nr-hugepages-via-sysfs ...

  10. vue中data中引用本地图片报错404

    首先说明vue-cli中assets和static两个文件的区别 1.assets在项目编译的过程中会被webpack处理理解为模块依赖,如果执行npm run dev或npm run build命令 ...