?LeetCode刷題實(shí)戰(zhàn)12: 整數(shù)轉(zhuǎn)羅馬數(shù)字
算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !
今天和大家聊的問(wèn)題叫做整數(shù)轉(zhuǎn)羅馬數(shù)字?,我們先來(lái)看題面:
https://leetcode-cn.com/problems/integer-to-roman/

題意

樣例
示例?1:
輸入:?3
輸出:?"III"
示例?2:
輸入:?4
輸出:?"IV"
示例?3:
輸入:?9
輸出:?"IX"
示例?4:
輸入:?58
輸出:?"LVIII"
解釋: L =?50, V =?5, III =?3.
題解

int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] symbols = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
public?String intToRoman(int?num)?{
????StringBuilder sb = new?StringBuilder();
????// Loop through each symbol, stopping if num becomes 0.
????for?(int?i = 0; i < values.length && num >= 0; i++) {
????????// Repeat while the current symbol still fits into num.
????????while?(values[i] <= num) {
????????????num -= values[i];
????????????sb.append(symbols[i]);
????????}
????}
????return?sb.toString();
}
今天的文章就到這里,如果覺(jué)得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力。
上期推文:
LeetCode刷題實(shí)戰(zhàn)1:在數(shù)組上遍歷出花樣
LeetCode刷題實(shí)戰(zhàn)2:用鏈表模擬加法
LeetCode刷題實(shí)戰(zhàn)3:最長(zhǎng)不重復(fù)子串
LeetCode刷題實(shí)戰(zhàn)4:兩個(gè)正序數(shù)組的中位數(shù)
LeetCode刷題實(shí)戰(zhàn)5:判斷回文子串
LeetCode刷題實(shí)戰(zhàn)6:Z字形變換
LeetCode刷題實(shí)戰(zhàn)7:整數(shù)反轉(zhuǎn)
LeetCode刷題實(shí)戰(zhàn)8:字符串轉(zhuǎn)換整數(shù)
LeetCode刷題實(shí)戰(zhàn)9:求解回文數(shù)
LeetCode刷題實(shí)戰(zhàn)10:字符串正則匹配
LeetCode刷題實(shí)戰(zhàn)11: 盛最多水的容器
