GeezerVison is showing github.com — Refine 012_Integer_to_Roman · iamsathish/leetcode@b516688
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -21,7 +21,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal | 21 | 21 | | 8 | [String to Integer (atoi)](https://leetcode.com/problems/string-to-integer-atoi/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/008_String_to_Integer(atoi).py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/008_String_to_Integer(atoi).java) | Overflow, Space, and negative number | | 22 | 22 | | 9 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/009_Palindrome_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/009_Palindrome_Number.java) | Get the len and check left and right with 10^len, 10 | | 23 | 23 | | 11 | [Container With Most Water](https://leetcode.com/problems/container-with-most-water/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/011_Container_With_Most_Water.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/011_Container_With_Most_Water.java) | 1. Brute Force, O(n^2) and O(1)<br>2. Two points, O(n) and O(1) | | 24 | | -| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/012_Integer_to_Roman.py) | [Background knowledge](http://www.rapidtables.com/convert/number/how-number-to-roman-numerals.htm) Just like 10-digit number, divide and minus | | | 24 | +| 12 | [Integer to Roman](https://leetcode.com/problems/integer-to-roman/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/012_Integer_to_Roman.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/012_Integer_to_Roman.java) | [Background knowledge](http://www.rapidtables.com/convert/number/how-number-to-roman-numerals.htm) Just like 10-digit number, divide and minus | | 25 | 25 | | 13 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/013_Roman_to_Integer.py) | Add all curr, if curr > prev, then need to subtract 2 * prev | | 26 | 26 | | 15 | [3Sum](https://leetcode.com/problems/3sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/015_3Sum.py) | 1. Sort and O(n^2) search with three points <br>2. Multiple Two Sum (Problem 1) | | 27 | 27 | | 16 | [3Sum Closest](https://leetcode.com/problems/3sum-closest/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/016_3Sum_Closest.py) | Sort and Multiple Two Sum check abs| | |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution { | | 2 | + public String intToRoman(int num) { | | 3 | + Map<Integer, String> map = new HashMap(); | | 4 | + map.put(1, "I"); map.put(5, "V"); map.put(10, "X"); | | 5 | + map.put(50, "L"); map.put(100, "C"); map.put(500, "D"); map.put(1000, "M"); | | 6 | + map.put(4, "IV"); map.put(9, "IX"); map.put(40, "XL"); map.put(90, "XC"); | | 7 | + map.put(400, "CD"); map.put(900, "CM"); | | 8 | + | | 9 | + int[] sequence = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; | | 10 | + | | 11 | + StringBuffer sb = new StringBuffer(); | | 12 | + for (int i = 0; i<sequence.length; i++) { | | 13 | + int base = sequence[i]; | | 14 | + | | 15 | + while (num >= base) { | | 16 | + sb.append(map.get(base)); | | 17 | + num -= base; | | 18 | + } | | 19 | + } | | 20 | + | | 21 | + return sb.toString(); | | 22 | + } | | 23 | +} |
Collapse file Copy file name to clipboardExpand all lines: java/012_integertoroman.java -21Lines changed: 0 additions & 21 deletions
|