Created
March 21, 2018 05:24
-
-
Save jianminchen/d0ea6391ee9cebb72f52e41d52cb5a47 to your computer and use it in GitHub Desktop.
Convert integer to name - Leetcode 273 - 45 minutes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9:32 pm | |
273. Integer to English Words | |
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 2^31 - 1. | |
2 billion is max 2,000,000,000 = 2^9 | |
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" | |
// Int to Words | |
// 12 -> twelve | |
// 4012 -> four thousand twelve | |
// 4000012 -> four million twelve | |
[0,1,2,3] | |
[billion, million, hundred, ""] | |
1000 ^ ( 3 - i) - | |
int -> number on line 19 | |
go over loop line 18 -> | |
1000,000,000 -> %1000,000,000 | |
residue will be 0 - 999 | |
for the number less than 0 - 999, we will map to | |
[100,10,1] | |
for example, 345 | |
[3, 4, 5] | |
then concatenate to 3 hundred forty five | |
string mapToMmeaningfulSentence(int number) | |
{ | |
// 2, 000,000,000 -> 1, 234, 567, 890 -> forward order, deal with leftmost one -> billion, then work on 234 -> two hundred thirty four, then we know it is from digit 7 to 9, add suffix called million, | |
// next work on 567 -> five hundred sixty seven, add suffix called thousand | |
// next work on 890 -|> eight hundred ninty | |
var everyThousand = new int[]{0, 1, 2, 3}; | |
var everyThousandName = new string[]{"billion", "million", "hundred", ""}; | |
var lessThan20BigerThan10 = new string[]{eleven, tweleve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, ninteen}; | |
var with0 = new string[]{ten, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninty}; | |
var lessThan10 = new string[]{zero, one, two,three, four, five, six, seven, eight, nine}; | |
// base case 100 -|> one hundred -> | |
// 1, 234, 567, 890 | |
//112 | |
int residue = 0; | |
int leftover = nummber; /// 112 | |
var name = ""; | |
foreach(int index in everythousand ){ index = 0 | |
int thousands = 1000^(3 - index); // 1, 1,000,000, 1000, 1 | |
residue = number % thousands; // 0, 0, 0, 112 | |
leftover = number - residum * thousands; // 112, 112, 112, 0 | |
if(residue = 0) // true, treu | |
continue; | |
// residumme is less than 1000 | |
// check right most digit | |
// | |
var hundredDigit = residum%100; | |
var hudnredName = lessThan10[hundredDigt] | |
name += hundredDigt == 0? "" : hundredName + " hundred"; | |
// check second right most digit | |
var twoDigit|Number = residum - hundredDigit * 100; | |
var tenthDigit = twoDigitNumber/ 10; | |
name += tenthDigit== 0? "" : (tenthDigit == 1)? "" : with0[tenthDigit]; | |
// check third most digit | |
var singleDigitNumber = twoDigitNumber - tenDigit * 10; | |
var tenDigitIsOnne = tenthDigit == 1; | |
if(tenDigitIsOne) | |
{ | |
name += lessThan20BigerThan10[singleDigitNummber]; | |
} | |
else | |
{ | |
name += (singledigitNumber == 0)? "" : lessThan10[singleDigitNumber]; | |
} | |
name+= everyThousandName[index]; | |
} | |
return name; | |
} | |
10 digits | |
0 digit - one to nine | |
1 digt - 1x -->| | |
2 digit - hundred | |
3 digit - thousand | |
4 digit - same as 1 digt | |
5 digit - same as 3 digit | |
6 - million | |
7 - same as 1 | |
8 - same as 2 | |
9 - billion | |
public String intToWordsNumbersUnderThousand(int val){ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment