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

Commit0b362be

Browse files
author
applewjg
committed
Integer To Roman
Change-Id: I295dfba44c3189f5885dc346f33d5bea9e169a71
1 parentf6160ab commit0b362be

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

‎IntegertoRoman.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 13, 2014
4+
Problem: Integer to Roman
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/integer-to-roman/
7+
Notes:
8+
Given an integer, convert it to a roman numeral.
9+
Input is guaranteed to be within the range from 1 to 3999.
10+
11+
Solution: Buffer the roman numbers.
12+
*/
13+
14+
publicclassSolution {
15+
publicStringintToRoman(intnum) {
16+
int[]values = {1000,900,500,400,100,90,50,40,10,9,5,4,1 };
17+
String[]numerals = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I" };
18+
StringBuilderresult =newStringBuilder();
19+
for(inti=0;i<values.length;i++)
20+
{
21+
while(num>=values[i])
22+
{
23+
num-=values[i];
24+
result.append(numerals[i]);
25+
}
26+
}
27+
returnresult.toString();
28+
}
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp