LeetCode 阶乘后的零算法题解 All In One
factorial
阶乘后的零
原理 图解
- 实现 factorial
- 计算后面 0 的个数,除
0!
本身的 0
阶乘!
https://www.shuxuele.com/numbers/factorial.html
https://www.shuxuele.com/definitions/factorial.html
172. 阶乘后的零
- Factorial Trailing Zeroes
function trailingZeroes(n: number): number {
if(n === 0) return 0;
function factorial(n, multi = 1) {
if(n === 1) return multi;
multi *= n;
return factorial(n - 1, multi)
}
const num = factorial(n);
const arr:string[] = `${num}`.split(``).reverse();
let len: number = 0;
for(const item of arr) {
if(item === '0') {
len += 1;
} else {
break;
}
}
return len;
};
// 大数相乘 ???
// testCase factorial(30) = 2.652528598121911e+32 ❌
// 通过测试用例:21 / 500
// 输入:30 输出:0 预期结果:7
/*
(() => {
const log = console.log;
function factorial(n, multi = 1) {
if(n === 0) {
return 0;
}
if(n === 1) {
return multi;
}
multi = n * multi;
return factorial(n - 1, multi);
};
const arr = [...new Uint8Array(100)].map((item, i) => i);
// for (const [item, i] of arr.entries()) {
// log(`item, i =`, item, i);
// }
for (const item of arr) {
log(`testCase factorial(${item}) =`, factorial(item));
if(`${factorial(item)}`.includes(`e`)) {
break;
}
}
// factorial(50)
// 3.0414093201713376e+64
// 阶乘, `科学计数法` `e` bug ❌
})();
*/
/*
$ npx ts-node ./172\ factorial-trailing-zeroes.ts
testCase factorial(0) = 0
testCase factorial(1) = 1
testCase factorial(2) = 2
testCase factorial(3) = 6
testCase factorial(4) = 24
testCase factorial(5) = 120
testCase factorial(6) = 720
testCase factorial(7) = 5040
testCase factorial(8) = 40320
testCase factorial(9) = 362880
testCase factorial(10) = 3628800
testCase factorial(11) = 39916800
testCase factorial(12) = 479001600
testCase factorial(13) = 6227020800
testCase factorial(14) = 87178291200
testCase factorial(15) = 1307674368000
testCase factorial(16) = 20922789888000
testCase factorial(17) = 355687428096000
testCase factorial(18) = 6402373705728000
testCase factorial(19) = 121645100408832000
testCase factorial(20) = 2432902008176640000
testCase factorial(21) = 51090942171709440000
testCase factorial(22) = 1.1240007277776077e+21
*/
LeetCode 题解 / LeetCode Solutions
https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos
YouTube & LeetCode 力扣官方算法题解视频列表
https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE
https://github.com/xgqfrms/leetcode/issues/14
https://www.youtube.com/results?search_query=+Leetcode+172
<iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/3Hdmv_Ym8PI?start=40" title="YouTube video player" width="560"></iframe>~~
https://github.com/neetcode-gh/leetcode/blob/main/javascript/172-Factorial-Trailing-Zeroes.js
https://github.com/neetcode-gh/leetcode/blob/main/typescript/172-Factorial-Trailing-Zeroes.ts
~~
类似问题
LeetCode
https://leetcode.com/problems//
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载
标签:multi,factorial,题解,testCase,item,https,阶乘,com,LeetCode From: https://www.cnblogs.com/xgqfrms/p/16767496.html