题目来源:
leetcode题目,网址:2595. 奇偶位数 - 力扣(LeetCode)
解题思路:
位运算。将 1 左移 x (0,1,2,3......)位,若结果小于等于所给数,将其于该数进行与操作,若结果不为 0,int[x%2] 加一。
解题代码:
class Solution {
public int[] evenOddBit(int n) {
int[] res=new int[2];
int temp=1;
for(int i=0;temp<=n;i=(i+1)%2){
res[i]+= (temp&n)==0?0:1;
temp=temp<<1;
}
return res;
}
}
总结:
无官方题解。