莽夫流,还是官方简单。
class Solution {
int cut(String curStr,String cuts[],HashMap hashMap,HashMap hashMap1){
int[] ints = new int[curStr.length()];
for (int k = 0; k < cuts.length; k++) {
for (int i = 0; i < curStr.length()-cuts[k].length()+1; i++) {
int flag = 1;
for (int j = 0; j < cuts[k].length(); j++) {
if(curStr.charAt(i+j)!=cuts[k].charAt(j)){
flag = 0;
break;
}
}
if(flag == 1){
ints[i] = 1;
}
}
}
int num = 0;
for (int i = 0; i < curStr.length(); i++) {
if(ints[i] == 0){
// System.out.println(curStr.charAt(i));
num += (int)hashMap1.get(curStr.charAt(i)+"");
}else{
num += (int)hashMap.get(curStr.charAt(i)+""+curStr.charAt(i+1)+"");
i++;
}
}
return num;
}
public int romanToInt(String s) {
HashMap<String,Integer> hashmap = new HashMap<>();
hashmap.put("IV",4);
hashmap.put("IX",9);
hashmap.put("XL",40);
hashmap.put("XC",90);
hashmap.put("CD",400);
hashmap.put("CM",900);
HashMap<String,Integer> hashmap1 = new HashMap<>();
hashmap1.put("I",1);
hashmap1.put("V",5);
hashmap1.put("X",10);
hashmap1.put("L",50);
hashmap1.put("C",100);
hashmap1.put("D",500);
hashmap1.put("M",1000);
String str[] = {"IV","IX","XL","XC","CD","CM"};
return cut(s,str,hashmap,hashmap1);
}
}
作者:你的雷哥
本文版权归作者所有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。