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;
    }
};
相关推荐
手写码匠3 分钟前
手写 LLM 安全护栏:从内容审核到越狱防御的完整实现
人工智能·深度学习·算法·aigc
luj_176814 分钟前
草酸与烟酸对消化及糖代谢的影响解析
服务器·c语言·开发语言·经验分享·算法
青风9725 分钟前
16-ADAPTRACK:基于自适应阈值的多目标跟踪匹配算法
人工智能·算法·目标跟踪
汤姆yu44 分钟前
macOS系统下Aider完整安装、配置与实战使用教程
大数据·人工智能·算法·macos·github·copilot
Sam09271 小时前
【AI 算法精讲 14】TF-IDF:词频与逆文档频率
人工智能·python·算法·ai
AI科技星1 小时前
拓扑生命系统确定性理论:基于32维流形的遗传密码起源与衰老动力学( 中英双语顶刊终稿·标准数学符号)
开发语言·网络·人工智能·算法·机器学习·乖乖数学·全域数学
编程圈子1 小时前
电机驱动开发学习18. SVPWM空间矢量调制算法详解与实现
驱动开发·学习·算法
大鱼>2 小时前
机器学习基础:从零理解核心概念与算法分类
算法·机器学习·分类
AI科技星2 小时前
基于32维Cayley_Dickson超复数的全域拓扑统一场论——反重力、真空自持供能、维度瞬移与星际宇宙脑秩序体系
人工智能·学习·算法·机器学习·数据挖掘
aichitang20242 小时前
数论变换(NTT)
c++·算法·fft·ntt