Integer to English Words: convert numbers to English spelling

Keywords: less

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"

Thought: this problem is not difficult, mainly format conversion is too troublesome!!! For the space position, I tried for a long time....

The idea is to divide the numbers from the back to the front, take three numbers as a group, and judge each group. Pay attention to the differences between 1 - 19 in English grammar.

There is also the space position. I just didn't understand it at the beginning, and then tried it out little by little. It's heartbreaking

class Solution {
    HashMap<Integer,String> B;//A large unit, such as a thousand, a million, etc
    HashMap<Integer,String> M;//A medium-sized unit, such as 11, 12, etc
    HashMap<Integer,String> S;//A unit of minimum rank, such as 1 - 9
    
    public void init(){
        B = new HashMap<Integer,String>();
        B.put(0,"");
        B.put(1," Thousand");
        B.put(2," Million");
        B.put(3," Billion");
        
        M = new HashMap<Integer,String>();
        M.put(0,"");
        M.put(1,"One");
        M.put(2,"Two");
        M.put(3,"Three");
        M.put(4, "Four");
        M.put(5,"Five");
        M.put(6,"Six");
        M.put(7,"Seven");
        M.put(8,"Eight");
        M.put(9,"Nine");
        M.put(10,"Ten");
        M.put(11,"Eleven");
        M.put(12,"Twelve");
        M.put(13,"Thirteen");
        M.put(14,"Fourteen");
        M.put(15,"Fifteen");
        M.put(16,"Sixteen");
        M.put(17,"Seventeen");
        M.put(18,"Eighteen");
        M.put(19,"Nineteen");
        M.put(20,"Twenty");
        M.put(30,"Thirty");
        M.put(40,"Forty");
        M.put(50,"Fifty");
        M.put(60,"Sixty");
        M.put(70,"Seventy");
        M.put(80,"Eighty");
        M.put(90,"Ninety");
        M.put(100,"Hundred");
        
        S = new HashMap<Integer,String>();
        S.put(0,"");
        S.put(1,"One");
        S.put(2,"Two");
        S.put(3,"Three");
        S.put(4,"Four");
        S.put(5,"Five");
        S.put(6,"Six");
        S.put(7,"Seven");
        S.put(8,"Eight");
        S.put(9,"Nine");       
    }
    
    public String numberToWords(int num) {
        if(num == 0) return "Zero";
        init();
        int seg = 1;
        ArrayList<String> res = new ArrayList<String>();//Save a set of three numbers
        StringBuilder sb = new StringBuilder();
        int b = 0;//Control the use of large units
        String nums = num +"";
        int r = nums.length()-1;
        while(r >= 0){
        	int l = (r - 2 > 0)?(r - 2):0;
        	int sub = Integer.parseInt(nums.substring(l, r+1));//subString is the front closed and back open interval, so r+1
            num = num / seg * seg;
            if(sub >= 100){//Hundred bit
                int m = sub / 100 ; 
                sb.append(S.get(m)+" Hundred");
                sub = sub % 100;
            }
            if(sub != 0) sb.append(" ");
            if(sub < 20 ){//Ten place
                sb.append(M.get(sub));
                sub = 0;
            }else{
                int m = sub / 10 * 10;
                sb.append(M.get(m));
                sub = sub % 10;
            }
            if(sub != 0) sb.append(" ");
            sb.append(S.get(sub));//Bit   	
            if(Integer.parseInt(nums.substring(l, r+1))!=0){//One billion thousandthat's not going to happen
            	sb.append(B.get(b));
            }
            b++;//Control the position of thousand, million and billion
            res.add(sb.toString().trim());
            sb.setLength(0);
            r = r - 3;//Set every three numbers
        }
        sb.setLength(0);
        for(int i = res.size() - 1;i>=0; i--){       	
            sb.append(res.get(i).trim());
            if(i != 0&&res.get(i).length()!=0) sb.append(" ");
        }
        return sb.toString().trim();
    }
}




Posted by Hikari on Thu, 30 Apr 2020 01:05:00 -0700