Home > Back-end >  For bosses to help have a look at this code
For bosses to help have a look at this code

Time:12-03

Title:
Given a sorted array and a target, find the target in an array, and returns the index, if the target doesn't exist in the array, returns it will be the location of the insert according to the order

You can assume that no repeating elements in the array,

Example 1:
Input:,3,5,6 [1], 5
Output:
2
Example 2:
Input:,3,5,6 [1], 2
Output: 1

Example 3:
Input:,3,5,6 [1], 7
Output: 4

Example 4:
Input:,3,5,6 [1], 0
Output: 0

Code:
Nums int searchInsert (int * and an int numsSize, int target) {
Int left=0, right=numsSize, mid;// right here why not assigned numsSize - 1?
While (left & lt; Right) {
Mid=left + - left (right)/2;
If (target & lt;=nums [mid]) {// here & lt;==can you changed?
Right=mid;// since a + 1 below, that here - 1: why not?
} else {
Left=mid + 1;
}
}
return left;
}
can finally help me clarify this binary search code idea?

CodePudding user response:

This algorithm, their best step through it, understanding is deep,

CodePudding user response:

The second problem, cannot change is equal to, if not equal elements that can't not binary

CodePudding user response:

Nums int searchInsert (int * and an int numsSize, int target) {
//what do you want to understand the two variables left here right it represents the most on the left of the array (that is, the lowest value) and most the right side (the highest) array subscript
Int left=0, right=numsSize, mid; /=numsSize you can understand as you type of values such as,3,5,6 {1} plus 7 one you input the right subscript 4 so if you will value equals numsSize - 1 will lead to the return value is 3 instead of 4
While (left & lt; Right) {
Mid=left + - left (right)/2;
If (target & lt;=nums [mid]) {//step on the mid assignment is in the middle of the array value namely nums (mids) here & lt;=nums (mid) is directly insert you input the target value compared with the value of intermediate
Right=mid; //if you insert a value less than the right side of the median will array right (highest) subscript assignment into mid of intermediate value namely binary
} else {
Left=mid + 1; //if you insert value is greater than the left of an array of intermediate value will be left (lowest) subscript assignment into mid of intermediate value namely binary
}
} after the left and the right values to assign a value that is used to length of the array into a length of 4 2 also can take values in the middle of the while loop
return left;
}
binary understandingFor example:
Digital: guess to guess guess a value within 100
You can guess first 50 (i.e., target)
- "<-" you lose 50 directly below the value (that is, the (left=mid + 1) continue to guess) between 51-100
- "is greater than or equal to -" you lose more than 50 value directly (that is, between 0 to 50=mid (right) continue to guess)
Have been circulating
- "<-" you lose directly below the 75 value (that is, the (left=mid + 1) continue to guess between 76-100)
- "is greater than or equal to -" you lose more than 25 values directly (i.e., in the (right=mid) continue to guess between 0 to 25)
. The cycle until the guess


CodePudding user response:

The
reference 3 floor Li Mu general response:
int searchInsert (int * nums, int numsSize, int target) {
//what do you want to understand the two variables left here right it represents the most on the left of the array (that is, the lowest value) and most the right side (the highest) array subscript
Int left=0, right=numsSize, mid; /=numsSize you can understand as you type of values such as,3,5,6 {1} plus 7 one you input the right subscript 4 so if you will value equals numsSize - 1 will lead to the return value is 3 instead of 4
While (left & lt; Right) {
Mid=left + - left (right)/2;
If (target & lt;=nums [mid]) {//step on the mid assignment is in the middle of the array value namely nums (mids) here & lt;=nums (mid) is directly insert you input the target value compared with the value of intermediate
Right=mid; //if you insert a value less than the right side of the median will array right (highest) subscript assignment into mid of intermediate value namely binary
} else {
Left=mid + 1; //if you insert value is greater than the left of an array of intermediate value will be left (lowest) subscript assignment into mid of intermediate value namely binary
}
} after the left and the right values to assign a value that is used to length of the array into a length of 4 2 also can take values in the middle of the while loop
return left;
}
binary understandingFor example:
Digital: guess to guess guess a value within 100
You can guess first 50 (i.e., target)
- "<-" you lose 50 directly below the value (that is, the (left=mid + 1) continue to guess) between 51-100
- "is greater than or equal to -" you lose more than 50 value directly (that is, between 0 to 50=mid (right) continue to guess)
Have been circulating
- "<-" you lose directly below the 75 value (that is, the (left=mid + 1) continue to guess between 76-100)
- "is greater than or equal to -" you lose more than 25 values directly (i.e., in the (right=mid) continue to guess between 0 to 25)
. The cycle until the guess


why is finally return left rather than return right?
  • Related