Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit49156f9

Browse files
committed
intToRoman
1 parent267a8b2 commit49156f9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎string/IntToRoman.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packageAlgorithms.string;
2+
3+
publicclassIntToRoman {
4+
publicStringintToRoman(intnum) {
5+
intnums[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
6+
String[]romans = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
7+
8+
StringBuildersb =newStringBuilder();
9+
10+
inti =0;
11+
// 使用贪心法。尽量拆分数字
12+
while (i <nums.length) {
13+
if (num >=nums[i]) {
14+
sb.append(romans[i]);
15+
num -=nums[i];
16+
}else {
17+
i++;
18+
}
19+
}
20+
21+
returnsb.toString();
22+
}
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp