首页 > 其他 > 详细

leetcode 172: Factorial Trailing Zeroes

时间:2014-12-30 10:03:06      阅读:1947      评论:0      收藏:0      [点我收藏+]

Factorial Trailing Zeroes

Total Accepted: 28 Total Submissions: 69

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in polynomial time complexity.

[分析]

分解因子, 当且仅当 因子中出现 一对 (2,5)时, 最后结果会增加一个 trailing zero.

[注意事项]

[CODE]

public class Solution {
    public int trailingZeroes(int n) {
        if(n<1) return 0;
        long c2 = 0;
        long c5 = 0;
        for(int i=n; i>1; i--) {
            int k = i;
            while(k%5==0) { k/=5; ++c5;}
            while(k%2==0) { k/=2; ++c2;}
        }
        return (int)Math.min(c2, c5);
    }
}



leetcode 172: Factorial Trailing Zeroes

原文:http://blog.csdn.net/xudli/article/details/42262153

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!