Factorial Trailing Zeroes , 172题,可能是我没读清楚,还报错。

package com.x.xyz;

public class HelloWorld {

	public static void main(String[] args) {
		System.out.println("Star !");
		System.out.println(trailingZeroes(0));
		System.out.println(trailingZeroes(1));
		System.out.println(trailingZeroes(2));
		System.out.println(trailingZeroes(3));
		System.out.println(trailingZeroes(4));
		System.out.println(trailingZeroes(5));
		System.out.println(trailingZeroes(6));
		System.out.println(trailingZeroes(7));
	}

	public static int trailingZeroes(int n) {
		int sum = 10;
//		if(0 == n) {
//			return 1;
//		}
//		for(int i=1; i<n; i++) {
//			sum = sum * 10;
//		}
//		return sum;
		if(0 == n) {
			return 1 * sum;
		}else{
			return trailingZeroes(n-1) * sum;
		}
	}
}

Star !

10

100

1000

10000

100000

1000000

10000000

100000000

时间: 2024-10-13 16:54:21

Factorial Trailing Zeroes , 172题,可能是我没读清楚,还报错。的相关文章

Factorial Trailing Zeroes 172

题目描述: 给出一个integer n,计算n!结尾0的个数 题目分析: 考虑暴力,计算n!统计最后面0的个数.先不说数字溢出,其次n是一个integer ,O(n)复杂度超时 我们接着考虑,产生0的情况 只有包含因子5的数乘以一个偶数会在结尾产生0(5*2,15*2,75*2),因为偶数的个数大于因子包含5的数的个数,那么我们只要考虑包含因子5的数: 但是是不是包含因子5的数一定只会在结尾产生一个0呢,对于25*4提供两个0,这是因为25,包含两个因子5(5*5):同样125会提供3个0: 这

172 Factorial Trailing Zeroes 过了。英文真不好读啊。

package com.x.xyz; public class HelloWorld { public static void main(String[] args) { System.out.println("Star !"); System.out.println(trailingZeroes(10)); System.out.println(trailingZeroes(125)); System.out.println(trailingZeroes(25)); System.o

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)

172. 阶乘后的零 LeetCode172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. 示例 1: 输入: 3 输出: 0 解释: 3! = 6, 尾数中没有零. 示例 2: 输入: 5 输出: 1 解释: 5! = 120, 尾数中有 1 个零. 说明: 你算法的时间复杂度应为 O(log n) . Java 实现 class Solution { // 递归思路 public static int trailingZe

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

LeetCode Day4——Factorial Trailing Zeroes

1 /* 2 * Problem 172: Factorial Trailing Zeroes 3 * Given an integer n, return the number of trailing zeroes in n!. 4 * Note: Your solution should be in logarithmic time complexity. 5 */ 6 7 /* 8 * Solution 1 9 * 对于每一个数字,累计计算因子10.5.2数字出现的个数,结果等于10出现的

Factorial Trailing Zeroes

Factorial Trailing Zeroes Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (M) Number of Digit One n!的结果中0的个

【LeetCode】Factorial Trailing Zeroes (2 solutions)

Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Credits:Special thanks to @ts for adding this problem and creating all test cases. 对n!做质因数分解n!=2x*

LeetCode 172 Factorial Trailing Zeroes(阶乘后的零)(*)

翻译 给定一个整型n,返回n!后面的零的个数. 注意:你的解决方案应该在log时间复杂度内. 原文 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 起初我看题目的时候没太注意,还以为就是求n这个数后面的零而已,虽然心想不会这么简单吧--就写了一份代码提交了,结果WA提示我5的话应该返回1,