计算出n阶乘中尾部零的个数。 比如11!=39916800, return 2
有没有更省时的解题方法?
long long trailingZeros(long long n) { // write your code here, try to do it without arithmetic operators. unsigned long num=0; while(n>0){ num+=n/5; n=n/5; } return num; }
知识盲点 感觉已经很极致了
个人认为: 这个已经是很快的了吧?到log(n)了? 如果从代码上面优化的话可以换一下顺序,省一次乘法: n = n/5; num += n; (这个思路是找有多少个约数5吧?)
是时候分析一下时间复杂度了 ?
© 2018-2023 0xFFFF