leetcode题解-13. Roman to Integer
来源:互联网 发布:查看mysql root密码 编辑:程序博客网 时间:2024/06/07 05:17
题意:给定一个字符串表示的罗马数字,把该罗马数字转化为一个整数。大小范围限制在 1 到 3999。
分析:该题与LeetCode 12 - Integer to Roman正好相反,需要将罗马数字转为整数。该题较为简单,采用discuss中的做法,计算每个符号并将其值相加,减去特殊情况的额外部分。另外吐槽一下12题和13题题意都不清楚,如果没有做过这种题大部分人都写不出来规则,但是明白规则以后又是简单的数字相加减,怪不得那么多人吐槽这种题。
class Solution { public int romanToInt(String s) { int sum=0; if(s.indexOf("IV")!=-1){sum-=2;} if(s.indexOf("IX")!=-1){sum-=2;} if(s.indexOf("XL")!=-1){sum-=20;} if(s.indexOf("XC")!=-1){sum-=20;} if(s.indexOf("CD")!=-1){sum-=200;} if(s.indexOf("CM")!=-1){sum-=200;} char c[]=s.toCharArray(); int count=0; for(;count<=s.length()-1;count++){ if(c[count]=='M') sum+=1000; if(c[count]=='D') sum+=500; if(c[count]=='C') sum+=100; if(c[count]=='L') sum+=50; if(c[count]=='X') sum+=10; if(c[count]=='V') sum+=5; if(c[count]=='I') sum+=1; } return sum; }}
阅读全文
0 0
- 13. Roman to Integer LeetCode题解
- leetcode题解-13. Roman to Integer
- LeetCode题解:Roman to Integer
- LeetCode题解:Integer to Roman
- LeetCode题解:Roman to Integer
- LeetCode题解:Integer to Roman
- LeetCode 13. Roman to Integer 题解(C++)
- LeetCode 题解(58): Integer to Roman
- LeetCode 题解(73): Roman to Integer
- leetcode题解||Integer to Roman问题
- leetcode题解 || Roman to Integer问题
- LeetCode题解——Roman to Integer
- LeetCode题解——Integer to Roman
- LeetCode 12 Integer to Roman题解
- LeetCode题解-12-Integer to Roman
- LeetCode题解-13-Roman to Integer
- 12. Integer to Roman LeetCode题解
- leetcode题解-12. Integer to Roman
- java集合总结
- 输入一行字符,分别统计出其中英文字母、数字、空格和其他字符的个数。
- redis单击安装-备忘录
- windows下压缩包安装配置apache
- 软件测试工程师必须懂得技能
- leetcode题解-13. Roman to Integer
- Android_启动页
- weblogic10.3.6 启动项目报 weblogic.descriptor.DescriptorException: VALIDATION PROBLEMS WERE FOUND
- 利用Github搭建Hexo博客
- 从零开始学习c++之一维、二维数组和vector的简单使用
- 2017秦皇岛现场赛H.Prime Set(奇偶二分图 匈牙利算法)
- ReactNative高级---JavaScript与Native之间的通信(二)
- 文章标题
- 清华大学视频:ARM微控制器与嵌入式系统