Leetcode---阶乘后的零

阶乘后的零

题目链接:阶乘后的零
这一题用求阶乘的方法必然溢出
需要探索数学技巧,我其实没有真正弄明白,技巧非常简单,求尾数中0的个数,只需求0——n中含有5这个因子的数的个数,特别的像:25,50这样的数要视为含有两个5,25=55,50=25*5

代码:
public int trailingZeroes(int n) {
		int result = 0;
		while(n!=0) {
			result+=(n/=5);
		}
		return result;
    }

Excel表列序号

题目链接:Excel表列序号
这道题其实就是一个进制转换的问题
思路就是从低位到高位依次转变为数字,从高位转不合理,因为不知道有多少位,那么相应位上的权值就是不确定的

代码:
	public int titleToNumber(String s) {
		
        char[] c = s.toCharArray();
        int result = 0,weight = 1;
        for(int i=s.length()-1;i>=0;i--) {
        	result+=((c[i]-'A'+1)*weight);
        	weight*=26;
        }
		return result;
    }

猜你喜欢

转载自blog.csdn.net/tiaochewang219/article/details/85464045