leetcode第13题--Roman to Integer

Problem:

Given a roman numeral, convert it to an integer.

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

遍历一次输入的字符串,如果不满足类似4或者9的就直接加相应的数,否则减去相应的数。其中对应如下”IVXLCDM“对应{1,5,10,50,100,500,1000}

class Solution {

public:
int romanToInt(string s)
{
    string refe = "IVXLCDM";
    int val[] = {1, 5, 10, 50, 100, 500, 1000};
    int result = 0;

    for (int i = 0; i < s.size(); i++)
    {
        for (int j = 0; j < refe.length(); j++)
        {
            if (i+1<s.size() && s[i] == refe[j] && ((j+1<refe.size() && s[i+1] == refe[j+1]) || (j+2<refe.size() && s[i+1] == refe[j+2])))
            {result -= val[j];}
            else if (s[i] == refe[j])
            {result += val[j];}
        }
    }
    return result;
}
};
时间: 2024-08-05 13:43:01

leetcode第13题--Roman to Integer的相关文章

leetcode_13题——Roman to Integer(string,数学问题)

Roman to Integer Total Accepted: 35099 Total Submissions: 100583My Submissions Question Solution Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. Hide Tags Math String Have you met this quest

LeetCode第[13]题(Java):Roman to Integer

题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve is

LeetCode之Easy篇 ——(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. 思路分析: 1.熟悉罗马数字的规则.见LeetCode之Easy篇 --(12)Integer to Roman 2.将输入的罗马数字转化成数组,并逐一通过case比对,然后根据其规则进行运算. Java代码示例: class Solution { public int romanT

leetcode第八题--String to Integer (atoi)

Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem

(atoi)Leetcode第八题_String to Integer (atoi)

String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended f

[leetcode]经典算法题- String to Integer (atoi)

题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for

[LeetCode][Python]Roman to Integer

# -*- coding: utf8 -*-'''__author__ = '[email protected]'https://oj.leetcode.com/problems/roman-to-integer/Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.===Comments by Daba

【leetcode刷题笔记】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解:基本的罗马字符和数字对应如下表所示: 罗马字符 数字 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表: 罗马字符 数字 IV 4 IX

【leetcode刷题笔记】Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字:否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字.利用一个HashMap存放罗马字符和数字的对应. 罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.