Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Hide Tags
public class Solution { public String intToRoman(int num) { // HashMap<Character,Integer> hm = new HashMap<Character,Integer>(); // hm.put(‘I‘, 1); // hm.put(‘V‘, 5); // hm.put(‘X‘, 10); // hm.put(‘L‘, 50); // hm.put(‘C‘, 100); // hm.put(‘D‘, 500); // hm.put(‘M‘, 1000); StringBuilder sb = new StringBuilder(); addCharacter(sb, ‘M‘, ‘?‘, ‘?‘, num/1000); num = num%1000; addCharacter(sb, ‘C‘, ‘D‘, ‘M‘, num/100); num = num%100; addCharacter(sb, ‘X‘, ‘L‘, ‘C‘, num/10); num = num%10; addCharacter(sb, ‘I‘, ‘V‘, ‘X‘, num); return sb.toString(); } private void addCharacter(StringBuilder sb, char one, char five, char ten, int c) { if(c >= 1 && c<=3) for(int i = 0;i<c;++i) sb.append(one); else if(c == 4) { sb.append(one); sb.append(five); } else if(c == 5) sb.append(five); else if(c>=6 && c<=8){ sb.append(five); for(int i = 0;i<c-5;++i) sb.append(one); } else if(c==9){ sb.append(one); sb.append(ten); } else if(c==10) sb.append(ten); } }
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
public class Solution { public int romanToInt(String s) { HashMap<Character,Integer> hm = new HashMap<Character,Integer>(); hm.put(‘I‘, 1); hm.put(‘V‘, 5); hm.put(‘X‘, 10); hm.put(‘L‘, 50); hm.put(‘C‘, 100); hm.put(‘D‘, 500); hm.put(‘M‘, 1000); int pre = hm.get(s.charAt(0)); int total = 0; int l = s.length(); if(l==1) return pre; for(int i =1;i<l;++i) { int cur = hm.get(s.charAt(i)); if(pre>=cur) { total+=pre; } else { total-=pre; } pre=cur; } total+=pre; return total; } }
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1.
For example,
123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
Hint:
public class Solution { private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"}; public String numberToWords(int num) { if (num == 0) return "Zero"; int i = 0; String words = ""; while (num > 0) { int lower3 = num % 1000; if (lower3 != 0) words = toWords(lower3) + THOUSANDS[i] + " " + words; num /= 1000; ++i; } return words.trim(); } private String toWords(int num) { if (num == 0) return ""; else if (num < 20) return LESS_THAN_20[num] + " "; else if (num < 100) return TENS[num / 10] + " " + toWords(num % 10); else return LESS_THAN_20[num / 100] + " Hundred " + toWords(num % 100); } }
12. Integer to Roman && 13. Roman to Integer && 273. Integer to English Words
原文:http://www.cnblogs.com/neweracoding/p/5700025.html