`

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

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

    java入门 java_leetcode题解之012_Integer_to_Roman

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

    java入门 java_leetcode题解之013_Roman_to_Integer

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

    leetcode题库-LeetCode:力码

    leetcode题库 LeetCode 题解合集 本仓库展示了LeetCode题库...13 罗马数字转整数 Roman to Integer.cpp 15 三数之和 3Sum.cpp 最接近的三数之和 3Sum Closest .cpp 20 有效的括号 Valid Parentheses.cpp 22 括号生成 G

    _leetcode-python.pdf

    - Integer to Roman / Roman to Integer: 这两个问题分别要求将整数转换为罗马数字,或者将罗马数字转换为整数。 - Longest Common Prefix: 找出一个字符串数组中所有字符串的最长公共前缀。 - 3Sum / 3Sum Closest ...

    leetcode提交记录消失-leetcode-java:我对leetcode问题的解决方案

    leetcode提交记录消失解决leetcode问题 ...https://leetcode.com/problems/roman-to-integer/description/ first-submission-successful : yes 2018-05-16 : - id : 172 type : math difficulty : easy url : ...

    leetcode338-coding_notebook:编码_笔记本

    问题/数组和字符串/13.roman_to_integer.md) [26. Remove Duplicates from Sorted Array](Leetcode Problems/Array and String/26_remove_duplicates_from_sorted_array.md) [(雅虎)139。 Word Break](Leetcode ...

Global site tag (gtag.js) - Google Analytics