๐ ๋ฌธ์
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not
IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
• I can be placed before V (5) and X (10) to make 4 and 9.
• X can be placed before L (50) and C (100) to make 40 and 90.
• C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral.
๋ก๋ง ์ซ์๋ I, V, X, L, C, D ๋ฐ M์ 7๊ฐ์ง ๊ธฐํธ๋ก ํ์๋๋ค.
์๋ฅผ ๋ค์ด, 2๋ ๋ก๋ง ์ซ์์์ II๋ก ํ๊ธฐ๋๊ณ ๋จ์ง ๋ ๊ฐ์ 1์ ๋ํ ๊ฒ์ด๋ค. 12๋ XII๋ก ํ๊ธฐ๋๋ฉฐ, X + II์ ๋๋ค. ์ซ์ 27์ XXVII๋ก ํ๊ธฐ๋๋ฉฐ, ์ด๋ XX + V + II ์ด๋ค.
๋ก๋ง ์ซ์๋ ์ผ๋ฐ์ ์ผ๋ก ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ํฐ ์์๋ก ์ด๋ค. ๊ทธ๋ฌ๋ 4์ ์ซ์๋ III. ๋์ ์ซ์ 4๋ IV๋ก ์ด๋ค. 1์ด 5๋ณด๋ค ์์ ์๊ธฐ ๋๋ฌธ์ ๋นผ๋ฉด 4๊ฐ ๋๋ค. ๊ฐ์ ์๋ฆฌ๊ฐ IX๋ก ์ฐ์ฌ์ง ์ซ์ 9์๋ ์ ์ฉ๋๋ค. ๋นผ๊ธฐ๊ฐ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ๋ 6๊ฐ์ง๋ค.
• V(5)์ X(10) ์์ I๋ฅผ ๋ฐฐ์นํ์ฌ 4์ 9๋ฅผ ๋ง๋ค ์ ์๋ค.
• X๋ L(50)๊ณผ C(100) ์์ ๋ฐฐ์นํ์ฌ 40๊ณผ 90์ ๋ง๋ค ์ ์๋ค.
• C๋ D(500)์ M(1000) ์์ ๋ฐฐ์น๋์ด 400๊ณผ 900์ ๋ง๋ค ์ ์๋ค.
์ ์๊ฐ ์ฃผ์ด์ง๋ฉด ๋ก๋ง ์ซ์๋ก ๋ณํํด๋ผ.
๐ ์์
Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.
Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
๐ ํ์ด ์ค๋ช
- 900, 400, 90, 40, 9, 4 ์ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์กฐ๊ฑด๋ฌธ์ ์ฐ๋ ค๊ณ ํ์ผ๋ ์ฝ๋๊ฐ ๊ธธ์ด์ง ๊ฒ ๊ฐ์ ์๊ฐ์ ๋ฐ๊พธ์๋ค.
- ๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด์ ๋ด์์ ์ฝ๊ฒ ํ๊ธฐ๋ก ํ์๋ค.
- ๋ฐ๋ผ์ String[] ๊ณผ int[]์ ๋ฐฐ์ด์ ๊ฐ๊ฐ roman ๋ฌธ์์ int ๊ฐ์ ๋ฃ์ด์ฃผ์๋ค.
public String intToRoman(int num) {
int[] number = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
}
- ๊ฐ ๋ฐฐ์ด์ ์ธ๋ฑ์ค ๊ฐ์ ํ์ธํด์ผํ๊ธฐ ๋๋ฌธ์ idx๊ฐ์ ์ ์ธํด์ฃผ๊ณ ์ด๊ธฐํ ํด์ฃผ์๋ค.
- ๋ค์ด์ค๋ num ๊ฐ์ด 0 ๋ณด๋ค ํฌ๊ณ ์ธ๋ฑ์ค ๊ฐ์ด number ๋ฐฐ์ด์ ๊ธธ์ด๋ณด๋ค ์์ผ๋ฉด ๋ฐ๋ณต๋ฌธ ์์ ๋ธ๋ญ์ด ๋์๊ฐ ์ ์๋๋ก ์กฐ๊ฑด์์ ์ ์ด์ฃผ์๋ค.
- number ๋ฐฐ์ด์ ํ์ฌ ์ธ๋ฑ์ค ๊ฐ๋ณด๋ค ํฌ๋ฉด, ํ์ฌ ๋ฐฐ์ด์ ๊ฐ์ ๋นผ์ฃผ๊ณ StringBuilder๋ฅผ ์ด์ฉํ์ฌ ํด๋น ์ธ๋ฑ์ค์ roman ๋ฌธ์์ด์ ์ถ๊ฐํด์ฃผ์๋ค.
public String intToRoman(int num) {
...
int idx = 0;
StringBuilder sb = new StringBuilder();
while (num > 0 && idx < number.length) {
if (num >= number[idx]) {
sb.append(roman[idx]);
num -= number[idx];
} else {
idx++;
}
}
return sb.toString();
}
๐ ์ต์ข ํ์ด
public String intToRoman(int num) {
int[] number = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int idx = 0;
StringBuilder sb = new StringBuilder();
while (num > 0 && idx < number.length) {
if (num >= number[idx]) {
sb.append(roman[idx]);
num -= number[idx];
} else {
idx++;
}
}
return sb.toString();
}
'์๊ณ ๋ฆฌ์ฆ > LeetCode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 013. Roman to Integer JAVA (0) | 2022.12.30 |
---|---|
[LeetCode] 011.Container With Most Water JAVA (0) | 2022.12.26 |
[LeetCode] 004. Median of Two Sorted Arrays JAVA (0) | 2022.12.22 |
[LeetCode] 008. String to Integer (atoi) JAVA (0) | 2022.12.21 |
[LeetCode]006. Zigzag Conversion JAVA (0) | 2022.12.21 |
๋๊ธ