leetcode - 172. Factorial Trailing Zeroes
2018-02-01 13:22
267 查看
Problem:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
解释:求n阶乘最后有几个0。时间复杂度要求logn。
Solve:
因为10=2*5,如75=5*5*3,有两个5。且阶乘中2的数量肯定比5多。所以求解5的数量就行了。
(时间复杂度O(logn),AC-1ms)public int trailingZeroes(int n) {
return n==0?0:trailingZeroes(n/5);
}
// public int trailingZeroes(int n) {//直观写法
// int result = 0;
//
// while(n!=0){
// n=n/5;
// result+=n;
// }
//
// return result;
// }后记:想不到有这样的计算方法,看了讨论区才知道的。技不如人。
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
解释:求n阶乘最后有几个0。时间复杂度要求logn。
Solve:
因为10=2*5,如75=5*5*3,有两个5。且阶乘中2的数量肯定比5多。所以求解5的数量就行了。
(时间复杂度O(logn),AC-1ms)public int trailingZeroes(int n) {
return n==0?0:trailingZeroes(n/5);
}
// public int trailingZeroes(int n) {//直观写法
// int result = 0;
//
// while(n!=0){
// n=n/5;
// result+=n;
// }
//
// return result;
// }后记:想不到有这样的计算方法,看了讨论区才知道的。技不如人。
相关文章推荐
- leetcode 172. Factorial Trailing Zeroes
- 【LeetCode】172. Factorial Trailing Zeroes
- 【一天一道LeetCode】#172. Factorial Trailing Zeroes
- LeetCode 172. Factorial Trailing Zeroes
- LeetCode 172. Factorial Trailing Zeroes (阶乘末尾零的数量)
- [LeetCode] 172. Factorial Trailing Zeroes
- LeetCode 172. Factorial Trailing Zeroes
- [Leetcode] 172. Factorial Trailing Zeroes 解题报告
- leetcode 每日一题 172. Factorial Trailing Zeroes
- 172. Factorial Trailing Zeroes LeetCode
- Leetcode——172. Factorial Trailing Zeroes
- [leetcode: Python]172. Factorial Trailing Zeroes
- Leetcode:172. Factorial Trailing Zeroes(JAVA)
- LeetCode 172. Factorial Trailing Zeroes(0结尾)
- LeetCode 172. Factorial Trailing Zeroes
- Leetcode 172. Factorial Trailing Zeroes (Easy) (cpp)
- leetcode 172. Factorial Trailing Zeroes(阶乘尾后0个数)(因式分解)
- LeetCode 172. Factorial Trailing Zeroes(C++)
- [LeetCode]172. Factorial Trailing Zeroes
- [leetcode]: 172. Factorial Trailing Zeroes