LeetCode //13. Roman to Integer

这篇具有很好参考价值的文章主要介绍了LeetCode //13. Roman to Integer。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

From: LeetCode
Link: 13. Roman to Integer
文章来源地址https://www.toymoban.com/news/detail-594763.html


Solution:

Ideas:
This program first defines a helper function value() to convert a single Roman numeral character to its integer value. Then, in the romanToInt() function, it processes the input string one character at a time. If it encounters a case where a smaller numeral appears before a larger one (which is the subtractive case), it subtracts the value of the smaller numeral. Otherwise, it adds the value of the numeral. Finally, the main() function gets the input from the user and calls the romanToInt() function to convert the input to an integer.
Code:
int value(char roman) {
    switch (roman) {
        case 'I': return 1;
        case 'V': return 5;
        case 'X': return 10;
        case 'L': return 50;
        case 'C': return 100;
        case 'D': return 500;
        case 'M': return 1000;
        default: return 0;
    }
}

int romanToInt(char *s) {
    int total = 0;
    while(*s) {
        if(*s == '\0') {
            break;
        }

        // If this is the subtractive case.
        if(value(*s) < value(*(s+1))) {
            total -= value(*s);
        } else {
            total += value(*s);
        }

        // Move on to the next character.
        s++;
    }

    return total;
}

到了这里,关于LeetCode //13. Roman to Integer的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • int[]数组转Integer[]、List、Map「结合leetcode:第414题 第三大的数、第169题 多数元素 介绍」

    输出: 众所周知,将普通数组转为List集合,可以通过JDK提供的诸多方法来减轻我们的编码负担,所以接下来小名借用两个leetcode题中的场景来分享下数组转集合的使用方法: 看到开头的 「int[ ]转Integer[ ]」 可能有的小伙伴并不知道什么情况会用。当然平日开发我们断然不会

    2024年02月14日
    浏览(37)
  • 【LeetCode 算法】Minimum Operations to Halve Array Sum 将数组和减半的最少操作次数-Greedy

    给你一个正整数数组 nums 。每一次操作中,你可以从 nums 中选择 任意 一个数并将它 减小 到 恰好 一半 。(注意,在后续操作中你可以对减半过的数继续执行操作) 请你返回将 nums 数组和 至少 减少一半 的 最少 操作数。 1 = n u m s . l e n g t h = 1 0 5 1 = n u m s [ i ] = 1 0 7 1 = num

    2024年02月15日
    浏览(36)
  • 罗马数字转整数——leetcode.13

    引言: 本篇博客属于题解篇,是我在刷力扣的题后给大家总结了一下解题思路以及代码实现,并在后面分析了map与unordered_map的区别。我也会分享我的做题思路,一起来看看吧。 题目: 罗马数字包含以下七种字符:  I ,  V ,  X ,  L , C , D  和  M 。 例如, 罗马数字 

    2024年04月13日
    浏览(24)
  • python LeetCode 刷题记录 13

    题号:13 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1 。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特

    2024年02月08日
    浏览(27)
  • 代码训练LeetCode(13)颠倒二进制位

    代码训练(13)LeetCode之颠倒二进制位 Author: Once Day Date: 2024年4月9日 漫漫长路,才刚刚开始… 全系列文章可参考专栏: 十年代码训练_Once-Day的博客-CSDN博客 参考文章: 190. 颠倒二进制位 - 力扣(LeetCode) 力扣 (LeetCode) 全球极客挚爱的技术成长平台 1. 原题 颠倒给定的 32 位无符号整

    2024年04月27日
    浏览(32)
  • 【LeetCode】数据结构题解(13)[设计循环链表]

    所属专栏:玩转数据结构题型❤️ 🚀 博主首页:初阳785❤️ 🚀 代码托管:chuyang785❤️ 🚀 感谢大家的支持,您的点赞和关注是对我最大的支持!!!❤️ 🚀 博主也会更加的努力,创作出更优质的博文!!❤️ 🚀 关注我,关注我,关注我,重要的事情说三遍!!!!!

    2024年02月13日
    浏览(42)
  • leetcode13题罗马数字转成整数

    2024年02月20日
    浏览(28)
  • LeetCode 每日一题 2023/8/7-2023/8/13

    记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步 8/7 344. 反转字符串 双指针 8/8 1749. 任意子数组和的绝对值的最大值 记录最小值 最大值 8/9 1281. 整数的各位积和之差 按要求计算 8/10 1289. 下降路径最小和 II 从上到下遍历每一行 在每一

    2024年02月13日
    浏览(43)
  • 2023-08-13 LeetCode每日一题(合并两个有序数组)

    点击跳转到题目位置 给你两个按 非递减顺序 排列的整数数组 nums1 和 nums2,另有两个整数 m 和 n ,分别表示 nums1 和 nums2 中的元素数目。 请你 合并 nums2 到 nums1 中,使合并后的数组同样按 非递减顺序 排列。 **注意:**最终,合并后数组不应由函数返回,而是存储在数组 num

    2024年02月13日
    浏览(47)
  • Leetcode array 704 27 189 121 380 238 134 13

    二分搜索: 判断条件是left 和 right 这里就要注意区间开闭的问题 因为需要运行时间时O(1),所以要加上map 删除的时候要把需要删除的变到最后,再直接pop掉 rand用法: 1.rand() 2. nums[rand() % nums.size()]; 3. left+rand() % (right-left)

    2024年02月09日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包