第九周
博弈论:
leetcode:1025:
public class Solution {
public boolean divisorGame(int N) {
//博弈论,最终是偶数的话就会赢
return (N&1)==0;
}
}
leetcode:319:
public class Solution2 {
public static int bulbSwitch(int n) {
//最后发现规律:结果为开根号
return (int) Math.sqrt(n);
}
}
leetcode:326
public class Solution2 {
public boolean isPowerOfThree(int n) {
//3^19=1162261467,只要是3的次幂就可以被n整除
return n>0&&1162261467%n==0;
}
}
leetcode:172
class Solution {
public int trailingZeroes(int n) {
//题目要求logn,所以使用递归
//0的个数就是2*5的对数,但是像25是两个5,所直接递归
return n==0?0:n/5+trailingZeroes(n/5);
}
}
leetcode:292
public boolean canWinNim(int n) {
//寻找规律,当我们面对n是4的倍数时就一定会输,反正就会赢
return n%4!=0;//不是4的倍数就赢
}
leetcode:877
public boolean stoneGame(int[] piles) {
//你会发现,因为亚历克斯先选,最优解总是他比较多
return true;
}
笔记: