zoukankan      html  css  js  c++  java
  • [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.

     1 int c2n(char c){
     2     switch (c){
     3         case 'I': return 1; 
     4         case 'V': return 5;  
     5         case 'X': return 10;  
     6         case 'L': return 50;  
     7         case 'C': return 100;  
     8         case 'D': return 500;  
     9         case 'M': return 1000;  
    10         default: return 0;  
    11     }  
    12 }
    13 
    14 int romanToInt(char* s) {
    15     int result = 0;
    16     for(int i = 0; i < strlen(s); i++){
    17         if ( i>0 && (c2n(s[i]) > c2n(s[i-1]))){
    18             result += (c2n(s[i]) - 2 * c2n(s[i-1]));
    19         }else{
    20             result += c2n(s[i]);   
    21         }
    22     }
    23     
    24     return result;
    25 }
  • 相关阅读:
    微服务
    JNDI Tomcat
    JNDI
    依赖倒置原则
    mac下为gdb创建证书赋权其调试其它应用
    sed相关
    关于autoconf
    mac相关
    about gnu bash shell
    关于gcc
  • 原文地址:https://www.cnblogs.com/amadis/p/5926477.html
Copyright © 2011-2022 走看看