13. Roman to Integer //罗马数字转整数
- Total Accepted: 121305
- Total Submissions: 280158
- Difficulty: Easy
- Contributors: Admin
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
---------------------------------第一次觉得自己这次代码很简洁-------------------------------
时间复杂度O(n),参照12题(见下)逆向的Top Solution,得出规律——只要处理好CD,CM,XL,XC,IV,IX这几个之后其他的按单个的累加即可。
//String M[] = {"", "M", "MM", "MMM"};
//String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
//String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
//String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
if(i+1i+1
//本来以为去掉会提速的,结果并没有
class Solution {
public:
int romanToInt(string s) {
int result=0;
for(int i=0;
i
运行结果:
文章图片
难得一次击败了80%+的人,mark一下
祝刷题愉快~
文章图片
【LeetCode|【LeetCode013算法/编程练习C++】Roman to Integer //难得击败了80%的人】
推荐阅读
- 数据结构与算法|【算法】力扣第 266场周赛
- leetcode|今天开始记录自己的力扣之路
- Python|Python 每日一练 二分查找 搜索旋转排序数组 详解
- 【LeetCode】28.实现strstr() (KMP超详细讲解,sunday解法等五种方法,java实现)
- LeetCode-35-搜索插入位置-C语言
- leetcode python28.实现strStr()35. 搜索插入位置
- Leetcode Permutation I & II
- python|leetcode Longest Substring with At Most Two Distinct Characters 滑动窗口法
- LeetCode 28 Implement strStr() (C,C++,Java,Python)
- Python|Python Leetcode(665.非递减数列)