摘要: 快速幂。处理当指数为负数时的情景,还要注意int32的数据范围。 class Solution { public double myPow(double x, int n) { if(x==0) return 0; double res=1; long b=n; if(n<0) { x=1/x; b 阅读全文
posted @ 2021-03-13 13:58 wsshub 阅读(49) 评论(0) 推荐(0)
摘要: 这道题绝了,虽然是简单题,但是可以暴露不少问题。 // you need to treat n as an unsigned value public int hammingWeight(int n) { int cnt =0; int tmp; while(n!=0){//!!不可以写成n>0,因 阅读全文
posted @ 2021-03-13 02:14 wsshub 阅读(52) 评论(0) 推荐(0)
摘要: 这道题最优解是贪心+快速幂。 但贪心算法还必须清楚背后的数学定理才会有思路,没意思。 学习如何用大数BigInteger和快速幂的代码即可。(虽然我没用到快速幂) 注意必须对数组手动初始化,因为不会帮你默认初始化,不手动初始化在n=10时会出错嗯。 1 import java.math.*; 2 c 阅读全文
posted @ 2021-03-13 01:28 wsshub 阅读(50) 评论(0) 推荐(0)