We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。
Input: [1,3,5,6], 5 Output: 2
Input: [1,3,5,6], 2 Output: 1
Input: [1,3,5,6], 7 Output: 4
Input: [1,3,5,6], 0 Output: 0
The text was updated successfully, but these errors were encountered:
/** * @param {number[]} nums * @param {number} target * @return {number} */ var searchInsert = function(nums, target) { const len = nums.length; if (len === 0 || nums[len - 1] < target) { return len; } let left = 0; let right = nums.length - 1; while (left < right) { const mid = (left + right) >> 1; if (nums[mid] < target) { left = mid + 1; } else { right = mid; } } return left; };
function searchInsert(nums: number[], target: number): number { const len = nums.length; if (len === 0 || nums[len - 1] < target) { return len; } let left = 0; let right = nums.length - 1; while (left < right) { const mid = (left + right) >> 1; if (nums[mid] < target) { left = mid + 1; } else { right = mid; } } return left; }
Sorry, something went wrong.
No branches or pull requests
35. Search Insert Position
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
你可以假设数组中无重复元素。
Example 1
Example 2
Example 3
Example 4
The text was updated successfully, but these errors were encountered: