Skip to content

Commit e594030

Browse files
committed
273_Integer_to_English_Words
1 parent 629d0bd commit e594030

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Remember solutions are only solutions to given problems. If you want full study
9292
| 267 | [Palindrome Permutation II](https://leetcode.com/problems/palindrome-permutation-ii/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/267_Palindrome_Permutation_II.py) | Check palindrome then generate half with [Permutations II](https://leetcode.com/problems/permutations-ii/), O(n^2) and O(n^2) |
9393
| 268 | [Missing Number](https://leetcode.com/problems/missing-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/268_Missing_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/268_Missing_Number.java) | 1. Find missing by n * (n - 1)/2 - sum(nums)<br>2. XOR with index<br>3. Sort and binary search |
9494
| 270 | [Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/270_Closest_Binary_Search_Tree_Value.py) | 1. Recursively brute force, O(n) and O(n)<br>2. Recursively compare root result with current kid's result (left or right), O(logn) and O(logn)<br>3. Iteratively compare root result with current kid's result (left or right), O(logn) and O(logn) |
95+
| 273 | [Integer to English Words](https://leetcode.com/problems/integer-to-english-words/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/273_Integer_to_English_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/python/273_Integer_to_English_Words.java) | Careful about corner cases, such 1-20 and 21-Hundred, O(lgn) and O(1) |
9596
| 274 | [H-Index](https://leetcode.com/problems/h-index/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/274_H-Index.py) | [Background](https://en.wikipedia.org/wiki/H-index)<br>1. Sort and check number of papers greater than h, O(nlogn) and O(1)<br>2. Counting sort, O(n) and O(n) |
9697
| 276 | [Paint Fence](https://leetcode.com/problems/paint-fence/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/276_Paint_Fence.py) | ways[i>2] = (ways[i-1] + ways[i-2]) * (k - 1), O(n) and O(1) |
9798
| 280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/280_Wiggle_Sort.py) | 1. Sort and insert (n - 1) / 2 from tail to correct position, O(nlogn) and O(1)<br>2. Sort and swap(i, i + 1) from 1 to n - 1, O(nlogn)<br>3. Iteratively check order and reverse order, if not satisfied, then swap i with i + 1, O(n) |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
// https://leetcode.com/problems/integer-to-english-words/discuss/70625/My-clean-Java-solution-very-easy-to-understand
3+
private final String[] LESS_THAN_20 = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
4+
private final String[] TENS = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
5+
private final String[] THOUSANDS = {"", "Thousand", "Million", "Billion"};
6+
7+
public String numberToWords(int num) {
8+
if (num == 0) return "Zero";
9+
10+
int i = 0;
11+
String words = "";
12+
13+
while (num > 0) {
14+
if (num % 1000 != 0)
15+
words = helper(num % 1000) + THOUSANDS[i] + " " + words;
16+
num /= 1000;
17+
i++;
18+
}
19+
20+
return words.trim();
21+
}
22+
23+
private String helper(int num) {
24+
if (num == 0)
25+
return "";
26+
else if (num < 20)
27+
return LESS_THAN_20[num] + " ";
28+
else if (num < 100)
29+
return TENS[num / 10] + " " + helper(num % 10);
30+
else
31+
return LESS_THAN_20[num / 100] + " Hundred " + helper(num % 100);
32+
}
33+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def numberToWords(self, num):
3+
# https://leetcode.com/problems/integer-to-english-words/discuss/70632/Recursive-Python
4+
to19 = 'One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve ' \
5+
'Thirteen Fourteen Fifteen Sixteen Seventeen Eighteen Nineteen'.split()
6+
tens = 'Twenty Thirty Forty Fifty Sixty Seventy Eighty Ninety'.split()
7+
def words(n):
8+
if n < 20:
9+
return to19[n - 1:n]
10+
if n < 100:
11+
return [tens[n / 10 - 2]] + words(n % 10)
12+
if n < 1000:
13+
return [to19[n / 100 - 1]] + ['Hundred'] + words(n % 100)
14+
for p, w in enumerate(('Thousand', 'Million', 'Billion'), 1):
15+
if n < 1000 ** (p + 1):
16+
return words(n / 1000 ** p) + [w] + words(n % 1000 ** p)
17+
return ' '.join(words(num)) or 'Zero'

0 commit comments

Comments
 (0)