[leetCode刷题笔记]2017.04.07

168. Excel Sheet Column Title


public class Solution {
    public String convertToTitle(int n) {
        StringBuilder sb = new StringBuilder();
        
        while (n != 0) {
            char ch = (char) ((n - 1) % 26 + 65);
            n = (n - 1) / 26;
            sb.append(ch);
        }
        return sb.reverse().toString();
    }
}

171. Excel Sheet Column Number


public class Solution {
    public int titleToNumber(String s) {
        int n = s.length();
        int output = 0;
        for (int i = 0; i < n; i++) {
            int add = 1 + s.charAt(i) - 'A';
            output += add * Math.pow(26, n - 1 - i);
        }
        return output;
    }
}

172. Factorial Trailing Zeroes

先求n大于5的几次方k,例如:n=26,k = 2 (5*5)   n=126 k = 3 (5*5*5)

然后再算这里面有几个5 25 125 .。。。最后一加就好了。。。

public class Solution {
    public int trailingZeroes(int n) {
        if (n < 5) {
            return 0;
        }
        int k = 0;
        int d = n;
        while (d >= 5) {
            k += 1;
            d /= 5; 
        }
        int output = 0;
        for (int i = 1; i <= k; i++) {
            output += n / Math.pow(5, i);
        }
        return output;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值