logn 时间复杂度 计算整数 末尾0的个数

折半查找

class Solution {
public:
    int trailingZeroes(int n) {
        int len = 0;
        int tmp = n;
        while(tmp) {
            tmp /= 10;
            len++;
        }
        int sum_zero = 0;
        int high = len;
        int low = 1;

        // cout<<len<<endl;

        while(low <= high) {
            int mid = (high + low) / 2;
            int factor = int(pow(10, mid-1));
            if(n  == (n / factor) * factor ){
                sum_zero = mid - 1;
                // n /= factor;
                low = mid + 1;
            }  else {
                high = mid - 1;
            }
        }

        // if(low == high && n / int(pow(10, low-1)) == n) sum_zero++;
        return sum_zero;
    }
};
点赞