题目:

  Given a roman numeral, convert it to an integer.

  Input is guaranteed to be within the range from 1 to 3999.

思路:与12题正好相反,罗马数字基本字符集:I V X L C D M (1, 5, 10, 50, 100, 500, 1000)。思路是从字符串最低位开始匹配,累加相应字符对应的阿拉伯数字,要注意的就是I,X,L(1,10,100)要判断是在左还是在右,在左就减在右就加。判断方法是看累加值是否分别大于5,50,500

  1. public class Solution {
  2. public int romanToInt(String s) {
  3. char[] arr=s.toCharArray();
  4. int val=0;
  5. for(int i=arr.length-1;i>-1;i--){
  6. switch(arr[i]){
  7. case 'I':val+=val>=5?-1 : 1;
  8. break;
  9. case 'V':val+=5;
  10. break;
  11. case 'X':val+=10*(val>=50?-1:1);
  12. break;
  13. case 'L':val+=50;
  14. break;
  15. case 'C':val+=100*(val>=500?-1:1);
  16. break;
  17. case 'D':val+=500;
  18. break;
  19. case 'M':val+=1000;
  20. break;
  21. }
  22. }
  23. return val;

  

 

【LeetCode】13. Roman to Integer 罗马数字转整数的更多相关文章

  1. LeetCode 13 Roman to Integer(罗马数字转为整数)

    题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description   int toNumber(char ch) { switc ...

  2. [leetcode]13. Roman to Integer罗马数字转整数

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

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

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

  4. 【LeetCode】Roman to Integer(罗马数字转整数)

    这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...

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

  6. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  7. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  8. leetcode 13 Roman to Integer 罗马数组转整型

    描述: 将一个字符串表示的罗马数字转为整数,范围0~3999 解决: 如果后一个比前一个大,则表示减,没什么技巧. map<}, {}, {}, {}, {}, {}, {}}; int rom ...

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

随机推荐

  1. POM (Project Object Model)简介

    1  概念介绍 一个项目所有的配置都放置在 POM 文件中:定义项目的类型.名字,管理依赖关系,定制插件的行为等等.比如说,你可以配置 compiler 插件让它使用 java1.5 来编译. < ...

  2. Windows2012 cannot access netapp CIFS share

    NAS1> options cifs.smb2.signing.requiredcifs.smb2.signing.required off NAS1> options cifs.smb2 ...

  3. 【log】logback.xml

    <?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- 控制台 - ...

  4. Linux常用命令记录

    产用Linux命令 cat /proc/partitions //查看系统分区情况 fdisk -l /dev/sdb //查看磁盘物理存储 mount /dev/目录 /mnt/目录 //挂载文件 ...

  5. 网页爬虫--scrapy入门

    本篇从实际出发,展示如何用网页爬虫.并介绍一个流行的爬虫框架~ 1. 网页爬虫的过程 所谓网页爬虫,就是模拟浏览器的行为访问网站,从而获得网页信息的程序.正因为是程序,所以获得网页的速度可以轻易超过单 ...

  6. BestCoder Round #87 LCIS(dp)

    LCIS 要用dp的思路想这题 [题目链接]LCIS [题目类型]dp &题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的,比如(x,x+1,...,y−1 ...

  7. NeHe OpenGL教程 第十四课:图形字体

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  8. NeHe OpenGL教程 第七课:光照和键盘

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  9. 别去研究C++

    转载 YH,今天早晨起来.回想昨天,虽然吐槽了 C++ 的各种问题,但给别人打工,还是要靠 C++ 干活吃饭.我对待 C++ 的态度和云风不同,虽然他所说的 C++ 技术的事情我都懂都理解,而我感受到 ...

  10. React Native 开发。

    1.react-native run-android 安装 2.react-native start  开启调试端口