Leetcode每日一题_13
public int romanToInt(String s) {
        
if(s.length()==0||s==null)return 0;
int res=Toint(s.charAt(0));
for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;<&nbsp;s.length();&nbsp;i++)&nbsp;{
if&nbsp;(Toint(s.charAt(i))>Toint(s.charAt(i-1)))&nbsp;{
res+=Toint(s.charAt(i))-2*Toint(s.charAt(i-1));
}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;{
res=res+Toint(s.charAt(i));
}
}
return&nbsp;res;
}
private&nbsp;static&nbsp;int&nbsp;Toint(char&nbsp;c)&nbsp;{
int&nbsp;res=0;
switch&nbsp;(c)&nbsp;{
case&nbsp;'I':
return&nbsp;1;
case&nbsp;'V':
return&nbsp;5;
case&nbsp;'X':
return&nbsp;10;
case&nbsp;'L':
return&nbsp;50;
case&nbsp;'C':
return&nbsp;100;
case&nbsp;'D':
return&nbsp;500;
case&nbsp;'M':
return&nbsp;1000;
}
return&nbsp;res;
}
全部评论

相关推荐

点赞 评论 收藏
分享
06-11 13:34
门头沟学院 C++
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务