ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

273. Integer to English Words

2022-01-28 06:31:10  阅读:123  来源: 互联网

标签:map num Words index return 273 words put Integer


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

 

标签:map,num,Words,index,return,273,words,put,Integer
来源: https://www.cnblogs.com/feiflytech/p/15851806.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有