為了練習尋找實習時會碰到的問題,我用別人推薦的 LeetCode online judge。
希望能直接寫程式,不用事先編譯就能直接AC。
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
If you want to use (copy, paste or quote) my original article,
please contact me through email. (autek.roy@gmail.com)
If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
希望能直接寫程式,不用事先編譯就能直接AC。
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
編譯錯誤次數 Compile Error number: 4
嘗試次數 Try number: 8 (AC:1) (WA: 3) (CE: 4)
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: No
嘗試次數 Try number: 8 (AC:1) (WA: 3) (CE: 4)
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: No
點這裡看題目 Click here to see this Problem!
我對羅馬數字系統一點也不懂==
看著維基百科的解釋才把它寫出來~
我對羅馬數字系統一點也不懂==
看著維基百科的解釋才把它寫出來~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
//assume string s is a valid roman number | |
int romanToInt(string s) { | |
char roman[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'}; | |
int dig[7] = { 1, 5, 10, 50, 100, 500, 1000}; | |
int len = s.size(); | |
int num = 0, pre = 0, val; | |
for(int i = len - 1; i >= 0; i--){ | |
for(int j = 0; j < 7; j++){ | |
if(roman[j] == s[i]){ | |
val = dig[j]; | |
break; | |
} | |
} | |
//雖然不能連續加超過三位,但是預設s是合法羅馬數字 | |
if(pre == 0 || val >= pre){ | |
pre = val; | |
num += val; | |
} | |
else if(val < pre){ | |
pre = 0;//左減只有一位, 所以只要一減過之後就可以準備開始加 | |
num -= val; | |
} | |
} | |
return num; | |
} | |
}; |
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!