13. Roman to Integer ★

题目内容:

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;       
    }
   
}

时间: 2024-08-07 16:36:44

13. Roman to Integer ★的相关文章

leetCode 13. Roman to Integer 字符串

13. Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 补充:罗马数字 罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

12. Integer to Roman &amp;&amp; 13. Roman to Integer &amp;&amp; 273. Integer to English Words

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

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 ,即为两个并列的 1.12 写做 XII ,即为 X + II . 27 写做 XXVII, 即为 XX + V + II . 通常情况下,罗马数字中小的数字在大的数字的右边.但也存在特例,例如 4 不写做 IIII,而是 IV.数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数

Leetcode 13. Roman to Integer(水)

13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve i

LeetCode 13. Roman to Integer

https://leetcode.com/problems/roman-to-integer/#/description Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 字符串简单题,要搞清楚转换规则.如果当前字母比前一个大,则相减,比如IV = 5 - 1:否则就相加,比如VI = 5 + 1,II = 1 + 1. 罗马数字_

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 from 1 to 3999. Subscribe to see which companies asked this questio 解析:给出一个罗马数字,要求把其转换为一个整数.输入范围在1到3999内. 罗马数字的规则如下: 罗马数字 I V X L C D M 代表的阿拉伯数字 1 5

【leetcode】13. Roman to Integer

题目描述: Given a roman numeral, convert it to an integer. 解题思路: 罗马计数法有以下规则: 基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: 不能把基本数字 V .L .D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目:放在大数的右边采用相加的方式构成数目.只能使用一个: V 和 X 左边的小数字只能用 Ⅰ: L 和 C 左边的小数字只能用X: D

13 Roman to Integer (easy)

Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 简单介绍一下罗马数字,摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗马数字中没有“0”,与进位制无关.一般认为罗马数字只用来记数,而不作演算

[LeetCode][13]Roman to Integer解析 罗马字符转int类型关于栈的常数实现-Java实现

Q: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. A: 以下解法和代码没有借阅以往任何资料,如果有更好的解法请在评论区留言 看到这一题我真是感叹城会玩,昨天刚刚解过一个int转罗马字母,今天又反过来解.自叹弗如.这题的该意思就是给一个罗马字符,把他转化成一个int数字,范围在1-3999之间.关于罗马数字的详细信息就不复制了,

LeetCode记录之13——Roman to Integer

能力有限,这道题采用的就是暴力方法,也只超过了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之间. 1 class Solution { 2 public int romanToInt(