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

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
 
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);
  }
}

 

 
 

转载于:https://www.cnblogs.com/neweracoding/p/5700025.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值