码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 面试经典150题——Day17


    文章目录

      • 一、题目
      • 二、题解

    一、题目

    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 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].

    题目来源: leetcode

    二、题解

    如果小值在大值前面,就减去该值,如果大值在小值前面,直接加上该值

    class Solution {
    public:
        int romanToInt(string s) {
            int n = s.length();
            unordered_map<char,int> map;
            map['I'] = 1;
            map['V'] = 5;
            map['X'] = 10;
            map['L'] = 50;
            map['C'] = 100;
            map['D'] = 500;
            map['M'] = 1000;
            int res = 0;
            for(int i = 0;i < n;i++){
                if(i < n - 1 && map[s[i]] < map[s[i+1]]) res -= map[s[i]];
                else res += map[s[i]];
            }
            return res;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    Linux友人帐之命令
    NXP官方开发板uboot程序烧写到SD卡并启动
    [论文评析]基于人体姿态识别的立定跳远 动作智能评估系统
    2.10 PE结构:重建重定位表结构
    某些之前的漏洞的遗忘的记录
    容灾备份 | 看我使用Powershell操作FTP进行数据文件自动上传备份
    【交叉熵损失torch.nn.CrossEntropyLoss详解-附代码实现】
    frps内网穿透
    Hexagon_V65_Programmers_Reference_Manual(34)
    ajax(Springmvc实现和注册提示效果)
  • 原文地址:https://blog.csdn.net/weixin_46841376/article/details/133855570
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号