[Leetcode] Integer to Roman
3971 ワード
https://leetcode.com/problems/integer-to-roman/
Given an integer, convert it to a roman numeral.
問題は、整数が与えられたらローマ数字でマークすることです.
問題を追加するには、次の手順に従います.
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 XII, which is simply X
ご存じのように、数字2はローマ数字IIと表記できます.
12もXIIと表記されている.これが整数numをローマ数字としてマークする方法の問題です.
私は問題のヒントで与えられた部分を考慮して問題を解いたのです.ヒントは次のとおりです.
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.
IはVとXの前にしか現れないXはLとCの前にしか現れない
すなわち
4, 9
40, 90
マーク400900の方法を考えればいいと思います.
ソースコードもif文を開きます.他にきれいな方法があると思いますソースコードは次のとおりです.
これはあまり難しくない問題です.時間が20分くらいかかったようです.
これからはdfsやdpなどの問題を勉強します.
Given an integer, convert it to a roman numeral.
問題は、整数が与えられたらローマ数字でマークすることです.
問題を追加するには、次の手順に従います.
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 XII, which is simply X
ご存じのように、数字2はローマ数字IIと表記できます.
12もXIIと表記されている.これが整数numをローマ数字としてマークする方法の問題です.
私は問題のヒントで与えられた部分を考慮して問題を解いたのです.ヒントは次のとおりです.
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.
IはVとXの前にしか現れないXはLとCの前にしか現れない
すなわち
4, 9
40, 90
マーク400900の方法を考えればいいと思います.
ソースコードもif文を開きます.他にきれいな方法があると思いますソースコードは次のとおりです.
public class IntegerToRoman {
private static final int I = 1;
private static final int V = 5;
private static final int X = 10;
private static final int L = 50;
private static final int C = 100;
private static final int D = 500;
private static final int M = 1000;
public static String solution(int num) {
int total = 0;
int calc = num;
StringBuilder ret = new StringBuilder();
while (total != num) {
if (calc / M > 0 ) {
total += M;
calc -= M;
ret.append("M");
}
else if (calc / D > 0) {
if (calc >= 900) {
total += 900;
calc -= 900;
ret.append("CM");
continue;
}
total += 500;
calc -= 500;
ret.append("D");
} else if (calc / C > 0) {
if (calc >= 400) {
total += 400;
calc -= 400;
ret.append("CD");
continue;
}
total += 100;
calc -= 100;
ret.append("C");
} else if (calc / L > 0) {
if (calc >= 90) {
total += 90;
calc -= 90;
ret.append("XC");
continue;
}
total += 50;
calc -= 50;
ret.append("L");
} else if (calc / X > 0) {
if (calc >= 40) {
total += 40;
calc -= 40;
ret.append("XL");
continue;
}
total += 10;
calc -= 10;
ret.append("X");
} else if (calc / V > 0) {
if (calc >= 9) {
total += 9;
calc -= 9;
ret.append("IX");
continue;
}
total += 5;
calc -= 5;
ret.append("V");
} else if (calc / I > 0) {
if (calc >= 4) {
total += 4;
calc -= 4;
ret.append("IV");
continue;
}
total += 1;
calc -= 1;
ret.append("I");
}
}
return ret.toString();
}
}
私たちは4、9、または40、90を考慮し、他の内容を考慮しません.次にint calcを宣言し,計算する値を動的に更新した.これはあまり難しくない問題です.時間が20分くらいかかったようです.
これからはdfsやdpなどの問題を勉強します.
Reference
この問題について([Leetcode] Integer to Roman), 我々は、より多くの情報をここで見つけました https://velog.io/@lkimilhol/Leetcode-Integer-to-Romanテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol