[LeetCode 9,13][简单]回文数/罗马数字转整数

9.回文数
题目链接

class Solution {
public:
    typedef long long LL;
    bool isPalindrome(int x) {
        LL x1=abs((LL)x),x2=0;
        while(x1){
            x2*=10;
            x2+=(x1%10);
            x1/=10;
        }
        if(x2==x)return true;
        else return false;
    }
};

13.罗马数字转整数
题目链接

class Solution {
public:
    char Seq[8]="IVXLCDM";
    int ind[8]={1,5,10,50,100,500,1000,-1};
    int sind[27];
    void init(){
        for(int i=0;i<7;i++){
            sind[Seq[i]-'A']=i;
        }
    }
    char str[100];
    int romanToInt(string s) {
        init();
        strcpy(str,s.c_str());
        int l=s.size(),ans=0;
        for(int i=0;i<l;i++){
            int f=1;
            if(i!=(l-1)){
                if(sind[str[i]-'A']<sind[str[i+1]-'A'])f=-1;
            }
            ans+=(f*ind[sind[str[i]-'A']]);
        } 
        return ans; 
    }
};
发布了73 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/IDrandom/article/details/103466453