LeetCode每日一题

LeetCode每日一题:阶乘后的零

2020-08-05  本文已影响0人  Patarw
 class Solution {
public int trailingZeroes(int n) {
  int index = 0;
  for(int i = 1;i <= n;i++){
     int m = i;
     while(m > 0){
        if(m % 5 == 0){
         index++;
         m = m / 5;
        }else{
            break;
        }
     }
  }
 return index;
}
}

运行过后发现还是超出了时间限制


说明我们的思路还是不够简单,所以看了下官方题解:

 public int trailingZeroes(int n) {
int count = 0;
while (n > 0) {
    count += n / 5;
    n = n / 5;
}
return count;
}

这道题说是一道算法题,但是我觉得更像是一道数学题

上一篇下一篇

猜你喜欢

热点阅读