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

Commit971d5f7

Browse files
authored
Format code in RomanToInteger.java
* Format code* Remove redundant code
1 parentc14af04 commit971d5f7

File tree

1 file changed

+51
-55
lines changed

1 file changed

+51
-55
lines changed

‎Conversions/RomanToInteger.java

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
importjava.util.*;
2+
23
publicclassRomanToInteger {
3-
4-
/*
5-
This function convert Roman number into Integer
6-
@param A is Roman number string
7-
*/
8-
publicstaticintromanToInt(StringA) {
9-
Map<Character ,Integer>map =newHashMap<>();
10-
map.put('I' ,1);
11-
map.put('V' ,5);
12-
map.put('X' ,10);
13-
map.put('L' ,50);
14-
map.put('C' ,100);
15-
map.put('D' ,500);
16-
map.put('M' ,1000);
17-
18-
charc =A.charAt(A.length()-1);
19-
charprev =' ';
20-
21-
intsum =0;
22-
23-
intnewPrev =0,currentNum =0;
24-
for(inti =A.length() -1;i>=0;i--)
25-
{
26-
c =A.charAt(i);
27-
28-
29-
if(prev !=' ') {
30-
//checking current Number greater then previous or not
31-
newPrev =map.get(prev) >newPrev ?map.get(prev) :newPrev ;
32-
}
33-
34-
35-
currentNum =map.get(c);
36-
37-
if(currentNum >=newPrev )//if current number greater then prev max previous then add
38-
{
39-
sum +=currentNum;
40-
}
41-
else {
42-
43-
sum -=currentNum;// subtract upcoming number until upcoming number not greater then prev max
44-
}
45-
46-
prev =c;
47-
}
48-
49-
returnsum;
50-
}
514

5+
privatestaticMap<Character,Integer>map =newHashMap<>() {{
6+
put('I',1);
7+
put('V',5);
8+
put('X',10);
9+
put('L',50);
10+
put('C',100);
11+
put('D',500);
12+
put('M',1000);
13+
}};
14+
15+
/**
16+
* This function convert Roman number into Integer
17+
*
18+
* @param A Roman number string
19+
* @return integer
20+
*/
21+
publicstaticintromanToInt(StringA) {
22+
23+
charprev =' ';
24+
25+
intsum =0;
26+
27+
intnewPrev =0;
28+
for (inti =A.length() -1;i >=0;i--) {
29+
charc =A.charAt(i);
5230

53-
publicstaticvoidmain(String[]args) {
54-
55-
56-
intsum =romanToInt("MDCCCIV") ;
57-
System.out.println(sum);
58-
}
31+
if (prev !=' ') {
32+
// checking current Number greater then previous or not
33+
newPrev =map.get(prev) >newPrev ?map.get(prev) :newPrev;
34+
}
5935

60-
}
36+
intcurrentNum =map.get(c);
37+
38+
// if current number greater then prev max previous then add
39+
if (currentNum >=newPrev) {
40+
sum +=currentNum;
41+
}else {
42+
// subtract upcoming number until upcoming number not greater then prev max
43+
sum -=currentNum;
44+
}
45+
46+
prev =c;
47+
}
48+
49+
returnsum;
50+
}
51+
52+
publicstaticvoidmain(String[]args) {
53+
intsum =romanToInt("MDCCCIV");
54+
System.out.println(sum);
55+
}
56+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp