Java教程

273. Integer to English Words

本文主要是介绍273. Integer to English Words,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
public class Solution {
    private String[] lessThanTwenty = { "", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine",
            "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
            "Nineteen" };
    private String[] lessThanHundred = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty",
            "Ninety" };

     public String numberToWords(int num) {
        if (num < 0)
            return "";
        if (num == 0)
            return "Zero";        
        return helper2(num);
    }
    
    private String helper2(int num) {
        if (num >= 1000000000)
            return (helper2(num / 1000000000) + " Billion " + helper2(num % 1000000000)).trim();
        if (num >= 1000000)
            return (helper2(num / 1000000) + " Million " + helper2(num % 1000000)).trim();
        if (num >= 1000)
            return (helper2(num / 1000) + " Thousand " + helper2(num % 1000)).trim();
        if (num >= 100)
            return (helper2(num / 100) + " Hundred " + helper2(num % 100)).trim();
        if (num >= 20)
            return (lessThanHundred[num / 10] + " " + helper2(num % 10)).trim();
        else
            return lessThanTwenty[num];
    }
}

Words to Integer

    private Map<String, Integer> map = new HashMap<>();

    public int wordsToNumber(String words) {
        map.put("One", 1);
        map.put("Two", 2);
        map.put("Three", 3);
        map.put("Four", 4);
        map.put("Five", 5);
        map.put("Six", 6);
        map.put("Seven", 7);
        map.put("Eight", 8);
        map.put("Nine", 9);
        map.put("Ten", 10);
        map.put("Eleven", 11);
        map.put("Twelve", 12);
        map.put("Thirteen", 13);
        map.put("Fourteen", 14);
        map.put("Fifteen", 15);
        map.put("Sixteen", 16);
        map.put("Seventeen", 17);
        map.put("Eighteen", 18);
        map.put("Nineteen", 19);
        map.put("Twenty", 20);
        map.put("Thirty", 30);
        map.put("Forty", 40);
        map.put("Fifty", 50);
        map.put("Sixty", 60);
        map.put("Seventy", 70);
        map.put("Eighty", 80);
        map.put("Ninety", 90);
        map.put("Zero", 0);

        return helper(words);
    }

    private int helper(String words) {
        words = words.trim();
        int index = words.indexOf("Billion");
        if (index > 0) {
            return helper(words.substring(0, index)) * 1000000000 + helper(words.substring(index + 7));
        }
        index = words.indexOf("Million");
        if (index > 0) {
            return helper(words.substring(0, index)) * 1000000 + helper(words.substring(index + 7));
        }
        index = words.indexOf("Thousand");
        if (index > 0) {
            return helper(words.substring(0, index)) * 1000 + helper(words.substring(index + 8));
        }
        index = words.indexOf("Hundred");
        if (index > 0) {
            return helper(words.substring(0, index)) * 100 + helper(words.substring(index + 7));
        }
        index = words.indexOf(" ");
        if (index >= 0) {
            return helper(words.substring(0, index)) + helper(words.substring(index + 1));
        }
        if (words.equals(""))
            return 0;
        return map.get(words);
    }

 

这篇关于273. Integer to English Words的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!