GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/src/IntegerToRoman.java at master · LeBW/leetcode Expand file tree/ Copy pathIntegerToRoman.javaMore file actions 63 lines (58 loc) · 1.45 KB / Copy pathIntegerToRoman.javaFile metadata and controls63 lines (58 loc) · 1.45 KB 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 import java.util.HashMap; /** * @author LBW */ public class IntegerToRoman { public String intToRoman(int num) { HashMap<Integer, String> map = new HashMap<>(); map.put(1, "I"); map.put(4, "IV"); map.put(5, "V"); map.put(9, "IX"); map.put(10, "X"); map.put(40, "XL"); map.put(50, "L"); map.put(90, "XC"); map.put(100, "C"); map.put(400, "CD"); map.put(500, "D"); map.put(900, "CM"); map.put(1000, "M"); StringBuilder result = new StringBuilder(); while (num > 0) { int cur = maxPosition(num); result.append(map.get(cur)); num -= cur; } return result.toString(); } private int maxPosition(int num) { if (num >= 1000) return 1000; else if (num >= 900) return 900; else if (num >= 500) return 500; else if (num >= 400) return 400; else if (num >= 100) return 100; else if (num >= 90) return 90; else if (num >= 50) return 50; else if (num >= 40) return 40; else if (num >= 10) return 10; else if (num >= 9) return 9; else if (num >= 5) return 5; else if (num >= 4) return 4; else if (num >= 1) return 1; return -1; } } |
| You are visitor 01402775 — 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. |