题目描述:

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 question

预备知识:

记数方法

基本字符

I

V

X

L

C

D

M

相应的阿拉伯数字表示为

1

5

10

50

100

500

1000

1、相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;

2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;

3、小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;

4、正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)

5、在一个数的上面画一条横线,表示这个数扩大1000倍。 ##

思路:

一个罗马字符对应一个数字权,那么就可以用数据结构map

对于s中出现的每一个罗马字符从右到左(无低位高位这么一说),如果权值在变大,则权相加

如果变小了,则减去这个权值 ##

代码:

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int romanToInt(String s) {
        int result=0;
        Map<Character,Integer> roman = new HashMap<Character,Integer>();
        roman.put('I', 1);
        roman.put('V', 5);
        roman.put('X', 10);
        roman.put('L', 50);
        roman.put('C', 100);
        roman.put('D', 500);
        roman.put('M', 1000);
        for(int i=s.length()-1;i>=0;i--)
        {
            if(i==s.length()-1)
            {
                result=roman.get(s.charAt(i));
                continue;
            }
            if(roman.get(s.charAt(i)) >= roman.get(s.charAt(i+1)))
                result+=roman.get(s.charAt(i));
            else
                result-=roman.get(s.charAt(i));
        }    

        return result;
    }
}

leetcode之旅(10)-Roman to Integer的更多相关文章

  1. leetcode之旅(11)-Integer to Roman

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

  2. 【LeetCode算法-13】Roman to Integer

    LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...

  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. leetcode第13题--Roman to Integer

    Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  5. [LeetCode&Python] Problem 13. Roman to Integer

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  6. LeetCode记录之13——Roman to Integer

    能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...

  7. 【leetcode❤python】13. Roman to Integer

    #-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...

  8. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

  9. [LeetCode] Roman to Integer 罗马数字转化成整数

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

随机推荐

  1. 如何禁止App在后台运行以及如何保存和恢复App的状态

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 如果禁止App在后台运行 iOS上的App类似于Windows ...

  2. 06 获取Activity的栈管理器

    代码 <span style="font-size:18px;">package com.fmy.day8_29task.util; import java.util. ...

  3. MANIFEST.MF Error: No available bundle exports package

    Issue: When you imported some 3rd jars and compiled MANIFEST.MF, you may got following compling erro ...

  4. Dynamics CRM 给视图配置安全角色

    CRM2011后给表单设置了安全角色,可以配置实体表单给不同的安全角色查看,但视图的权限始终没有开放配置,这里介绍个工具可以实现这种配置. 先奉上2011/2013版本的工具地址(2015/2016见 ...

  5. ADFS3.0 Customizing the AD FS Sign-in Pages

    Windows Server2012R2自带的adfs是3.0的版本,不同于以前的版本的是3.0中登陆页面的定制化全部是通过powershell指令实现,官方的介绍链接如下:http://techne ...

  6. 03_Weblogic之配置简单域:启动和配置域,使用模板创建域,使用控制台

     1 域:概览 是Oracle Weblogic Server的基本管理单元 始终包含一个配置为管理服务器的Oracle WebLogic Server实例 域中可以包括一些称为受管服务器的Ora ...

  7. C++ Primer 有感(异常处理)(三)

    先看下面的代码: [cpp] view plaincopy int main() { int *i=new int(10); /* 这中间的代码出现异常 */ delete i; return 0; ...

  8. Linux0.11 创建进程的过程分析--fork函数的使用

    /* * linux/kernel/fork.c * * (C) 1991 Linus Torvalds */ /* 注意:signal.c和fork.c文件的编译选项内不能有vc变量优化选项/Og, ...

  9. 【翻译】在Sencha应用程序中使用插件和混入

    原文:Using Plugins and Mixins in Your Sencha Apps 概述 当扩展一个框架类的功能的时候,通常都会直接将新功能写入派生类,然而,如果所需的同一功能存在于多个组 ...

  10. Linux Shell 命令--cut

    解读-help 用法:cut [选项]... [文件]... 从每个文件中输出指定部分到标准输出. 长选项必须使用的参数对于短选项时也是必需使用的.   -b, --bytes=列表          ...