Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example:
For num = 5
you should return [0,1,1,2,1,2]
.
Follow up:
public class Solution { public int[] countBits(int num) { int[] re=new int[num+1]; int index=1; int begin=1; long count=1; re[0]=0; while(index<=num){ for(int i=0;i<begin&&index<=num;i++){ re[index++]=re[i]+1; } begin=begin<<1; count=begin; } return re; } }最优的解法,只是通过将这种能够规律转化成公式,再加上位运算,速度优化不会太高,不过位运算十分重要。
public int[] countBits(int num) { int[] f = new int[num + 1]; for (int i=1; i<=num; i++) f[i] = f[i >> 1] + (i & 1); return f; }
1.获得int型最大值:
int getMaxInt() { //return (1 << 31) - 1;// 2147483647, 由于优先级关系,括号不可省略 //return ~(1 << 31);//2147483647 return Integer.MAX_VALUE; }
int getMinInt(){ //return 1 << 31;//-2147483648 return Integer.MIN_VALUE; }
int mulAdivTwo(int n){ return n << 1; //计算n*2 //return n >> 1;//除以2 }
int mulAdivTwoPower(int n,int m){ return n << m; //计算n*(2^m) //return n >> m; //计算n/(2^m) }
boolean isOddNumber(int n){ return (n & 1) == 1; }
6.不适用临时变量交换两个数:
a ^= b; b ^= a; a ^= b;
int abs(int n){ return (n ^ (n >> 31)) - (n >> 31); /* n>>31 取得n的符号,若n为正数,n>>31等于0,若n为负数,n>>31等于-1 若n为正数 n^0=0,数不变,若n为负数有n^-1 需要计算n和-1的补码,然后进行异或运算, 结果n变号并且为n的绝对值减1,再减去-1就是绝对值 */ }
int max(int a,int b){ return b & ((a-b) >> 31) | a & (~(a-b) >> 31); /*如果a>=b,(a-b)>>31为0,否则为-1*/ }
int min(int a,int b){ return a & ((a-b) >> 31) | b & (~(a-b) >> 31); /*如果a>=b,(a-b)>>31为0,否则为-1*/ }
boolean isSameSign(int x, int y){ //有0的情况例外 return (x ^ y) >= 0; // true 表示 x和y有相同的符号, false表示x,y有相反的符号。 }
boolean isFactorialofTwo(int n){ return n > 0 ? (n & (n - 1)) == 0 : false; /*如果是2的幂,n一定是100... n-1就是1111.... 所以做与运算结果为0*/ }
int quyu(int m,int n){//n为2的次方 return m & (n - 1); /*如果是2的幂,n一定是100... n-1就是1111.... 所以做与运算结果保留m在n范围的非0的位*/ }
int getBit(int n, int m){ return (n >> (m-1)) & 1; }
int setBitToOne(int n, int m){ return n | (1 << (m-1)); /*将1左移m-1位找到第m位,得到000...1...000 n在和这个数做或运算*/ }
int setBitToZero(int n, int m){ return n & ~(1 << (m-1)); /* 将1左移m-1位找到第m位,取反后变成111...0...1111 n再和这个数做与运算*/ }
int calc(int n){ if( n & (n-1) ==0 ) return n; n |= n>>1; n |= n>>2; n |= n>>4; n |= n>>8; n |= n>>16; return n+1; }
http://blog.163.com/126_xialin.126/blog/static/279086312014117111557771/