LeetCode Factorial Trailing Zeroes, Excel Sheet Column Number

Factorial Trailing Zeroes

题意:给一个数n,问n!的末尾有几个0

第一反应是0只有2、5相遇能出一个0,所以n有几个5的倍数似乎就是答案了。

(n!有k个5,肯定有k个2,能凑出相对应的0)

但是如果是像25的怎么办?他能凑出100,两个0,;所以前面单单5的倍数不够,还应该是5^i,将这个i加起来。

代码如下:

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

Excel Sheet Column Number

  题意:给一个字符串,问它在Excel表格当中的列号

按照进制,从右往左,第i位,就是26^i-1,再乘当前位数的数值,对应就是A-1,B-2,……Z-26

import java.util.TreeMap;

public class Solution {
	public int titleToNumber(String s) {
		TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
		map.put('A', 1);
		map.put('B', 2);
		map.put('C', 3);
		map.put('D', 4);
		map.put('E', 5);
		map.put('F', 6);
		map.put('G', 7);
		map.put('H', 8);
		map.put('I', 9);
		map.put('J', 10);
		map.put('K', 11);
		map.put('L', 12);
		map.put('M', 13);
		map.put('N', 14);
		map.put('O', 15);
		map.put('P', 16);
		map.put('Q', 17);
		map.put('R', 18);
		map.put('S', 19);
		map.put('T', 20);
		map.put('U', 21);
		map.put('V', 22);
		map.put('W', 23);
		map.put('X', 24);
		map.put('Y', 25);
		map.put('Z', 26);

		int len = s.length();
		int ret = 0;
		int digit = 1;
		for (int i = 1; i <= len; i++) {
			ret += map.get(s.charAt(len - i)) * digit;
			digit *= 26;
		}
		return ret;
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值