Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit36b4334

Browse files
committed
integer to english
1 parent4b87258 commit36b4334

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

‎Easy/104-maxDepth.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,12 @@ var maxDepth = function(root) {
3030
varrHeight=maxDepth(root.right)+1;
3131
returnlHeight>rHeight ?lHeight :rHeight;
3232
};
33+
34+
// simplist
35+
varmaxDepth=function(root){
36+
if(!root){
37+
return0;
38+
}
39+
40+
returnMath.max(maxDepth(root.left),maxDepth(root.right))+1;
41+
};

‎Hard/273-integerToEnglish.js‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
*@param {number} num
3+
*@return {string}
4+
*/
5+
6+
// my own solution
7+
varnumberToWords=function(num){
8+
vardict={1:'One',2:'Two',3:'Three',4:'Four',5:'Five',
9+
6:'Six',7:'Seven',8:'Eight',9:'Nine',10:'Ten',
10+
11:'Eleven',12:'Twelve',13:'Thirteen',14:'Fourteen',
11+
15:'Fifteen',16:'Sixteen',17:'Seventeen',18:'Eighteen',
12+
19:'Nineteen',20:'Twenty',30:'Thirty',40:'Forty',
13+
50:'Fifty',60:'Sixty',70:'Seventy',80:'Eighty',
14+
90:'Ninety'};
15+
varstr='';
16+
17+
if(num===0){
18+
return'Zero';
19+
}
20+
21+
for(vari=9;i>=0;){
22+
varmost=Math.floor(num/Math.pow(10,i));
23+
num=num%Math.pow(10,i);
24+
25+
if(i===9&&most>0){
26+
str+=dict[most]+' Billion ';
27+
}
28+
29+
if(i===6&&most>0){
30+
str+=threeDigits(most,dict)+' Million ';
31+
}
32+
33+
if(i===3&&most>0){
34+
str+=threeDigits(most,dict)+' Thousand ';
35+
}
36+
37+
if(i===0&&most>0){
38+
str+=threeDigits(most,dict);
39+
}
40+
41+
if(num===0){
42+
str=str.trim();
43+
break;
44+
}
45+
46+
i-=3;
47+
}
48+
49+
returnstr;
50+
};
51+
52+
varthreeDigits=function(num,dict){
53+
varhundred=0;
54+
varrem=0;
55+
varstr='';
56+
57+
if(num===0){
58+
return'';
59+
}elseif(num<100){
60+
str=twoDigits(num,dict);
61+
}else{
62+
hundred=Math.floor(num/100);
63+
str=dict[hundred]+' Hundred';
64+
rem=num%100;
65+
66+
if(rem<=10&&rem>0){
67+
str+=' '+dict[rem];
68+
}elseif(rem>10){
69+
str+=' '+twoDigits(rem,dict);
70+
}
71+
}
72+
73+
returnstr;
74+
};
75+
76+
vartwoDigits=functiontwoDigits(num,dict){
77+
varstr='';
78+
varleast=Math.floor(num%10);
79+
varmost=num-least;
80+
81+
if(least===0||num<=20){
82+
str+=dict[num];
83+
}else{
84+
str+=dict[most]+' '+dict[least];
85+
}
86+
87+
returnstr;
88+
}

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,5 @@
200200
*[132. Palindrome Partitioning II](https://leetcode.com/problems/palindrome-partitioning-ii/) -[Solution](./Hard/132-palindromePartitioningII.js)
201201
*[145. Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) -[Solution](./Hard/145-binaryTreePostorder.js)
202202
*[154.Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) -[Solution](./Hard/154-findMinimumInRotatedSortedArray-II.js)
203+
*[273. Integer to English Words](https://leetcode.com/problems/integer-to-english-words/) -[Solution](./Hard/273-integerToEnglish.js)
203204
*[287. Find the Duplicate Number](https://leetcode.com/problems/find-the-duplicate-number/) -[Solution](./Hard/287-findDuplicateNumber.js)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp