Leetcode 13. Roman to Integer

  1. 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, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.

X can be placed before L (50) and C (100) to make 40 and 90.

C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer.

Example 1:

Input: s = "III"

Output: 3

Explanation: III = 3.

Example 2:

Input: s = "LVIII"

Output: 58

Explanation: L = 50, V= 5, III = 3.

Example 3:

Input: s = "MCMXCIV"

Output: 1994

Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

1 <= s.length <= 15

s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').

It is guaranteed that s is a valid roman numeral in the range [1, 3999].

解法1:

cpp 复制代码
class Solution {
public:
    int romanToInt(string s) {
        int n = s.size();
        int sum = 0;
        char c1 = 'Z', c2 = 'Z';
        for (int i = 0; i < n; i++) {
            c1 = s[i];
            if (i < n - 1) c2 = s[i + 1];
            switch(c1) {
                case 'I':
                    if (c2 == 'V' || c2 == 'X') sum -= 1;
                    else sum += 1;
                    break;
                case 'V':
                    sum += 5;
                    break;
                case 'X':
                    if (c2 == 'L' || c2 == 'C') sum -= 10;
                    else sum += 10;
                    break;
                case 'L':
                    sum += 50;
                    break;
                case 'C':
                    if (c2 == 'D' || c2 == 'M') sum -= 100;
                    else sum += 100;
                    break;
                case 'D':
                    sum += 500;
                    break;
                case 'M':
                    sum += 1000;
                    break;
                default:
                    break;
            }
        }
        return sum;
    }
};
相关推荐
橘颂TA8 分钟前
【剑斩OFFER】算法的暴力美学——Z字行变换
算法·leetcode·职场和发展·结构与算法
minji...14 分钟前
linux 进程控制(一) (fork进程创建,exit进程终止)
linux·运维·服务器·c++·git·算法
埃伊蟹黄面14 分钟前
双指针算法
数据结构·c++·算法
java修仙传15 分钟前
力扣hot100:反转链表
算法·leetcode·链表
Elias不吃糖17 分钟前
Leetcode-10.正则表达式匹配(暴力 或 记忆暴力)
数据结构·c++·算法·leetcode·深度优先
@小白鸽18 分钟前
1.3海量数据去重的Hash与BloomFilter
算法·哈希算法
小年糕是糕手19 分钟前
【C++】类和对象(四) -- 取地址运算符重载、构造函数plus
c语言·开发语言·数据结构·c++·算法·leetcode·蓝桥杯
sin_hielo20 分钟前
leetcode 3625
数据结构·算法·leetcode
不能只会打代码22 分钟前
力扣--3625. 统计梯形的数目 II 代码解析(Java,详解附注释附图)
算法·leetcode·职场和发展·力扣
练习时长一年22 分钟前
LeetCode热题100(岛屿数量)
算法·leetcode·职场和发展