[LeetCode][Java] 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-3999之间

算法分析:

* 罗马数字规则:

* 1, 罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。

* 罗马数字中没有“0”。

* 2, 重复次数:一个罗马数字最多重复3次。

* 3, 右加左减:

* 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。

* 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。

AC代码:

public class Solution
{
    private  int sss;

    public  int romanToInt(String s)
    {
      Map<Character, Integer> dct=new HashMap<Character, Integer>() ;
      dct.put('I', 1);
      dct.put('i', 1);
      dct.put('V', 5);
      dct.put('v', 5);
      dct.put('X', 10);
      dct.put('x', 10);
      dct.put('L', 50);
      dct.put('l', 50);
      dct.put('C', 100);
      dct.put('c', 100);
      dct.put('D', 500);
      dct.put('d', 500);
      dct.put('M', 1000);
      dct.put('m', 1000);
      int sum = 0, j;
      for(int i = 0; i < s.length(); ++i)
      {
          j = i+1;
          if(j < s.length() && dct.get(s.charAt(j)) > dct.get(s.charAt(i)))
          {
            sum += dct.get(s.charAt(j)) - dct.get(s.charAt(i));
            i = j;
          }
          else
            sum += dct.get(s.charAt(i));
      }
      return sum;
    }
}

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-08-30 12:46:39

[LeetCode][Java] Roman to Integer的相关文章

LeetCode 013 Roman to Integer

[题目] Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. [题意] 把罗马数转换为整数 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 大体思路是每个罗马字母对应的值相加即可, 但需要处理900, 400, 90, 40, 9, 4这几个特殊值(左

[LeetCode OJ] 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 class Solution { 2 public: 3 int romanToInt(string s) { 4 map<char,int> conversion; 5 conversion.insert(make_pair('

[LeetCode][Python]Roman to Integer

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/roman-to-integer/Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.===Comments by Daba

[LeetCode][JavaScript]Roman to Integer

Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. https://leetcode.com/problems/roman-to-integer/ 罗马数字转阿拉伯数字. 从后往前扫,如果当前的数大于之前的数,加上这个数,反之减去当前的数. 1 /** 2 * @param {string} s 3

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 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).按照下面的规则可以表示任意正整数. 重复数次:一个罗马数字重复几次,就表示这个数的几倍. 右加左减:在一个较大的罗马数

【leetcode】Roman to Integer

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 V X L C D M 阿拉伯数字 1 5 10 50 100 500 1000 计数规则: 相同的数字连写,所表示的数等于这些数字相加得到的数,例如:III = 3 小的数字在大的数字右边,所表示的数等于这些数字

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] 013. Roman to Integer (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 013.Roman_to_Integer (Easy) 链接: 题目:https://oj.leetcode.com/problems/roman-to-integer/ 代码(github):https://github.com/illuz/leetcode 题意: 把罗马数转为十进制. 分析: 跟 012. I