[Leetcode/javascript] 461.Hamming Distance__java

Source: Internet
Author: User
[Leetcode/javascript] 461.Hamming Distance Topic

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0≤x, y < 231.

Example:

input:x = 1, y = 4

Output:2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
? ?

The above arrows point to positions where the corresponding bits are different. Analysis

The topic is very simple, given two 32-bit integers, ask bits a few different.

The idea is to make a two number of different or, and then calculate the number of different or out (below num) bits there are several 1.

The key is how BITS calculates the number of 1. There are two methods: the NUM and 0x1 are carried out with the operation, the result is 1, representing the right of NUM is 1, otherwise it is 0, then the NUM to the right one, circular judgment, the end condition for num===0 will num and num-1 and operation, This turns the rightmost 1 bits in num to 0 (note that the rightmost 1, rather than the rightmost bit), loops, and the end condition is num===0

Recommend the second method, the second method on the Leetcode speed is not the first fast, but also defeated the 90%+ coder, although the second slightly slower, but the first algorithm will be problematic.

The problem with the first approach: we all know that 32-bit complement integers are negative, the highest bit is 1, two negative, the result is 0, but if there is only one negative number, then the highest number of different or out of number 1, the execution of the right move operation will continue to move into bits 1, resulting in a dead loop.

So I personally recommend the second method, you can directly a number of bits 1, without judging the conditions. Code

/**
 * @param {number} x
 * @param {numbers} y
 * @return {#} */
//Use XOR/Can get a different 1 on each bit
// Subtracting an integer from 1 and doing it with itself will turn the rightmost 1 of the integer to 0.
You can also use and 1 to do with the operation, and then the number to the right one bit, but if the input is negative will be infinite loop (negative number of the right to move in the bit is 1)
var hammingdistance = function (x, y) {
  var count = 0;
  var n = x ^ y;
  while (n) {
    ++count;
    n = (n-1) & n;
  }
  return count;
};

Test
Console.log (hammingdistance (1, 4));

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.