- 设计思想:
-视频讲解链接B站视频讲解
- 复杂度分析:
- 代码:
c++版本:
class Solution {
public:
/**
* the number of 0
* @param n long长整型 the number
* @return long长整型
*/
long long thenumberof0(long long n) {
// write code here
long long sum = 0;
while (n) {
sum += n / 5;
n = n / 5;
}
return sum;
}
};
Java版本:
import java.util.*;
public class Solution {
/**
* the number of 0
* @param n long长整型 the number
* @return long长整型
*/
public long thenumberof0 (long n) {
// write code here
long sum = 0;
while (n > 0) {
sum += n / 5;
n = n / 5;
}
return sum;
}
}Python版本:
#
# the number of 0
# @param n long长整型 the number
# @return long长整型
#
class Solution:
def thenumberof0(self , n ):
# write code here
sum = 0
while n > 0:
sum += (n//5)
n = n // 5
return sumJavaScript版本:
/**
* the number of 0
* @param n long长整型 the number
* @return long长整型
*/
function add(a ,b){
//取两个数字的最大长度
let maxLength = Math.max(a.length, b.length);
//用0去补齐长度
a = a.padStart(maxLength , 0);//"0009007199254740991"
b = b.padStart(maxLength , 0);//"1234567899999999999"
//定义加法过程中需要用到的变量
let t = 0;
let f = 0; //"进位"
let sum = "";
for(let i=maxLength-1 ; i>=0 ; i--){
t = parseInt(a[i]) + parseInt(b[i]) + f;
f = Math.floor(t/10);
sum = t%10 + sum;
}
if(f == 1){
sum = "1" + sum;
}
return sum;
}
function thenumberof0( n ) {
// write code here
let sum = 0;
let res = "";
while(n > 0){
res = add(res,(""+(parseInt(n / 5))));
n = parseInt(n / 5);
}
return res;///这里用字符串返回即可
}
module.exports = {
thenumberof0 : thenumberof0
};
京公网安备 11010502036488号