【LeetCode013算法/编程练习C++】Roman to Integer //难得击败了80%的人

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

运行结果:

【LeetCode013算法/编程练习C++】Roman to Integer //难得击败了80%的人_第1张图片


难得一次击败了80%+的人,mark一下


祝刷题愉快~

【LeetCode013算法/编程练习C++】Roman to Integer //难得击败了80%的人_第2张图片


你可能感兴趣的:(LeetCode,-----数据结构-----,-----语言相关-----)