LeetCode第13题

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: C = 100, L = 50, XXX = 30 and III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

废话不多说,直接上代码,姿势一定要好看

class Solution {
private int[] num;
public int romanToInt(String s) {
//先将罗马字母转为数字
stringToNum(s);
//检查左小右大的情况
checkLeftDigit();
//求和
int sum = 0;
for(int l = 0;l<num.length;l++){
sum+=num[l];
}
return sum;
} public void stringToNum(String s){
num = new int[s.length()];
for(int i=0;i<s.length();i++){
switch(s.charAt(i)){
case 'I':
num[i]=1;
break;
case 'V':
num[i]=5;
break;
case 'X':
num[i]=10;
break;
case 'L':
num[i]=50;
break;
case 'C':
num[i]=100;
break;
case 'D':
num[i]=500;
break;
case 'M':
num[i]=1000;
break;
default:
num[i]=0;
break;
}
}
} public void checkLeftDigit(){
for(int j = 0;j<num.length;j++){
for(int k = j+1;k<num.length;k++){
if(num[j]==1 &&(num[k]==5 || num[k]==10)){
num[j] *= (-1);
}else if(num[j]==10 &&(num[k]==50 || num[k]==100)){
num[j] *= (-1);
}else if(num[j]==100 &&(num[k]==500 || num[k]==1000)){
num[j] *= (-1);
}
}
}
}
}

1.因为是手写,api会写错,String的长度是length()不是length;数组的长度是length不是size()

2.一开始我考虑会不会有IXC(109?91?89?)的情况,后来审题发现罗马字母正常情况左大右小,何况109是CIX,91是XCI,89是LXXXIX;所以不会有IXC的情况,就不用判断了

3.其实Char也能通过ASCII来判断大小和差值,不过最后还是要转成INT相加,所以我就先转成INT了

【LeetCode算法-13】Roman to Integer的更多相关文章

  1. 【算法】LeetCode算法题-Roman To Integer

    这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...

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

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

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

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

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

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

  6. LeetCode题解(13)--Roman to Integer

    https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...

  7. 【LeetCode】13. Roman to Integer 罗马数字转整数

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

  8. 【leetcode】13. Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...

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

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

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

随机推荐

  1. 瑞联科技:Pwp3框架 调用存储过程返还数据集合 到前端界面展示

    一:代码结构: 1:Js 代码结构 2:Java 代码结构 二:前端界面展示效果 为了数据安全性:界面数据做了处理 三:全端代码展示 1:main.vop <html> <head& ...

  2. jquery easyui datagrid 加每页合计和总合计

    jquery easyui datagrid 加每页合计和总合计 一:效果图 二:代码实现 这个只有从后台来处理 后台根据rows 和page两个参数返回的datatable 命名为dt 然后根据dt ...

  3. Oracle数据库的状态查询

    本文来源:huang_xw 的<Oracle数据库的状态查询> 1 状态查询 启动状态 SQL语句 结果 nomount select status from v$instance; ST ...

  4. Confluence 6 为登录失败配置使用验证码

    如果你具有 Confluence 管理员的权限,你可以限制 Confluence 登录失败的最大尝试次数.在给予最大登录失败尝试(默认为 3 次)次数后,Confluence 将会在用户进行再次尝试的 ...

  5. Confluence 6 数据库问题解除

    有关数据库相关的问题,请参考 Database Troubleshooting 中的内容. 希望获得更多的帮助,请参考 Troubleshooting Problems and Requesting ...

  6. 华为手机浏览器 onclick失灵的问题

    开发h5 遇到的问题是华为浏览器onclick 点击失灵. 下面这个网站是检查 浏览器是否支持es6语法的网站 http://ruanyf.github.io/es-checker/index.cn. ...

  7. kali 局域网嗅探

    1.局域网图片嗅探 工具  arpspoof arpspoof -i eth0 -t 192.1681.10(网卡 目标地址) 192.168.1.1 局域网网关,如果在Windows中可以使用局域网 ...

  8. java-HTML&javaSkcript&CSS&jQuery&ajax

    CSS  伪装 1.<style>a;link{color:#000000} a:visited{color:#000000; a.:hover{color:#FF00FF} a:acti ...

  9. java----重载

    重载: //同一个类中,方法名相同,参数列表不同[java就是靠不同的参数列表来寻找方法的],返回值可以任意,注意和函数的返回值类型相同.public class Demo { public stat ...

  10. SCSS 使用@each 方法循环遍历数组颜色并给li赋值

    $list-bg:red,orange,blue,skyblue; ul{ >li{ height: 30px; @each $c in $list-bg{ $i:index($list-bg, ...