羅馬數字轉整數

<h4 style="box-sizing: border-box; font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-weight: 500; line-height: 1.1; color: rgb(113, 113, 113); margin-top: 10px; margin-bottom: 10px; font-size: 18px;"><span class="m-l-sm m-t-sm" style="box-sizing: border-box; margin-left: 10px; margin-top: 10px;">羅馬數字轉整數</span></h4><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">給定一個羅馬數字,將其轉換成整數。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;">返回的結果要求在1到3999的範圍內。</p><div class="line line-dashed" style="box-sizing: border-box; height: 2px; margin: 10px 0px; font-size: 0px; overflow: hidden; border-width: 1px 0px 0px; border-style: dashed; border-top-color: rgb(234, 237, 239); color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"></div><span class="text-sm" style="box-sizing: border-box; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;">您在真實的面試中是否遇到過這個題?</span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"> </span><div class="btn-group" data-toggle="buttons" style="box-sizing: border-box; position: relative; display: inline-block; vertical-align: middle; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><div class="btn btn-white btn-xs" id="yes" style="box-sizing: border-box; display: inline-block; padding: 1px 5px; margin-bottom: 0px; font-size: 12px; line-height: 1.5; text-align: center; white-space: nowrap; vertical-align: middle; cursor: pointer; -webkit-user-select: none; border: 1px solid rgba(150, 160, 180, 0.298039); border-radius: 3px; box-shadow: rgba(0, 0, 0, 0.0470588) 0px -1px 1px inset; position: relative; float: left; margin-left: 0px; background-image: none; background-clip: padding-box;">Yes</div></div><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><span style="color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"></span><div class="m-t-lg m-b-lg" style="box-sizing: border-box; margin-top: 20px; margin-bottom: 20px; color: rgb(113, 113, 113); font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><span style="box-sizing: border-box;">樣例</span><div class="m-t-sm" style="box-sizing: border-box; margin-top: 10px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">IV</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">4</code></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">XII</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">12</code></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">XXI</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">21</code></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;"><code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">XCIX</code> -> <code style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12.6px; padding: 2px 4px; color: rgb(199, 37, 78); white-space: nowrap; border-radius: 4px; background-color: rgb(249, 242, 244);">99</code></p></div></div>

int romanToInt(string& s) {
        // Write your code here
        if(s.length() == 0) {
            return 0;
        }
        unordered_map<char, int> m;
        m['I'] = 1;
	    m['V'] = 5;
	    m['X'] = 10;
	    m['L'] = 50;
	    m['C'] = 100;
	    m['D'] = 500;
	    m['M'] = 1000;
	    
        int num = m[s.at(s.length() - 1)];
        for(int i = s.length()-2; i >= 0; i--){
            if(m[s.at(i)] >= m[s.at(i+1)]) {
                num += m[s.at(i)];
            }
            else {
                num -= m[s.at(i)];
            }
        }
        return num;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章