Roman numerals are represented by seven different symbols: IVXLCD 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 is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
class Solution {
public int romanToInt(String s) {
String sym = "MDCLXVI";
int[] val = {1000,500,100,50,10,5,1};
int num = 0;
int p; //array pointer
for(int i = 0; i < s.length(); i++){
p = sym.indexOf(s.charAt(i));
if(i+1 < s.length() && sym.indexOf(s.charAt(i+1)) < p){
num = num + val[sym.indexOf(s.charAt(i+1))] - val[p];
i++;
}
else{
num += val[p];
}
}
return num;
}
}

解题思路:要处理的一个特殊情况是4,9,40...这类数,所以我们要扫描到当前位之后的那一位,判断是不是这种情况,做相应处理。

13. Roman to Integer (JAVA)的更多相关文章

  1. 「Leetcode」13. Roman to Integer(Java)

    分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...

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

  3. LeetCode 13. Roman to Integer(c语言版)

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

  4. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  5. Leetcode 13. Roman to Integer(水)

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

  6. leetcode:Roman to Integer(罗马数字转化为罗马数字)

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

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

  8. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  9. 13. Roman to Integer【leetcode】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

随机推荐

  1. three.js使用base64 图片创建Texture纹理

    下面使用的是literallycanvas绘图,然后获取绘图结果为base64内容 var lc = LC.init( document.getElementById('canvas-output') ...

  2. webview之学习文章(待续)

    webview与js交互: Tencent/VasSonic(缓存优化方案) lzyzsd/JsBridge: pengwei1024/JsBridge: -----webview的框架 TheFin ...

  3. 禅知Pro 1.6 前台任意文件读取 | 代码审计

    禅知 Pro v1.6 前台任意文件读取 | 代码审计 蝉知专业版是基于蝉知企业门户系统开源版开发,继承了蝉知本身的优秀功能.相对于蝉知开源版增强了商品的属性自定义.属性价格定制.物流跟踪.微信支付. ...

  4. Mysql 分组选择

    Mysql 分组选择 在其他的数据库中我们遇到分组选择的问题时,比如在分组中计算前10名的平均分 我们可以使用row_number()over() 比较方便的得到. 但是在mysql中,问题就被抛了出 ...

  5. 5DAY高级权限

    5DAY高级权限 0xff001 suid\sgid\sticky ; s\s\t ; 4\2\1特殊位 001.描述 suid,sgid针对文件程序时,具备临时提升权限 sgid 针对目录时,该目录 ...

  6. 微信小程序-滚动Tab选项卡

    前言:今天呢 给大家详细讲解一下滚动Tab选项卡:左右可滑动切换的效果,希望对大家做项目时候有用! 以前也遇到过这个,但是没有做记录.转载来源于:https://www.jianshu.com/p/9 ...

  7. 实战ELK(8) 安装ElasticSearch中文分词器

    安装 方法1 - download pre-build package from here: https://github.com/medcl/elasticsearch-analysis-ik/re ...

  8. centos nginx配置https

    1.获取https证书: 用的阿里的免费证书: 参考:https://blog.csdn.net/chandoudeyuyi/article/details/71246255 2.修改nginx配置文 ...

  9. 使用 nodeJs 开发微信公众号(上传图片)

    在给用户发送消息中涉及到的素材(图片.视频.音频.文章等)需要事先传到微信服务器,然后获得媒体id(media_id),然后把 media_id 传递给用户 上传分上传临时素材(只保存三天)和上传永久 ...

  10. oss对象云存储

    import qiniu import uuidimport config def qn_upload_voice(fileData): '''上传语音到七牛云 @arg: fileData - 编码 ...