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. java.lang.RuntimeException: Canvas: trying to draw too large(203212800bytes) bitmap.

    https://www.cnblogs.com/spring87/p/7645625.html 今天我师父发现了一个问题:在更换登录页图片后,更新版本,部分手机打开会闪退.借了一个三星手机后,查看问题 ...

  2. tomcat7闪退

    问题是我昨天运行的好好的,今天加了些代码,tomcat7就会启动闪退.我把conf/server.xml中的<Context />去掉,tomcat又能正常启动! 那么问题出在哪里呢? 我 ...

  3. 关于弹性布局的 flex-grow的用法和flex-shrink的用法

    1.首先 flex-grow设置在子项目上 2.flex-grow默认值为0,如果为值1的时候就会撑满 3.flex-grow还可以给其中的一个子元素单独设置,设置为2,其它的则为1或者2都可以,具体 ...

  4. Swift 循环引用

    - 循环引用的weak用法 // ** {} 中所有 self 都是弱引用,注意需要解包        // 1.类似于 OC 中的 __weak typeof(self) weakSelf = se ...

  5. thinkphp 视图(三)系统变量——原生标签

    查看系统变量 dump($_SERVER); 在view中获取服务器变量 <p>{$Think.server.HTTP_HOST}</p> 获取env变量 status=dev ...

  6. LWIP学习

    转自:https://blog.csdn.net/kzq_qmi/article/details/46900589 数据包pbuf:    LwIP采用数据结构 pbuf 来描述数据包,其结构如下:  ...

  7. 基于UML的中职班主任工作管理系统的分析与设计--文献随笔(二)

    一.基本信息 标题:基于UML的中职班主任工作管理系统的分析与设计 时间:2016 出版源:遵义航天工业学校 关键字:中职学校; 班主任工作管理; UML建模 二.研究背景 问题定义:班主任是一项特殊 ...

  8. AutoCAD开发1---获取块属性

    Private Sub CommandButton1_Click() Dim pEntity As AcadObject Dim pBlock As AcadBlockReference Dim pP ...

  9. ScheduledExecutorService--目前最理想的定时任务实现方式

    ScheduledExecutorService 理想的定时任务实现方式 : 通过线程池的方式来执行任务的 可以灵活的设定第一次执行任务延迟时间 提供了良好的约定,以便设定定时执行的间隔时间代码实现: ...

  10. Python Day 5

    阅读目录: 数字类型: 字符串类型: 列表类型: 可变与不可变类型: ##数字类型: # 了了解:py2中小整数用int存放,大整数用long # 1.整型 num = -10000000000000 ...