标签:
题目描述:public class Solution { public int RomanToInt(string s) { if(string.IsNullOrEmpty(s)) { return 0; } var len = s.Length; var result = Convert(s[0]); if(len < 2){ return result; } for(var i = 1;i < len; i++){ var pre = Convert(s[i - 1]); var current = Convert(s[i]); if(current <= pre){ result += current; }else{ result = result - 2 * pre + current; } } return result; } private int Convert(char c) { switch (c) { case ‘I‘: return 1; case ‘V‘: return 5; case ‘X‘: return 10; case ‘L‘: return 50; case ‘C‘: return 100; case ‘D‘: return 500; case ‘M‘: return 1000; default : throw new ArgumentException(); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/lan_liang/article/details/49704123