1. 程式人生 > >每日leetcode--(13) Roman to Integer

每日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;
}
};

相關推薦

每日leetcode--(13) Roman to Integer

我的想法:從給定字串的第一個字元開始,判斷給字元能否組成一個羅馬數字,如果能,再加入這個字元之後的字元,構成字串,再次判定它能否構成一個合法的羅馬數字,以此類推,找出能構成的最長字串,計算其對應的數字,然後再在原串中繼續向後,直到結束。 #include <i

LeetCode 13. Roman to Integer

cnblogs 如果 mes std line logs urn onclick += https://leetcode.com/problems/roman-to-integer/#/description Given a roman numeral, convert

Leetcode:13- Roman to Integer

字典 nbsp __name__ clas obj name 記錄 pos range 題意:輸入一個羅馬數字,把它轉化成對應整數 1 class Solution(object): 2 def romanToInt(self,s): 3 d

leetcode-13. Roman to Integer

www ant sub 個數字 pre n-2 div toc blog 思路:首先是本弱雞的弱雞思路1、將所有的組合放入Map中,key值代表羅馬數字,value代表羅馬數字對應的整數值2、每次取兩個值進行判斷,如果匹配直接跳兩個繼續匹配,否則取一個進行匹配ps:這樣做的

#Leetcode# 13. Roman to Integer

https://leetcode.com/problems/roman-to-integer/description/   Roman numerals are represented by seven different symbols: I, V, X,&nbs

LeetCode 13.Roman to Integer (羅馬數字轉整數)

題目描述: 羅馬數字包含以下七種字元:I, V, X, L,C,D ,M。 符 數值 I 1 V 5 X 10 L 50 C 100

Leetcode 13 Roman to Integer

Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value I 1 V 5 X

LeetCode--13. Roman to Integer & 12. Integer to Roman

題目連結:https://leetcode.com/problems/roman-to-integer/和https://leetcode.com/problems/integer-to-roman/ 這兩道姊妹題是十分經典有趣的字串題目。 第一題: 要求我們將羅馬數字轉為整數,這個下面

leetcode 13: Roman to Integer

題目: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V

[LeetCode][13]Roman to Integer解析 羅馬字元轉int型別關於棧的常數實現-Java實現

Q: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr

leetcode 13 Roman to Integer (羅馬數轉換成整數)

題目要求 將羅馬數字轉換為整數,其中羅馬數字有7種“Ⅰ,V, X, L, C, D, M”,含義屬下表所示: Ⅱ: 表示兩個Ⅰ相加(1+1=2) XⅡ: 表示X 和Ⅱ相加(10+2 = 12) 需要注意的是!!! 羅馬數字在書寫時遵循著從左到右是數值遞增(就是右邊的數會比左邊的大,

[LeetCode]13. Roman to Integer羅馬數字轉整數

instead placed man for sym () 兩種 字符 together Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol

[LeetCode]13. Roman to Integer(羅馬數字轉化為整數)

13. Roman to Integer 點選檢視相關題 整數轉化為羅馬數字 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the ran

LeetCode 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. 方法:理解羅馬數字。 public class S

LeetCode 13 Roman to Integer 解題報告

字典 代碼 app value ges python代碼 int same man 題目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol

[LeetCode&Python] Problem 13. Roman to Integer

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I

LeetCode13. Roman to Integer - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Roman numerals are represented by seven different symbols: I, V, X, L, C

C# 寫 LeetCode easy #13 Roman to Integer

13、Roman to Integer  Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb

13. (LeetCode-java)Roman to Integer

題目: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V

leetcode13. Roman to Integer(C)

題目連結 提交程式碼: int romanToInt(char* s) { int i,p = 0,numslen=0; int sum = 0; int* nums = (int*)malloc(30 * sizeof(int)); whil