首页 > 其他分享 >leetcode-461.汉明距离

leetcode-461.汉明距离

时间:2022-11-02 14:58:41浏览次数:77  
标签:右移 找为 int res 461 汉明 leetcode

461. 汉明距离

public int hammingDistance(int x, int y) {
        int z = x ^ y; //上下俩数都一样为0, 不一样为1, 得到一个整体
        int res = 0;
        //当z的2进制数一直右移, 不为0的时候, 找为1的数
        while(z != 0){
            res += z & 1; //找为1的数字, 有几个数字就
            z = z >> 1; //右移
        }
        return res;
    }

 

标签:右移,找为,int,res,461,汉明,leetcode
From: https://www.cnblogs.com/phonk/p/16850989.html

相关文章

  • [leetcode] The Skyline Problem
      classSolution{publicList<int[]>getSkyline(int[][]buildings){List<int[]>res=newArrayList<>();List<int[]>height=newAr......
  • LeetCode刷题记录.Day3
    长度最小的子数组题目链接209.长度最小的子数组-力扣(LeetCode)看似很简单。看完滑动窗口法的时候觉得很容易理解,时间复杂度O(n)的推导也理解。无非就是两个指针,因为题......
  • leetcode-2423-easy
    RemoveLetterToEqualizeFrequencyYouaregivena0-indexedstringword,consistingoflowercaseEnglishletters.Youneedtoselectoneindexandremovethe......
  • leetcode-2144-easy
    MinimumCostofBuyingCandiesWithDiscountAshopissellingcandiesatadiscount.Foreverytwocandiessold,theshopgivesathirdcandyforfree.Thec......
  • leetcode-1460-easy
    MakeTwoArraysEqualbyReversingSubarraysYouaregiventwointegerarraysofequallengthtargetandarr.Inonestep,youcanselectanynon-emptysubarra......
  • leetcode-257-easy
    BinaryTreePathsGiventherootofabinarytree,returnallroot-to-leafpathsinanyorder.Aleafisanodewithnochildren.Example1:Input:root=......
  • leetcode-1304-easy
    FindNUniqueIntegersSumuptoZeroGivenanintegern,returnanyarraycontainingnuniqueintegerssuchthattheyaddupto0.Example1:Input:n=5O......
  • leetcode-118-easy
    Pascal'sTriangleGivenanintegernumRows,returnthefirstnumRowsofPascal'striangle.InPascal'striangle,eachnumberisthesumofthetwonumbersdir......
  • leetcode-1137-easy
    N-thTribonacciNumberTheTribonaccisequenceTnisdefinedasfollows:T0=0,T1=1,T2=1,andTn+3=Tn+Tn+1+Tn+2forn>=0.Givenn,returnthe......
  • leetcode111-二叉树的最小深度
    111.二叉树的最小深度 这道题相比 104.二叉树的最大深度 还是难上一些的,但也不算太难。BFS/***Definitionforabinarytreenode.*structTreeNode{......