`

LeetCode 13 - Roman to Integer

 
阅读更多

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

 

int romanToInt(string s) {
    unordered_map<char,int> map = {{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
    int num = 0;
    for(int i=0; i<s.size(); i++) {
        if(i<s.size()-1 && map[s[i]]<map[s[i+1]]) {
            num += map[s[i+1]] - map[s[i]];
            i++;
        } else {
            num += map[s[i]];
        }
    }
    return num;
}

 

分享到:
评论

相关推荐

    c语言-leetcode 0013-roman-to-integer.zip

    c c语言_leetcode 0013_roman_to_integer.zip

    C语言-leetcode题解之13-roman-to-integer.c

    本篇题解所涉及的文件名为“13-roman-to-integer.c”,这是C语言编写的源代码文件,专门针对LeetCode上编号为13的题目:“Roman to Integer”。这道题目要求编写一个函数,该函数接收一个表示罗马数字的字符串作为...

    java-leetcode题解之012-Integer-to-Roman

    java入门 java_leetcode题解之012_Integer_to_Roman

    C语言-leetcode题解之12-integer-to-roman.c

    C语言-leetcode题解之12-integer-to-roman.c的实现,是编程学习者在提升数据结构和算法能力,以及深入理解C语言特性方面的一个有益尝试。通过对该题目的学习和练习,可以有效增进对计算机科学基础概念的掌握,并对...

    js-leetcode题解之13-roman-to-integer.js

    1. 题目理解:LeetCode第13题要求编写一个函数,该函数能够将给定的罗马数字字符串转换为对应的整数。在解决这个问题之前,我们首先要熟悉罗马数字的基本构成和使用规则。罗马数字包括 I(1), V(5), X(10), L(50), C...

    java-leetcode题解之013-Roman-to-Integer

    java入门 java_leetcode题解之013_Roman_to_Integer

    js-leetcode题解之12-integer-to-roman.js

    在leetcode网站上,这个问题被标记为“Medium”难度,主要考察程序员对于数值到特定表示法转换的理解,以及对数组、字符串操作的熟悉度。解决这道题不仅需要了解罗马数字的基本规则,还应该具备将复杂问题分解为较小...

    LeetCode Roman to Integer解决方案

    LeetCode Roman to Integer解决方案

    Roman to Integer完整代码

    leetcode上Roman to Integer的完整C++代码,已被accepted

    leetcode中国-leetcode-local:leetcode本地

    12.integer-to-roman,自动创建文件夹和文件并使用vim打开文件(如下文件内容是自动生成的,根据剪贴板中的代码) 在 Solution 类中填写代码,保存退出 在终端输入leetcode commit ,即可将Solution类的代码复制到系统...

    qycl50224#leetcode#13. 罗马数字转整数 Roman to Integer1

    13. 罗马数字转整数 Roman to Integer用哈希存储映射字符---&gt;对映的值对字符串的字符挨个判断,考虑下一个字符如果下一个字符大于当前字符,su

    leetcode338-LeetCode:LeetCode刷题总结

    第 338 章力码 LeetCode刷题总结 ...13.Roman to Integer 14.Longest Common Prefix (Trie树待完成) 15.3Sum 16.3Sum Closest 17.Letter Combinations of a Phone Number 18.4Sum 19.Remove Nth Node From End

    gasstationleetcode-leetcode-rust:莱特代码休息

    13-roman-to-integer 14 cargo run --bin 14-longest-common-prefix 17 cargo run --bin 17-letter-combinations-of-a-phone-number 20 cargo run --bin 20-valid-parentheses 21 cargo run --bin 21-merge-two-...

    leetcode答案-LeetCode:Swift中的LeetCode

    Roman to Integer Easy #21 Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray ...

    leetcode中国-leetcode:leetcode刷题

    leetcode中国 我自己的leetcode刷题记录 ###[20150920] Valid Palindrome Implement strStr() String to Integer (atoi) addBinary longestPalindrome maximal rectangle :dp问题,较难 largestRectangleArea 求直方...

    leetcode卡-LeetCode:LeetCode题解

    leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) :star: :star: :star: 注意细节,溢出 ---- strlen :star: :star: :star: const char,size_t类型 ---- ...

    leetcode530-algorithm:算法

    leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** Leetcode 题目列表 005 Longest Palindromic Substring 006 ZigZag Conversion 007 Reverse Integer 008 ...

    leetcode跳跃-LeCode:乐科

    leetcode 跳跃 LeetCode Solved by Python easy/middle/hard:15/36/5 1. Two Sum 两数之和 2. Add Two Numbers 两数相加 ...Integer ...to Integer ...Integer to Roman ...13. Roman to Integer 罗马数字转

    leetcode-integer_to_roman

    标题 "leetcode-integer_to_roman" 指的是一个关于 LeetCode 上的编程挑战,该挑战要求将整数转换为罗马数字。这是一个典型的字符串处理和算法设计问题,常见于计算机科学和技术面试中,用于测试候选人的逻辑思维和...

    leetcode2-Algorithms-Practice:创建此repo是为了跟踪我在解决问题方面的进展

    Integer 运行时间:52 毫秒内存使用:14.1 MB 14. Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 ...

Global site tag (gtag.js) - Google Analytics