每日leetcode--(13) Roman to Integer

我的想法:从给定字符串的第一个字符开始,判断给字符能否组成一个罗马数字,如果能,再加入这个字符之后的字符,构成字符串,再次判定它能否构成一个合法的罗马数字,以此类推,找出能构成的最长字符串,计算其对应的数字,然后再在原串中继续向后,直到结束。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;


string s1[] = {"??","I","II","III","IV","V","VI","VII","VIII","IX"};
string s10[] = {"??","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
string s100[] = {"??","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
string s1000[] = {"??","M","MM","MMM"};



vector<string> v1(s1,s1+10);
vector<string> v10(s10,s10+10);
vector<string> v100(s100,s100+10);
vector<string> v1000(s1000,s1000+4);

class Solution {
public:
    int romanToInt(string s) {
        // cout<<s<<endl;
        vector<vector<string > > V;
        V.push_back(v1);
        V.push_back(v10);
        V.push_back(v100);
        V.push_back(v1000);
        //cout<<V.size()<<endl;

        int ind = 0;
        string temp = "";
        vector<string> tv;
        int temp_num = 0;
        int res = 0;
        bool flag = false;
        int group = -1;
        while(ind<s.size())
        {
            temp.append(1,s[ind]);
            if(flag == false)
            {
                for(int i = 3;i>=0;i--)
                {
                    tv = V[i];
                    int diff = find(tv.begin(),tv.end(),temp) - tv.begin();
                    if(diff!=tv.size())
                    {
                        group = i;
                        temp_num = diff * (int)pow(10.0,i);
                        flag = true;
                        break;
                    }
                }
                ind ++;
            }
            else
            {
                int diff = find(tv.begin(),tv.end(),temp) - tv.begin();
                if(diff!=tv.size())
                {
                    temp_num = diff * (int)pow(10.0,group);
                    ind ++;
                }
                else
                {
                    res+=temp_num;
                    temp_num = 0;
                    temp = "";
                    flag = false;
                }
            }

        }
		res += temp_num;
        return res;
    }
};


int main()
{

    freopen("D:\\input.txt","r",stdin);
    //for(int i = 0;i<v1.size();i++)cout<<v1[i]<<endl;
    int total = 0;
    cin>>total;
    while(total--)
    {
        string roman = "";
        cin>>roman;
        //cout<<roman<<endl;
        Solution solu;
        cout<<solu.romanToInt(roman)<<endl;

    }

    return 0;
}

讨论中更快的O(n)解法:

// O(n) solution by others
#include <unordered_map>

class Solution {
public:
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;
}
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值