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;
}
}