솔루션: 정수에서 로마자로
Leetcode 문제 #12(중간): 정수에서 로마자로
설명:
(이동: Solution Idea || 코드: JavaScript | Python | Java | C++ )
Roman numerals are represented by seven different symbols:
I,V,X,L,C,DandM.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example,
2is written asIIin Roman numeral, just two one's added together.12is written asXII, which is simplyX + II. The number27is written asXXVII, which isXX + 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 asIV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written asIX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make4and9.Xcan be placed beforeL(50) andC(100) to make40and90.Ccan be placed beforeD(500) andM(1000) to make400and900.Given an integer, convert it to a roman numeral.
예:
Example 1: Input: num = 3 Output: "III"
Example 2: Input: num = 4 Output: "IV"
Example 3: Input: num = 9 Output: "IX"
Example 4: Input: num = 58 Output: "LVIII" Explanation: L = 50, V = 5, III = 3.
Example 5: Input: num = 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
제약 조건:
1 <= num <= 3999
아이디어:
(이동: Problem Description || 코드: JavaScript | Python | Java | C++ )
Roman to Integer와 마찬가지로 이 문제는 숫자와 숫자 간의 변환을 위한 조회 테이블을 사용하여 가장 쉽게 해결됩니다. 이 경우 값을 내림차순으로 쉽게 처리할 수 있으며 목표 숫자(N)를 같은 양만큼 줄이면서 적절한 숫자(또는 숫자)를 가능한 한 많이 삽입할 수 있습니다.
N이 소진되면 반환할 수 있습니다.
구현:
Java의 StringBuilder는 문자열 복사를 만드는 오버헤드 없이 반복되는 문자열 연결을 처리할 수 있습니다.
자바스크립트 코드:
(이동: Problem Description || Solution Idea )
const val = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
const rom = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
var intToRoman = function(N) {
let ans = ""
for (let i = 0; N; i++)
while (N >= val[i]) ans += rom[i], N -= val[i]
return ans
};
파이썬 코드:
(이동: Problem Description || Solution Idea )
val = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
rom = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
class Solution:
def intToRoman(self, N: int) -> str:
ans = ""
for i in range(13):
while N >= val[i]:
ans += rom[i]
N -= val[i]
return ans
자바 코드:
(이동: Problem Description || Solution Idea )
class Solution {
final static int[] val = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
final static String[] rom = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
public String intToRoman(int N) {
StringBuilder ans = new StringBuilder();
for (int i = 0; N > 0; i++)
while (N >= val[i]) {
ans.append(rom[i]);
N -= val[i];
}
return ans.toString();
}
}
C++ 코드:
(이동: Problem Description || Solution Idea )
class Solution {
public:
const int val[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
const string rom[13] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string intToRoman(int N) {
string ans = "";
for (int i = 0; N; i++)
while (N >= val[i]) ans += rom[i], N -= val[i];
return ans;
}
};
Reference
이 문제에 관하여(솔루션: 정수에서 로마자로), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/seanpgallivan/solution-integer-to-roman-1n3o텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)