加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[Lintcode]181. Flip Bits

发布时间:2020-12-14 04:25:28 所属栏目:大数据 来源:网络整理
导读:181. Flip Bits 本题难度: Easy Topic: MathBit Manipulation Description Determine the number of bits required to flip if you want to convert integer n to integer m. Example Given n = 31 (22222),m = 14 (01110),return 2. Notice Both n and m ar

181. Flip Bits

  • 本题难度: Easy
  • Topic: Math&Bit Manipulation

    Description

    Determine the number of bits required to flip if you want to convert integer n to integer m.

Example
Given n = 31 (22222),m = 14 (01110),return 2.

Notice
Both n and m are 32-bit integers.

我的代码

class Solution:
    """
    @param a: An integer
    @param b: An integer
    @return: An integer
    """
    def bitSwapRequired(self,a,b):
        # write your code here
        ff=pow(2,32)#注意负数
        a_r = (a + ff)%ff
        b_r = (b + ff)%ff
        count = 0
        while ((a_r or b_r) != 0):
            a_b = a_r % 2
            b_b = b_r % 2
            count += (a_b != b_b)
            a_r = a_r // 2
            b_r = b_r // 2
        return count

思路

  • 出错 没有考虑负数的情况

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读