您的位置:首页 > 其它

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: