Leetcode 13. Roman to Integer

这篇具有很好参考价值的文章主要介绍了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:文章来源地址https://www.toymoban.com/news/detail-714427.html

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;
    }
};

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

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

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

相关文章

  • java.lang.String cannot be cast to java.lang.Integer异常

    在java中无法直接将String类型强制转换为Integer类型。 Java中的String和Integer是两种不同的数据类型,它们之间不能直接进行强制类型转换。这主要是因为它们在底层的表示方式和数据结构上有很大的差别。 String是一个不可变的字符序列,用于表示文本数据。它是通过字符数组来

    2024年02月05日
    浏览(34)
  • RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    使用yolov5l模型训练时出现报错,但是昨天使用yolov5s模型时是可以正常训练的。 发生报错的原因是gpu内存占用过高,terminal输入nvidia-smi查看gpu的使用情况。   我们需要把bach_size调小,一般建议是8的倍数,内存不够用时尽量调低,此处我设置成了16。 结果运行正常。 使用yol

    2024年02月11日
    浏览(31)
  • LeetCode 2441. Largest Positive Integer That Exists With Its Negative【哈希集合】简单

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月05日
    浏览(32)
  • 解决Failed to convert value of type ‘java.lang.String‘ to required type ‘java.lang.Integer

    项目:网上商城练习 问题:使用postman测试接口报错:类型转换异常 上代码: 改为: 直接去掉{}和@PathVariable注释,容易找不到对应的参数类型,希望对大家有用,问题已解决。

    2024年02月11日
    浏览(44)
  • Algorithms practice:leetcode 33. Search in Rotated Sorted Array

    Algorithms practice:leetcode33 Search in Rotated Sorted Array There is an integer array ,nums , sorted in ascending order (with distinct values). Prior to being passed to your function, nums is possibly rotated,at an unknown pivot index k (1 = k nums.length) such that the resulting array is [nums[k], nums[k+1], …, nums[n-1], nums[0], nums[1], …, nums

    2024年01月21日
    浏览(36)
  • TypeError: only integer scalar arrays can be converted to a scalar index

    报错信息: 类型错误,只有整型标量数组才能转换成标量索引,但一般问题都不在于你的索引是不是整数。这个报错一般会出现在你想使用一个索引列表去索引另一个列表,即诸如list[index_list]的形式,此时就会出现此报错,因为 index_list 为 List列表类型,不被允许;如果是数

    2024年02月11日
    浏览(56)
  • 报错信息Failed to convert value of type ‘java.lang.String‘ to required type ‘java.lang.Integer‘

    2.1 从前端查看接口 根据报错信息它的信息大概是前台给我传了一个string类型的listAllTag不能转换成Integer,我看了半天也没能想到为什么他会传给我一个String的字符串因为这个接口就是简单的获取一个list集合返回,很棒前台接口也是报500。 2.2查看后端接口 就把重点放在了Contro

    2024年02月11日
    浏览(83)
  • 【bug解决】RuntimeError: Unable to find a valid cuDNN algorithm to run convolution

    进行深度学习的算法模型训练的时候,终端报错: 产生报错的原因可能有两种: 1.模型训练的环境中cudnn,CUDA的版本号不匹配 解决办法:安装对应的cudnn,以及cuda,找到对应的torch框架,进行安装 2.其实问题更加简单,是模型的训练的batch-size训练过大了,调整更小,就可以了

    2024年02月11日
    浏览(40)
  • int[]数组转Integer[]、List、Map「结合leetcode:第414题 第三大的数、第169题 多数元素 介绍」

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

    2024年02月14日
    浏览(37)
  • 已解决java.lang.String cannot be cast to java.lang.Integer异常的正确解决方法,亲测有效!!!

    已解决java.lang.String cannot be cast to java.lang.Integer异常的正确解决方法,亲测有效!!! 开发中经常会遇到java.lang.String cannot be cast to java.lang.Integer异常,记录下我怎么解决的。 这个错误是因为你试图将一个字符串对象转换为整数对象,但是类型不匹配。 下滑查看解决方法 在

    2023年04月08日
    浏览(62)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包