• Leetcode:Roman to Integer


    Roman to Integer:

    题目:

    Given a roman numeral, convert it to an integer.

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

    解法(C++):

    class Solution {
    private:
    int mean(char c)
    {
    int data = 0;
    switch(c)
    {
    case 'I':data = 1;break;
    case 'V':data = 5;break;
    case 'X':data = 10;break;
    case 'L':data = 50;break;
    case 'C':data = 100;break;
    case 'D':data = 500;break;
    case 'M':data = 1000;break;
    }
    return data;
    }
    public:
    int romanToInt(string s) {
    int result = 0;
    int n = s.size();
    result += mean(s.at(0));
    for(int i = 1; i < n; i++)
    {
    int pre = mean(s.at(i - 1));
    int cur = mean(s.at(i));
    if(pre >= cur)
    {

    result += cur;
    }
    else
    {
    result += (cur - 2 * pre);
    }
    }
    return result;

    }
    };

    【注】switch一定要加break

  • 相关阅读:
    python06
    python05
    Python02
    pythonday01
    python04
    Mac下如何安装pip
    更改pip源至国内镜像,显著提升下载速度
    login登录加密
    虚拟机安装Centos6.5之后的网络配置
    git常用的问题
  • 原文地址:https://www.cnblogs.com/mj-selina/p/5027454.html
Copyright © 2020-2023  润新知