您好,登錄后才能下訂單哦!
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
羅馬數(shù)字與整數(shù)互轉(zhuǎn)
int getInt(char c){ int temp; switch(c){ case 'I':return 1; case 'V':return 5; case 'X':return 10; case 'L':return 50; case 'C':return 100; case 'D':return 500; case 'M':return 1000; } return 0; } int romanToInt(char* s) { int temp; int sum=getInt(s[0]); for(int i=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])); } } return sum; }
PS:一開始以為就是從左到右求和比較。。。。。。。。。。。。結(jié)果發(fā)現(xiàn)錯(cuò)誤了。做這道題首先搞明白羅馬數(shù)字規(guī)則。。。。。。。。。。。。。。?!,F(xiàn)在也不知道。挨個(gè)比較,左邊比右邊大的話直接加,左邊比右邊小的話先加再減兩倍。。。。。。。。。。。。。。。。。。。。。。。。
2、第一次在Leetcode額外定義函數(shù),要寫在已給函數(shù)的前面。。。。。。。。。。。。。。。。。。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。