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