13.罗马数字转化为整型

Roman to Integer

问题描述:

Given a roman numeral, convert it to an integer.

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

知识补充:

学习unordered_map:

unordered_map和python里的字典很相似,可以实现key-value对应。可以通过key快速索引到value。unordered_map和map的区别是不会根据key的大小进行排序。

#include 
//引入unordered_map时有如下错误
//error This file requires compiler and library support for the ISO C++ 2011 standard.
//解决方法:工程上右键,选择build options,在compiler settings里面,选择c++0x支持。
unordered_map<char, int> T = { { 'I' , 1 }};//初始化key为char型,value为int型
T['W']=31;//插入数据
cout<'W']<//直接访问键对应的值,如果没有访问到,返回0
//找到key值为2的键值对
typedef unordered_map<int,int> mymap;
mymap T;
if( T.find(x)!=T.end() ){
        cout<<"get data where key=2! and data="<//遍历hash table
for( mymap::iterator iter=T.begin();iter!=T.end();iter++ ){
        cout<<"key="<first<<" and value="<second<

测试代码(c++):

int romanToInt(string roman) {
        int number = 0;
        int i = roman.size()-1;
        while(i>0)
        {
            if(roman[i]=='I')
            {
                number = number + 1;
            }else if(roman[i]=='V')
            {
                if(roman[i-1]=='I')
                {
                    number = number + 4;
                    i = i-2;
                    continue;
                }

                number = number + 5;
            }else if(roman[i]=='X')
            {
                if(roman[i-1]=='I')
                {
                    number = number + 9;
                    i = i-2;
                    continue;
                }
                number = number + 10;
            }else if(roman[i]=='L')
            {
                if(roman[i-1]=='X')
                {
                    number = number + 40;
                    i = i-2;
                    continue;
                }

                number = number + 50;
            }else if(roman[i]=='C')
            {
                if(roman[i-1]=='X')
                {
                    number = number + 90;
                    i = i-2;
                    continue;
                }

                number = number + 100;
            }else if(roman[i]=='D')
            {
                if(roman[i-1]=='C')
                {
                    number = number + 400;
                    i = i-2;
                    continue;
                }

                number = number + 500;
            }else
            {
                if(roman[i-1]=='C')
                {
                    number = number + 900;
                    i = i-2;
                    continue;
                }

                number = number + 1000;
            }

            --i;
        }
        if(i==0)
        {
            if(roman[0]=='I')
            {
                number += 1;
            }else if(roman[0]=='V')
            {
                number += 5;
            }else if(roman[0]=='X')
            {
                number += 10;
            }else if(roman[0]=='L')
            {
                number += 50;
            }else if(roman[0]=='C')
            {
                number += 100;
            }else if(roman[0]=='D')
            {
                number += 500;
            }else {
                number += 1000;
            }        
        }

        return number;
        }

性能:

13.罗马数字转化为整型_第1张图片

测试代码(Python):

def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        number = 0
        roman = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
        number = number + roman[s[-1:]]
        for i in range(len(s)-2,-1,-1):
            if(roman[s[i]]>=roman[s[i+1]]):
                number = number + roman[s[i]]
            else:
                number = number - roman[s[i]]


        return number

性能:

13.罗马数字转化为整型_第2张图片

参考代码(c++):

int romanToInt(string s) 
{
    unordered_map<char, int> T = { { 'I' , 1 },
                                   { 'V' , 5 },
                                   { 'X' , 10 },
                                   { 'L' , 50 },
                                   { 'C' , 100 },
                                   { 'D' , 500 },
                                   { 'M' , 1000 } };

   int sum = T[s.back()];
   for (int i = s.length() - 2; i >= 0; --i) 
   {
       if (T[s[i]] < T[s[i + 1]])
       {
           sum -= T[s[i]];
       }
       else
       {
           sum += T[s[i]];
       }
   }

   return sum;
}

性能:

13.罗马数字转化为整型_第3张图片

你可能感兴趣的:(LeetCode,C++)