Lintcode457. classic binary search problem, lintcode457 binary
[One click] errors reported countless times
One-sentence approach: Perform binary search for ordered Integers
Problem:
Format:
Conclusion: To clarify the relationship between the badge and the number, first define the badge, and finally return the badge
Public class Solution {
/*
* @ Param nums: An integer array sorted in ascending order
* @ Param target: An integer
* @ Return: An integer
*/
Public int findPosition (int [] nums, int target ){
// Write your code here
If (nums. length = 0 ){
Return-1;
}
Int start = 0;
Int end = nums. length-1;
Int mid;
While (start + 1 <end ){
Mid = start + (end-start)/2;
If (nums [mid] = target ){
End = mid;
} Else if (nums [mid] <target ){
Start = mid;
} Else if (nums [mid]> target ){
End = mid;
}
}
If (nums [start] = target ){
Return start;
}
If (nums [end] = target ){
Return end;
}
Return-1;
}
}
The parallel structure is edited successively. It is best to separate it:
if (nums[start] == target){ return start; } if (nums[end] == target){ return end; }