12. Integer to Roman && 13. Roman to Integer && 273. Integer to English Words
2016-07-24 05:10
537 查看
12. Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.
Hide Tags
Math String
Hide Similar Problems
(E) Roman to Integer (H) Integer to English Words
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); } }
13. Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.
Hide Tags
Math String
Hide Similar Problems
(M) Integer to Roman
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; } }
273. Integer to English Words
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:
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words.
There are many edge cases. What are some good test cases? Does your code work with input such as 0? Or 1000010? (middle chunk is zero and should not be printed out)
Hide Tags
Math String
Hide Similar Problems
(M) Integer to Roman
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); } }
相关文章推荐
- 343. Integer Break
- Android多线程操作——线程池管理综述
- Palindrome Permutation
- Min Stack
- Burst Balloons
- Intersection of Two Arrays II
- Intersection of Two Arrays II
- Flex布局
- 【慕课学习】《JavaScript深入浅出》——语句
- Reverse Vowels of a String
- 用vbs 将有序文件转换为有序文件
- HTML5网页新风格总结(与html4对比)
- 一个小玩意引起的反思
- 基于内容的推荐(第二周)
- jquery中each的三种遍历方法
- 开始使用Gulp
- Meeting Rooms
- Meeting Rooms
- 网站性能测试工具ab学习
- 打印一个N*N的方阵,N为每边字符的个数,要求最外层为“X”,第二层为“Y”,从第三层起每层依次打印数字0,1,2,3,...