publicclassSolution{// you need to treat n as an unsigned valuepublicinthammingWeight(int n){returnInteger.bitCount(n);}}publicclassSolution{// you need to treat n as an unsigned valuepublicinthammingWeight(int n){int count =0;while(n!=0){
count++;
n = n&(n-1);}return count;}}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/classSolution{publicbooleanisSymmetric(TreeNode root){if(root ==null)returntrue;returnisSubSym(root.left , root.right);}privatebooleanisSubSym(TreeNode node1 ,TreeNode node2){if(node1 ==null&& node2 ==null)returntrue;if(node1 ==null|| node2 ==null)returnfalse;return node1.val == node2.val &&isSubSym(node1.left , node2.right)&&isSubSym(node1.right , node2.left);}}
/**
底层运算
*/classSolution{publicintadd(int a,int b){while(b!=0){int c =(a & b)<<1;//c是进位
a^=b;//a为非进位和
b = c;}return a;}}