LeetCode 0273. Integer to English Words Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0273. Integer to English Words

Description

Convert a non-negative integer num to its English words representation.

 

Example 1:

Input: num = 123
Output: "One Hundred Twenty Three"

Example 2:

Input: num = 12345
Output: "Twelve Thousand Three Hundred Forty Five"

Example 3:

Input: num = 1234567
Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"

 

Constraints:

  • 0 <= num <= 231 - 1

Solutions

Solution 1

PythonJavaC++GoTypeScriptJavaScriptC#
class Solution: def numberToWords(self, num: int) -> str: if num == 0: return 'Zero' lt20 = [ '', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen', ] tens = [ '', 'Ten', 'Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety', ] thousands = ['Billion', 'Million', 'Thousand', ''] def transfer(num): if num == 0: return '' if num < 20: return lt20[num] + ' ' if num < 100: return tens[num // 10] + ' ' + transfer(num % 10) return lt20[num // 100] + ' Hundred ' + transfer(num % 100) res = [] i, j = 1000000000, 0 while i > 0: if num // i != 0: res.append(transfer(num // i)) res.append(thousands[j]) res.append(' ') num %= i j += 1 i //= 1000 return ''.join(res).strip()(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !