13. Roman to Integer

  • Total Accepted: 95998
  • Total Submissions: 234087
  • Difficulty: Easy

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

  思路:

  根据上一篇的关于罗马数字的解释,我们可以知道,在罗马数字中左减右加的原则,所以当较小的数在较大的数前面时,表示是相减的,所以此题就显得很简单了

 public int romanToInt(String s) {

     char [] arr = s.trim().toCharArray() ;
int res = toNumber(arr[0]) ;
for(int i = 1 ; i < arr.length ; i++){
if(toNumber(arr[i-1]) < toNumber(arr[i])){
res += toNumber(arr[i]) - 2*toNumber(arr[i-1]) ;
}else{
res += toNumber(arr[i]) ;
}
}
return res ; } private int toNumber(char ch) {
switch (ch) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
return 0;
}

LeetCode--No.013 Roman to Integer的更多相关文章

  1. 【LeetCode】013. Roman to Integer

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

  2. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  3. No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

  4. 【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 ...

  5. 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  ...

  6. 【JAVA、C++】LeetCode 013 Roman to Integer

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

  7. [Leetcode]013. Roman to Integer

    public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; ...

  8. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  9. LeetCode:12. Roman to Integer (Easy)

    1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...

随机推荐

  1. elasticsearch template

    # curl -XPUT localhost:9200/_template/template_1 -d '{"template" : "te*","s ...

  2. mybatis-generator 覆盖新增XML

    参考文章:https://www.cnblogs.com/xxoome/p/10068780.html 1.添加依赖(版本1.3.7) plugin> <groupId>org.my ...

  3. Pandas合并数据集之concat、combine_first方法

    轴向连接(concat) Numpy import numpy as np import pandas as pd from pandas import Series arr = np.arange( ...

  4. Linux vfpd锁定用户目录

    在linux ftp配置中,为了防止用户cd 到其他目录,需要锁定用户的根目录. Step1:修改配置文件 [root@ess ~]# vi /etc/vsftpd/vsftpd.conf #chro ...

  5. Python local error

    原来在python的函数中和全局同名的变量,如果你有修改变量的值就会变成局部变量,在修改之前对该变量的引用自然就会出现没定义这样的错误了,如果确定要引用全局变量,并且要对它修改,必须加上global关 ...

  6. react官方脚手架搭建项目

    1.全局安装 npm install -g create-react-app 2. app后面还要给项目文件命名 create-react-app //是全局命令来创建react项目 3.然后按照提示 ...

  7. .net core 中使用ef 访问mysql

    1.参考文档说修改项目文件添加,就得这么做,不然会报错 <ItemGroup> <DotNetCliToolReference Include="Microsoft.Ent ...

  8. eclipse配置servlet错误

    可能是因为你的web.xml里的<url>映射的名字和servlet相同

  9. Codeforces Round #539 (Div. 2) 异或 + dp

    https://codeforces.com/contest/1113/problem/C 题意 一个n个数字的数组a[],求有多少对l,r满足\(sum[l,mid]=sum[mid+1,r]\), ...

  10. Python开发——6.文件操作

    一.文件操作 1.文件操作的处理流程 打开文件得到文件句柄并赋值给一个变量====>通过句柄对文件进行分析====>关闭文件 #1. 打开文件,得到文件句柄并赋值给一个变量 f=open( ...