leetcode:Roman to Integer

注意优先看9**和4**


public class Solution {

    public int romanToInt(String s) {
        int rst=0;
        int i,n;
        char[] ca=s.toCharArray();
        n=s.length();
        for(i=0;i<n;i++)
        {
        if(ca[i]=='M')
        rst+=1000;
        else if(i+1<n && ca[i]=='C' && ca[i+1]=='M')
        {rst+=900;i++;}
        else if(i+1<n && ca[i]=='C' && ca[i+1]=='D')
        {rst+=400;i++;}
        else if(ca[i]=='D')
        rst+=500;
        else if(ca[i]=='C')
        rst+=100;
        else if(i+1<n && ca[i]=='X' && ca[i+1]=='C')
    {rst+=90;i++;}
        else if(i+1<n && ca[i]=='X' && ca[i+1]=='L')
    {rst+=40;i++;}
        else if(ca[i]=='L')
        rst+=50;
        else if(ca[i]=='X')
        rst+=10;
        else if(i+1<n && ca[i]=='I' && ca[i+1]=='X')
{rst+=9;i++;}
        else if(i+1<n && ca[i]=='I' && ca[i+1]=='V')
{rst+=4;i++;}
        else if(ca[i]=='V')
        rst+=5;
        else if(ca[i]=='I')
        rst+=1;
        }
        
        
return rst;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值