๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/LeetCode

[LeetCode] 012. Integer to Roman JAVA

by Bhinney 2022. 12. 23.
 

Integer to Roman - LeetCode

Integer to Roman - Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XI

leetcode.com


๐ŸŽ„ ๋ฌธ์ œ

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

๋Œ“๊ธ€