---
# System prepended metadata

title: Leetcode 35. Search Insert Position
tags: [Leetcode, Binary Search]

---

## 題解


```python=
class Solution:
    def searchInsert(self, nums: List[int], target: int) -> int:
        length = len(nums)
        left = 0
        right = length - 1

        if target > nums[-1]:
            return length

        while left < right:
            mid = left + (right - left) // 2
            if nums[mid] >= target:
                right = mid
            else:
                left = mid + 1
        
        return left

```