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 integer
     */
    public static int bitSwapRequired(int a, int b) {
        // write your code here
        int count = 0;

        for(int i = 0; i < 32; i++){
            int num1 = (a >> i) & 1;
            int num2 = (b >> i) & 1;

            if(num1 != num2)
                count++;
        }

        return count;
    }
};
时间: 2024-10-23 02:23:17

lintcode-easy-Flip Bits的相关文章

[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 int

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

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. 解题:比较两个整数对应的二进制数,共有多少位不同.注意,负数也包含在内.">>>"

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

[Lintcode easy]Longest Words

Longest Words Given a dictionary, find all of the longest words in the dictionary. Example Given { "dog", "google", "facebook", "internationalization", "blabla" } the longest words are(is) ["internati

lintcode easy Happy Number

Happy Number Write an algorithm to determine if a number is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process unt

[lintcode easy]Valid Sudoku

Valid Sudoku Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where empty cells are filled with the character .. Example The following partially filed sudoku is valid. Note A valid Sudoku board (partially filled) is no

[lintcode easy]Recover rotated sorted array

Recover Rotated Sorted Array Given a rotated sorted array, recover it to sorted array in-place. Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] Challenge In-place, O(1) extra space and O(n) time. Clarification What is rotated array? For example, the orgin

[lintcode easy]Merge Sorted Array II

Merge Sorted Array II Merge two given sorted integer array A and B into a new sorted integer array. Example A=[1,2,3,4] B=[2,4,5,6] return [1,2,2,3,4,4,5,6] Challenge How can you optimize your algorithm if one array is very large and the other is ver