5ms Java solution


  • 0
    D
    public class Solution {
        public String numberToWords(int num) {
            if (num == 0) return "Zero";
            int first = num % 1000;
            int second = (num / 1000) % 1000;
            int third = (num / 1000000) % 1000;
            int fourth = (num / 1000000000) % 1000;
            StringBuffer sb = new StringBuffer();
            String fourth_str = int_to_str(fourth);
            String third_str = int_to_str(third);
            String second_str = int_to_str(second);
            String first_str = int_to_str(first);
            if (fourth != 0) {
                sb.append(fourth_str);
                sb.append(" Billion");
            }
            if (third != 0) {
                sb.append(fourth == 0 ? "" : " ");
                sb.append(third_str);
                sb.append(" Million");
            }
            if (second != 0) {
                sb.append((fourth == 0 && third == 0) ? "" : " ");
                sb.append(second_str);
                sb.append(" Thousand");
            }
            if (first != 0) {
                sb.append((fourth == 0 && third == 0 && second == 0) ? "" : " ");
                sb.append(first_str);
            }
            return sb.toString();
        }
        
        private String int_to_str(int n) {
            if (n == 0) return "Zero";
            int first = n % 10;
            int second = (n / 10) % 10;
            int third = (n / 100) % 10;
            String first_str = _one_digit_str1(first);
            String second_str = _one_digit_str2(second, first);
            String third_str = _one_digit_str1(third);
            StringBuffer sb = new StringBuffer();
            if (third != 0) {
                sb.append(third_str);
                sb.append(" Hundred");
            }
            if (second != 0) {
                sb.append(third == 0 ? "" : " ");
                sb.append(second_str);
            }
            if (first != 0 && second != 1) {
                sb.append((third == 0 && second == 0) ? "" : " ");
                sb.append(first_str);
            }
            return sb.toString();
        }
        
        private String _one_digit_str1(int n) {
            switch (n) {
                case 1: return "One";
                case 2: return "Two";
                case 3: return "Three";
                case 4: return "Four";
                case 5: return "Five";
                case 6: return "Six";
                case 7: return "Seven";
                case 8: return "Eight";
                case 9: return "Nine";
            }
            return "XYZ";
        }
        
        private String _one_digit_str2(int a, int b) {
            switch (a) {
                case 1:
                    switch (b) {
                        case 0: return "Ten";
                        case 1: return "Eleven";
                        case 2: return "Twelve";
                        case 3: return "Thirteen";
                        case 4: return "Fourteen";
                        case 5: return "Fifteen";
                        case 6: return "Sixteen";
                        case 7: return "Seventeen";
                        case 8: return "Eighteen";
                        case 9: return "Nineteen";
                    }
                    break;
                case 2: return "Twenty";
                case 3: return "Thirty";
                case 4: return "Forty";
                case 5: return "Fifty";
                case 6: return "Sixty";
                case 7: return "Seventy";
                case 8: return "Eighty";
                case 9: return "Ninety";
            }
            return "ABC";
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.