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;
    }
};
相关推荐
Bruce_Liuxiaowei33 分钟前
驴滑块拼图游戏:从19世纪的纸片谜题到数学博弈论
人工智能·算法
一直C38 分钟前
Linux系统编程|信号进阶 + SystemV IPC(消息队列、共享内存)
linux·c语言·开发语言·算法·青少年编程·vim
鹿角片ljp2 小时前
LeetCode 141. 环形链表|从 HashSet 到快慢指针 O (1) 空间最优解
算法·leetcode·链表
薛定e的猫咪2 小时前
(NeurIPS 2022)GraphGPS:MPNN 与全局注意力的融合之道
人工智能·深度学习·学习·算法
AI服务老曹3 小时前
车牌识别算法接入AI视频分析平台的流程和误报优化
人工智能·算法·音视频
神威难绷泪3 小时前
Linux应用软件编程:线程分离属性 互斥机制 同步机制 死锁
linux·开发语言·算法·线程
小星星闪亮登场3 小时前
图论--最小生成树(内含二分图)
数据结构·算法·图论·迭代加深·图搜索算法
ZC跨境爬虫3 小时前
LeetCode 219. 存在重复元素 II(滑动窗口 + 哈希表详解)
算法·leetcode·散列表
吞下星星的少年·-·4 小时前
The 2025 ICPC Asia East Continent Online Contest (I) A题(模拟+贪心)
算法·贪心·模拟
小程序设计4 小时前
【机械设计】磁粉检测机器人的设计与验证
算法·机器人