(Easy) Factorial Trailing Zeros Leet Code
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
Input: 3
Output: 0
Explanation: 3! = 6, no trailing zero.
Example 2:
Input: 5
Output: 1
Explanation: 5! = 120, one trailing zero.
Note: Your solution should be in logarithmic time complexity.
Accepted
166,842
Submissions
444,468
Solution . Partially Correct. Not capable of dealing with overflow.
class Solution {
public int trailingZeroes(int n) {
String tmp = String.valueOf(FACTORIAL(n));
System.out.print(tmp);
int count = 0;
for(int i = tmp.length()-1; i >0 ; i--){
if(tmp.charAt(i)=='0'){
count++;
}
else{
break;
}
}
return count;
}
public long FACTORIAL(int n){
long res = n;
do{
n = n-1;
res = res* n;
}
while (n>1);
return res;
}
}
Solution: Math;
class Solution {
public int trailingZeroes(int n) {
if(n ==0){
return 0;
}
return n/5+ trailingZeroes(n/5);
}
}
https://www.geeksforgeeks.org/count-trailing-zeroes-factorial-number/
First, let's think about in what situation will we get a trailing zero. We get a trailing zero whenever we multiply a non zero number by 10.
Example: 1 * 10 = 10
Since 10 is made up of multiplying 2 by 5, another way to get a trailing zero is to multiply a non zero number by 2 and 5.
Example: 1 * 2 * 5 = 10
So, to count the number of trailing zeroes we just need to figure out how many times we multiply 2 by 5.
Example: 1 * (2 * 5) = 10//one trailing zero 1 * (2 * 5) * (2 * 5) = 100//two trailing zeroes
Now let's look at factorial.
The factorial of 5 is: 1 * 2 * 3 * 4 * 5 = 120 Since we have multiplied 2 and 5 once, there is one trailing zero.
The factorial of 10 is: 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10 = 3628800 Another way to write this is: 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 2 * 5 = 3628800 As you can see, we have multiplied 2 and 5 twice, so there are two trailing zeroes.
Instead of keeping track of the number of 2s and 5s, we really just need to keep track of the number of 5s. This is because in any factorial calculation there are always going to be more multiples of 2 than 5.
Example: From the numbers 1 to 10, there are five multiples of 2 but only two multiples of 5.
Question: How many 5s are there in the factorial of 25? You may guess the answer is 25/5 = 5, however there are actually 6.
Here are all the multiples of 5 in the factorial of 25: 5, 10, 15, 20, 25 Another way to write this is: (5 * 1), (5 * 2), (5 * 3), (5 * 4), (5 * 5) As you can see, 5 is actually multiplied 6 times.
We can simplify the answer to the Factorial Trailing Zeroes question to the following: (n/5) + (n/5^2) + (n/5^3)... (n/5^x) We continue until 5^x is greater than n.
var trailingZeroes = function(n) {
let numZeroes = 0;
for (let i = 5; i <= n; i *= 5) { numZeroes += Math.floor(n / i); } return numZeroes; };
전재 대상:https://www.cnblogs.com/codingyangmao/p/11389927.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.