GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-3/problems/src/math/RomanToInteger.java at master · abcdvemm/leetcode-3

Expand file tree

/

RomanToInteger.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
48 lines (43 loc) · 1.02 KB
/

RomanToInteger.java

Copy path

File metadata and controls

48 lines (43 loc) · 1.02 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
package math;
import java.util.HashMap;
import java.util.Map;
/**
* Created by gouthamvidyapradhan on 12/08/2017.
*
* <p>Given a roman numeral, convert it to an integer.
*
* <p>Input is guaranteed to be within the range from 1 to 3999.
*/
public class RomanToInteger {
/**
* Main method
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(new RomanToInteger().romanToInt("DXCIX"));
}
public int romanToInt(String s) {
Map<Character, Integer> map = new HashMap<>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
String str = new StringBuilder(s).reverse().toString();
int sum = 0, prev = -1;
for (int i = 0, l = str.length(); i < l; i++) {
int curr = map.get(str.charAt(i));
if (curr < prev) {
sum -= curr;
} else {
sum += curr;
}
prev = curr;
}
return sum;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.