码迷,mamicode.com
首页 > 其他好文 > 详细

12. Integer to Roman && 13. Roman to Integer && 273. Integer to English Words

时间:2016-07-24 07:01:32      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

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

 
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:

  1. Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000.
  2. 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.
  3. 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);
  }
}

 

 
 

12. Integer to Roman && 13. Roman to Integer && 273. Integer to English Words

标签:

原文地址:http://www.cnblogs.com/neweracoding/p/5700025.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!