题目内容:

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. 浅谈JavaScript的函数的call以及apply

    我爱撸码,撸码使我感到快乐!大家好,我是Counter.今天就来谈谈js函数的call以及apply,具体以代码举例来讲解吧,例如有函数: function func(a, b) { return a ...

  2. Django 中使用 Celery

    起步 在 <分布式任务队列Celery使用说明> 中介绍了在 Python 中使用 Celery 来实验异步任务和定时任务功能.本文介绍如何在 Django 中使用 Celery. 安装 ...

  3. 迁移python project

    1.从python官网下载同版本的安装版的python,在新机器上安装同样版本的python(python底层是用C语言写的,安装python会安装c  c++用到的库) 2.拷贝united1整个文 ...

  4. 机器学习实战(笔记)------------KNN算法

    1.KNN算法 KNN算法即K-临近算法,采用测量不同特征值之间的距离的方法进行分类. 以二维情况举例:         假设一条样本含有两个特征.将这两种特征进行数值化,我们就可以假设这两种特种分别 ...

  5. 论文笔记:Deeper and Wider Siamese Networks for Real-Time Visual Tracking

    Deeper and Wider Siamese Networks for Real-Time Visual TrackingUpdated on 2019-04-01 16:10:37 Paper ...

  6. 数据结构与算法(C#)入门 --- 线性表

    线性表: 线性表是最简单,最基本,最常用的数据结构.线性表中的数据元素之间存在一对一的关系.即:除了第一个元素,其他元素前面有且只有一个元素:除了最后一个元素,其他元素后面有且只有一个元素.生活中的例 ...

  7. Django troubleshootings

    当在云服务器上部署Django服务时: 1. 首先要在云上的主机上添加相应的端口访问权限. 2. 在project下面的urls.py里面设置如下: ALLOWED_HOSTS = ['www.aby ...

  8. 【python 3】 字符串方法操作汇总

    基础数据类型:str 1.1  字符串大小写转换 所有字母大写 : string.upper() 所有字母小写 : string. lower() 第一个单词的第一个字母大写,其他字母小写 :  st ...

  9. Xilinx Vivado的使用详细介绍(5):调用用户自定义封装的IP核

    Zedboard OLED Display Controller IP v1 介绍 Author:zhangxianhe 本文档提供了快速添加,连接和使用ZedboardOLED v1.0 IP内核的 ...

  10. Devexpress中文语言包汉化

    1.下载对应版本的dev语音包 将语言包解压,放到如下文件夹,编译时会自动复制到bin目录下的. C:\Program Files (x86)\DevExpress 17.2\Components\B ...