Leetcode12 Integer to Roman

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 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();
}
}

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3966 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.