标签:
/* * 13.Roman to Integer * 2016-4-14 by Mingyang * 注意几个特殊点就好了 */ public static int romanToInt(String s) { int res=0; if(s==null||s.length()==0) return res; for(int i=0;i<s.length();i++){ char temp=s.charAt(i); if(temp==‘V‘){ res=res+5; }else if(temp==‘L‘){ res=res+50; }else if(temp==‘D‘){ res=res+500; }else if(temp==‘I‘){ if(i!=s.length()-1&&(s.charAt(i+1)==‘V‘||s.charAt(i+1)==‘X‘)){ res=res-1; }else{ res=res+1; } }else if(temp==‘X‘){ if(i!=s.length()-1&&(s.charAt(i+1)==‘L‘||s.charAt(i+1)==‘C‘)){ res=res-10; }else{ res=res+10; } }else if(temp==‘C‘){ if(i!=s.length()-1&&(s.charAt(i+1)==‘D‘||s.charAt(i+1)==‘M‘)){ res=res-100; }else{ res=res+100; } }else if(temp==‘M‘){ res=res+1000; } } return res; }
标签:
原文地址:http://www.cnblogs.com/zmyvszk/p/5394980.html