Help a friend solve a LeetCode QJ problem, leetcodeqj
Introduction
I am not competent to refresh questions. I am the best friend and colleague to take an exam on an array of questions. Maybe I can share it during an interview. Entertainment and entertainment.
The beginning of the incident is like this.
Preface
Question
In an array, we can find out two elements that do not repeat each other. Other elements appear in pairs. The returned result sequence is not required.
Let's take a look at the answer page provided by this system. Select C.
You only need to do a good job later, you can first Run the Code detection, and then Submit the Submit Solution.
I will explain my ideas below. I don't have a Goolge answer, maybe it's not the best solution. You can optimize it again.
Body
1. Thinking about the algorithm way out
The first choice is that the algorithm complexity is greater than O (n). Here we use the O (n) level routine. Here we have a mathematical attempt.
A ^ a = 0, a ^ 0 = a, a ^ B = B ^ a. => a ^ B ^ a = a ^ B = 0 ^ B = B
^ Indicates an exclusive or. Remember to study electronics, even if it is a pair of B.
It is easy for us to learn from the above computation.
A ^ a = 0, then we set all the result of int * nums above to an exclusive or, and finally get the exclusive or value of the two numbers to be searched.
Well, we need to find one of them. Assume that the final number is a, B.
The final result above is a ^ B => convert to binary code. If it is 0x001100, then a and B are not in the third and fourth binary.
Then we only need to find the first different binary digits, and then compare the values in nums with one another to get a result.
The first code is as follows:
1/** 2 * Return an array of size * returnSize. 3 * Note: The returned array must be malloced, assume caller callfree (). 4 */5 int * singleNumber (int * nums, int numsSize, int * returnSize) {6 int * nnums = malloc (sizeof (int) * 2); 7 int I, j, sum = 0, flag = 1; 8 int a = 0, B; 9 10 // first obtain all the differences or result 11 for (I = 0; I <numsSize; + + I) 12 sum ^ = nums [I]; 13 // locate the first position 14 while (! (Flag & sum) 15 flag <= 1; 16 17 for (I = 0; I <numsSize; ++ I) 18 if (flag & nums [I]) 19 a ^ = nums [I]; 20 21 nnums [0] = a; 22 nnums [1] = a ^ sum; 23 24 * returnSize = 2; 25 return nnums; 26}
Such code is common.
If the test passes, I will optimize it!
2. Simple Optimization
Here we want to optimize the code first.
1/** 2 * Return an array of size * returnSize. 3 * Note: The returned array must be malloced, assume caller callfree (). 4 */5 int * singleNumber (int * nums, int numsSize, int * returnSize) {6 int sl = 0, x, a = 0; 7 int * end = nums + numsSize; 8 int * pt = nums; 9 // get all the data variance or 10 while (pt <end) 11 sl ^ = * pt ++; 12 13 // find the first digit 14 x = sl &-sl; 15 // first find the first digit 16 while (pt> nums) {17 int t = * -- pt; 18 if (x & t)
A ^ = t; 19} 20 21 nums [0] = a; 22 nums [1] = a ^ sl; 23 * returnSize = 2; 24 return nums; 25}
There are many techniques used. For example, sl &-sl finds the position value of sequence 1. For example, sl = 0x0110 => sl &-sl => 0x0010.
Finally, let's look at the running result diagram.
The average testing time is 4 ms. The first echelon may have better algorithms. This is the case here. I have the opportunity to ask again and share it with you.
You have the opportunity to try LeetCode QJ.
Postscript
Errors are inevitable. If you have any questions, leave a message. I wish the sun is shining today. The price is too high and the days are a little difficult ,.....
Another point: 30 years ago, a Big Brother was more expensive than 5000 yuan. Now, Android mobile phones in India are shipping 170, of which 150 is postage.
I think the same is true for the house price. I have rented a house for 10 years, and then I 've got Chinese cabbage ......
Every era has always a flickering theme, slow down, and there is always a way forward after thinking,