Hello, I'm senior brother Wu.
Unknowingly, by the end of the year, I don't know how many plans you have completed at the beginning of the year. Anyway, I intend to change [2021 plan] into [2022 plan].
At the end of the year, it means the new year, and the new year has come to the peak of job hopping.
If you plan to change jobs at that time, you need to prepare now, otherwise you will still be in place in the first half of 2022.
As for interview preparation, you still need to read or recite the eight part essay most of the time, and you should understand it thoroughly. Otherwise, the interviewer will easily fall into the pit when he asks detailed questions, such as some algorithm questions.
Today, I'd like to share the error prone details of the two algorithms. These are the actual cases of students in the algorithm training camp, which are of great reference significance.
1. The equal sign is missing
First of all, let's show you a video in the second phase algorithm training camp, which is about quick sorting. http://mpvideo.qpic.cn/0bc3taabyaaaraapknujzvqvbggddsmaahaa.f10002.mp4? dis_ k=7e3407dd6315ba8da2986bc25cba9e86&dis_ t=1637211684&vid=wxv_ 2127951233297465351&format_ id=10002&support_ redirect=0&mmversion=false
Don't brag. After you carefully watch this video, you can write the classic writing method of quick sorting code.
If you make a mistake, it may be caused by this detail problem.
This is a question that students can't solve. Why will they timeout without the equal sign?
It's simple. It'll go into an endless cycle.
This question is inconvenient to describe in words, so I specially made an animation to demonstrate it during live Q & A.
Through this animation, we can see that if the equal sign is missing, in this Code:
// right stops moving only when it encounters an element smaller than pivot // At this point, right points to an element smaller than pivot, which is not in its place while( left < right && nums[right] > pivot ){ // If the element pointed to by right is greater than pivot, then // right keeps moving to the left right--; } // Assign num [left] to num [right] // After this operation, the element smaller than pivot is moved to the left nums[left] = nums[right];
Once the element value pointed to by right is equal to pivot, the element value will be assigned to the element pointed to by left.
Then come to the following code:
// The movement is stopped only when it is greater than pivot left // At this point, left points to an element larger than pivot, which is not in its place while( left < right && nums[left] < pivot){ // If the element pointed to by left is less than pivot, then // left keeps moving to the right left++; } // Assign num [right] to num [left] // After this operation, the element larger than pivot is moved to the right nums[right] = nums[left];
At this time, the element value pointed to by left is also equal to pivot, so this element value will be assigned to the element pointed to by right.
In this way, there is a scene of repeated horizontal jump, falling into a dead cycle.
2. Or order problem
This is a detail problem in LeetCode No. 34, finding the first and last positions of elements in the sorted array.
Her code is 99% the same as the code mentioned in the course, except that the two lines of code are different, but the problem lies in these two lines of code.
The correct logic should be like this.
// And there is no element to the right of the middle position mid, that is, the middle position mid is the end position of the current interval // Or the latter element of the mid position is greater than the target value target // Indicates that the mid points to the end of the target if(mid == nums.length - 1 || nums[mid + 1] > target){ // The mid points to the end of the target and returns this result return mid; }
But she wrote it like this.
// And there is no element to the right of the middle position mid, that is, the middle position mid is the end position of the current interval // Or the latter element of the mid position is greater than the target value target // Indicates that the mid points to the end of the target if(nums[mid + 1] > target || mid == nums.length - 1 ){ // The mid points to the end of the target and returns this result return mid; }
This results in an out of bounds error prompt.
If the value of mid is the length of num minus one.
Then, in the original code, mid = = num.length - 1 will be executed first, instead of num [mid + 1] > target.
Once the order is reversed, nums [mid + 1] > target will be executed first, and the array is out of bounds.
This problem is very simple, but many times I can't see it, but others can see it at all, so I specially explained it to her by voice.
summary
The above two details may be ridiculous in some people's eyes: such a simple place will make mistakes??!
But this is the fact. Many algorithm problems are not difficult in themselves. The difficulty lies in the details. If there is no help, these details will not be understood for several days.
Once it is pointed out by others, you can figure it out in a few minutes. This may be a purpose of learning together: problems can be discussed and learning efficiency can be improved.