LeetCode 13 Roman to Integer (C,C++,Java,Python)

來源:互聯網
上載者:User

標籤:c   c++   java   leetcode   python   

Problem:

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

Solution:時間複雜度O(n)題目大意:與12題相反,給一個羅馬數字,要求轉化為十進位數字解題思路:
Java原始碼(用時749ms):
public class Solution {    public int romanToInt(String s) {        int index=0,num=0,temp=0;        while(index<s.length()){            char c=s.charAt(index++);            switch(c){                case 'I':num+=1;temp=1;break;                case 'V':num+=temp==1?3:5;break;                case 'X':num+=temp==1?8:10;temp=10;break;                case 'L':num+=temp==10?30:50;break;                case 'C':num+=temp==10?80:100;temp=100;break;                case 'D':num+=temp==100?300:500;break;                case 'M':num+=temp==100?800:1000;break;            }        }        return num;    }}
C語言原始碼(用時18ms):
int romanToInt(char* s) {    int num=0,temp=0;    while(*s){        switch(*s){            case 'I':num+=1;temp=1;break;            case 'V':num+=temp==1?3:5;break;            case 'X':num+=temp==1?8:10;temp=10;break;            case 'L':num+=temp==10?30:50;break;            case 'C':num+=temp==10?80:100;temp=100;break;            case 'D':num+=temp==100?300:500;break;            case 'M':num+=temp==100?800:1000;break;        }        s++;    }    return num;}

C++原始碼(用時58ms):

class Solution {public:    int romanToInt(string s) {        int index=0,num=0,temp=0;        while(index<s.size()){            char c=s[index++];            switch(c){                case 'I':num+=1;temp=1;break;                case 'V':num+=temp==1?3:5;break;                case 'X':num+=temp==1?8:10;temp=10;break;                case 'L':num+=temp==10?30:50;break;                case 'C':num+=temp==10?80:100;temp=100;break;                case 'D':num+=temp==100?300:500;break;                case 'M':num+=temp==100?800:1000;break;            }        }        return num;    }};

Python原始碼(用時138ms):
class Solution:    # @param {string} s    # @return {integer}    def romanToInt(self, s):        index=0;num=0;temp=0        while index<len(s):            c = s[index];index+=1            if c=='I':num+=1;temp=1            elif c=='V':num+=3 if temp==1 else 5            elif c=='X':num+=8 if temp==1 else 10;temp=10            elif c=='L':num+=30 if temp==10 else 50            elif c=='C':num+=80 if temp==10 else 100;temp=100            elif c=='D':num+=300 if temp==100 else 500            elif c=='M':num+=800 if temp==100 else 1000        return num


LeetCode 13 Roman to Integer (C,C++,Java,Python)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.