public class Solution {
public int HammingDistance(int x, int y) {
int distance = 0;
string sX = Convert.ToString(x, 2);
string sY = Convert.ToString(y, 2);
int maxLength = Math.Max(sX.Length, sY.Length);
//填充0,使两个字符串右对齐
sX = sX.PadLeft(maxLength, ‘0‘);
sY = sY.PadLeft(maxLength, ‘0‘);
for ( int i = 0 I MaxLength I ++) {
if (sX[i] != sY[i]) {
distance++;
}
}
return distance;
}
}
From for notes (Wiz)
end of the loop, only one number is not 0, the other number is 0, and at this point 0 corresponds to all the binary is 0, at this time as long as the number of the calculation is not 0 of binary 1 of the number.Improvement: The above uses three while, with one is enough:1 classSolution {2 Public:3 intHammingdistance (intXinty) {4 intdis =0;5 while(x | |y) {//or operation, as long as there is a not 0 for this loop6Dis + = (x 1) ^ (Y 1);7X >>=1;8Y >>=1;9 }Ten
The Hamming distance between and integers is the number of positions at which, the corresponding bits is different.Given integers x y and, calculate the Hamming distance.PS: To find the Hamming distance.Idea: The number of x and y binary xor 1Publicclasssolution{publicinthammingdistance (Intx, inty) {//stringx1= Integer.tobinarystring (x); //stringy1= Integer.tobinarystring (y);inttem=x^y; intcount=0;stringstr =integer.tobinarystring (TEM); for (Inti=0;iLeetcode
Problem Description:The Hamming distance between two integers refers to the number of different positions of these two numbers corresponding to bits.Give two integers x and y calculate the Hamming distance between them.Attention:0≤ x , y Example:input: x = 1, y = 4 output: 2 Explanation:1 (0 0 0 1 ) 4 (0 1 0 0) ↑ ↑ The above arrows pointed to the different positions of the corresponding bits.Method: 1 class Solution (object): 2 def Hammingdistance (self, x, y): 3 Span style
The Hamming distance between and integers is the number of positions at which, the corresponding bits is different.Given integers x y and, calculate the Hamming distance.Train of thought: Count two binary bitwise compare the number of different bits, first XOR, then the number of binary 1 of XOR. public class solution { public int hammingdistance (int x, int y) { int xor = x ^ y; int distance = 0; while (xor! = 0 ++; XOR = (xor-1); // " return distance; }}LeetCode-
fibs number of digits
problem:461
Time limit:1000ms
Memory limit:65536k
Description
The generalized Fibonacci sequence is defined as follows:
R0=a; R1= b; rn= uRn-1 + vRn-2 (n >= 3) Here a,b,u,v are real numbers, called RN for the generalized Fibonacci sequence. Now let you calculate the number of bits of the generalized Fibonacci sequence.
[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
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
Leetcode 461. Hamming Distance
Topics
The Hamming distance between and integers is the number of positions at which, the corresponding bits is different.
Given integers x and y, calculate the Hamming distance.
Solution:
Class Solution {public
:
int hammingdistance (int x, int y) {
int z = x^y;
int ans = 0;
while (z) {
ans++;
Z = z-1;
}
return ans;
}
;
Writing this blog is mainl
1. The array name is a pointer constant representing the first address of the array space, and the constant assignment is not allowed in the program.such as int a[]; A is the array name, which represents the pointer constant of the first address of the array controlA = 0; is wrong and does not allow assigning values to pointer constants2. You can point to a constant by the pointer, but you cannot modify the contents of the constant through the pointersuch as: Char *p;p = "Testing"*p = ' 0 ';3.So
most fundamental factor for the success of the postgraduate entrance exam is your insufficient internal power.Maybe you don't believe it. I will give you an example of my side. my colleagues and I are both admitted to external schools. We are all very diligent because of fierce competition. we use almost the same teaching materials, and our work habits are the same. I didn't go to the tutoring class, but at last I had a clear gap with him in the scor
Asp.net Microsoft certification new exam question bank and answer 1, asp.net exam question bank
1. You have created an ASP.net application that runs on the WEB site of TK. Your application contains 100 WEB pages. If you want to configure your application, a custom error message is displayed when an HTTP code error occurs. At the same time, you want to record the program errors to the log. If you want to ach
Detailed explanation of soft exam afternoon questions --- data flow diagram design, soft exam Data Flow
Five main questions in the afternoon of the soft exam over the years are data flow diagram design, database design, uml diagram, algorithm, and design pattern. Starting from today's blog, I want to share with you the content of the soft
◆ Learn about postgraduate entrance exams
Postgraduate information can be divided into public information and semi-public information based on the degree of openness. Public information refers to information publicly transmitted through various channels, including the National Graduate admission policies, professional directories, and enrollment brochures. Semi-public information is usually not made public, but candidates can still learn through special channels, such as the content and focus o
2015 Hebei degree English exam true answer "4800 exam after pay 53855"One, the first layer for hard studyMention the study to say "head cantilever, Cone Thorn shares", "hard, hard, and hard." Students at this level feel that learning is boring, learning is a forced behavior for them, not to learn the fun. For a long time, the study inevitably produced a sense of fear, thus breeding the mood of weariness, th
L1-005. Exam seat number, l1-005 exam seat number
Each PAT candidate is assigned two seat numbers when taking the test. One is the test seat and the other is the test seat. Under normal circumstances, the examinee receives the test seat number before admission. After the seat enters the test status, the system displays the test seat number of the examinee. During the test, the examinee needs to change to th
Another Alibaba pen exam and another Alibaba exam
The next written examination... Various busy times...
1. If the key code K of an integer is hashed to a hash list with N slots, which of the following hash functions are good hash functions ()
A. h (k) = K/n B. h (k) = 1 C. h (k) = k mod N
D. h (k) = (K + random (N) mod N, random (N) returns an integer ranging from 0 to N.
2. In the following sorting algorit
Guang lianda beiyou pen exam, Guang lianda beiyou exam
Guang lianda beiyou lecture pen questions
Programming questions
1. Compared with arrays, linked lists do not have the following advantages: small storage space.
2. A group of people of different ages, find the minimum age difference (absolute value), the time complexity is less than O (n ^ 2)
My idea is: sort the ages (from small to large) in a quick so
Exam: Use two stacks to implement the queue, and use the exam to implement the queue
# Include
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
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.