This question is not difficult, it is a tricky thing.
public class integerToRoman12 {
public String intToRoman(int< /span> num) {
StringBuilder sb = new StringBuilder();
int[] values = {1000,900, 500,400,100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] romans = {"M","CM","D","CD ","C","XC","L","XL","X","IX","V","IV","I"};
for(int i=0;i) { while(num>=values[i]) {
sb.append(romans[i]);
num-=values[i];
}
}
return sb.toString();
}
}
37ms,96.88%.
public span> class integerToRoman12 {
public String intToRoman(int num) {
StringBuilder sb = < span style="color: #0000ff">new StringBuilder();
int [] values = {1000,900, 500,400,100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] romans = {"M","CM","D","CD","C","XC","L","XL","X","IX","V" ,"IV","I"};
for(int< /span> i=0;i) { while(num>=values[i]) {
sb.append(romans[i]);
num-=values[i];
}
}
return sb.toString();
}
}