首页 > 代码库 > Roman to Integer
Roman to Integer
问题:罗马数字变为整数
class Solution {public: int romanToInt(string s) { char c[10][10][10]={{"0","I","II","III","IV","V","VI","VII","VIII","IX"},{"0","X","XX","XXX","XL","L","LX" ,"LXX","LXXX","XC"},{"0","C","CC","CCC","CD","D", "DC","DCC","DCCC","CM"},{"0","M","MM","MMM"}}; int num=0,i; for(i=0;i<s.size();i++) { if(s[i]==‘I‘) num+=1; if(s[i]==‘V‘) { if(i!=0 && s[i-1]==‘I‘) num+=3; else num+=5; } if(s[i]==‘X‘) { if(i!=0 && s[i-1]==‘I‘) num+=8; else num+=10; } if(s[i]==‘L‘) { if(i!=0 && s[i-1]==‘X‘) num+=30; else num+=50; } if(s[i]==‘C‘) { if(i!=0 && s[i-1]==‘X‘) num+=80; else num+=100; } if(s[i]==‘D‘) { if(i!=0 && s[i-1]==‘C‘) num+=300; else num+=500; } if(s[i]==‘M‘) { if(i!=0 && s[i-1]==‘C‘) num+=800; else num+=1000; } } return num; }};
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。