FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Conversions/RomanToInteger.java at master · java66liu/Java · GitHub
java66liu
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Conversions
/
RomanToInteger.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
65 lines (51 loc) · 1.52 KB
Breadcrumbs
Java
/
Conversions
/
RomanToInteger.java
Copy path
File metadata and controls
65 lines (51 loc) · 1.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package
Conversions
;
import
java
.
util
.*;
public
class
RomanToInteger
{
private
static
Map
<
Character
,
Integer
>
map
=
new
HashMap
<
Character
,
Integer
>() {
/**
*
*/
private
static
final
long
serialVersionUID
=
87605733047260530L
;
{
put
(
'I'
,
1
);
put
(
'V'
,
5
);
put
(
'X'
,
10
);
put
(
'L'
,
50
);
put
(
'C'
,
100
);
put
(
'D'
,
500
);
put
(
'M'
,
1000
);
}};
//Roman Number = Roman Numerals
/**
* This function convert Roman number into Integer
*
* @param A Roman number string
* @return integer
*/
public
static
int
romanToInt
(
String
A
) {
char
prev
=
' '
;
int
sum
=
0
;
int
newPrev
=
0
;
for
(
int
i
=
A
.
length
() -
1
;
i
>=
0
;
i
--) {
char
c
=
A
.
charAt
(
i
);
if
(
prev
!=
' '
) {
// checking current Number greater then previous or not
newPrev
=
map
.
get
(
prev
) >
newPrev
?
map
.
get
(
prev
) :
newPrev
;
}
int
currentNum
=
map
.
get
(
c
);
// if current number greater then prev max previous then add
if
(
currentNum
>=
newPrev
) {
sum
+=
currentNum
;
}
else
{
// subtract upcoming number until upcoming number not greater then prev max
sum
-=
currentNum
;
}
prev
=
c
;
}
return
sum
;
}
public
static
void
main
(
String
[]
args
) {
int
sum
=
romanToInt
(
"MDCCCIV"
);
System
.
out
.
println
(
sum
);
}
}
Back
|
FazBrowse Home
|
New Git URL