Leetcode 13. Roman to Integer C语言
Givenaromannumeral,convertittoaninteger.Inputisguaranteedtobewithintherangefrom1to3999.
罗马数字与整数互转
intgetInt(charc){inttemp;switch(c){case'I':return1;case'V':return5;case'X':return10;case'L':return50;case'C':return100;case'D':return500;case'M':return1000;}return0;}intromanToInt(char*s){inttemp;intsum=getInt(s[0]);for(inti=1;i<strlen(s);i++){if(getInt(s[i-1])<getInt(s[i])){sum+=getInt(s[i]);sum-=2*getInt(s[i-1]);//printf("%d-->%d\n",getInt(s[i-1]),getInt(s[i]));}else{sum+=getInt(s[i]);//printf("{%d}",getInt(s[i]));}}returnsum;}
PS:一开始以为就是从左到右求和比较。。。。。。。。。。。。结果发现错误了。做这道题首先搞明白罗马数字规则。。。。。。。。。。。。。。。。现在也不知道。挨个比较,左边比右边大的话直接加,左边比右边小的话先加再减两倍。。。。。。。。。。。。。。。。。。。。。。。。
2、第一次在Leetcode额外定义函数,要写在已给函数的前面。。。。。。。。。。。。。。。。。。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。