[Lintcode]181. Flip Bits

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 (11111), 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

思路

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

原文地址:https://www.cnblogs.com/siriusli/p/10358604.html

时间: 2024-08-12 13:16:43

[Lintcode]181. Flip Bits的相关文章

181. Flip Bits【LintCode, by java】

Description Determine the number of bits required to flip if you want to convert integer n to integer m. Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 解题:比较两个整数对应的二进制数,共有多少位不同.注意,负数也包含在内.">>>"

LintCode刷题笔记--Flip Bits

Flip Bits: 题目:Determine the number of bits required to flip if you want to convert integer n to integer m. 解题思路: 给出两个数字a和b,返回两个数字中需要转换的内容这道题主要是考察位运算的几种操作:1.首先使用异或运算来确定在哪些位置上两个数字的二进制位不一样的数字上都填上1,得到bit=a^b. 2. 之后在与1进行与运算,如果bit的最后一位是1那么就得到1,否则为03. 将bit向

Lintcode: Flip Bits

Determine the number of bits required to flip if you want to convert integer n to integer m. Have you met this question in a real interview? Yes Example Given n = 31 (11111), m = 14 (01110), return 2. Note Both n and m are 32-bit integers. This is to

Flip Bits

Determine the number of bits required to flip if you want to convert integer n to integer m. Notice Both n and m are 32-bit integers. Example Given n = 31 (11111), m = 14 (01110), return 2. 1 class Solution { 2 /** 3 *@param a, b: Two integer 4 *retu

debugfs linux rm 删除 恢复 Attempt to read block from filesystem resulted in short read while opening filesystem

sudo apt-get install foremost w debugfs: ? Available debugfs requests: show_debugfs_params, params Show debugfs parameters open_filesys, open Open a filesystem close_filesys, close Close the filesystem freefrag, e2freefrag Report free space fragmenta

【ThinkingInC++】73、深入理解模板

第五章 深入理解模板 5.1 模板参数 关于bitset bitset就是可以存放二进制的容器. 对于bitset的主要操作有: (constructor) Construct bitset (public member function)    //构造bitset..   格式bitset<长度>  名字 applicable operators Bitset operators (functions)          //可以直接对bitset容器进行二进制操作,如^,|,~,<

Flip the Bits(思维)

You are given a positive integer n. Your task is to build a number m by flipping the minimum number of bits in the binary representation of n such that m is less than n (m < n) and it is as maximal as possible. Can you? Input The first line contains

Lintcode: Update Bits

Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j) Have you met this question in a real interview? Yes Ex

lintcode-easy-Flip Bits

Determine the number of bits required to flip if you want to convert integer n to integer m. Example Given n = 31 (11111), m = 14 (01110), return 2. Note Both n and m are 32-bit integers. class Solution { /** *@param a, b: Two integer *return: An int