码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 172: Factorial Trailing Zeroes

时间:2014-12-30 10:05:31      阅读:190      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!