|
| 1 | +<h2>35. Search Insert Position</h2><h3>Easy</h3><hr><div><p>Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.</p> |
| 2 | + |
| 3 | +<p>You must write an algorithm with <code>O(log n)</code> runtime complexity.</p> |
| 4 | + |
| 5 | +<p> </p> |
| 6 | +<p><strong>Example 1:</strong></p> |
| 7 | +<pre><strong>Input:</strong> nums = [1,3,5,6], target = 5 |
| 8 | +<strong>Output:</strong> 2 |
| 9 | +</pre><p><strong>Example 2:</strong></p> |
| 10 | +<pre><strong>Input:</strong> nums = [1,3,5,6], target = 2 |
| 11 | +<strong>Output:</strong> 1 |
| 12 | +</pre><p><strong>Example 3:</strong></p> |
| 13 | +<pre><strong>Input:</strong> nums = [1,3,5,6], target = 7 |
| 14 | +<strong>Output:</strong> 4 |
| 15 | +</pre><p><strong>Example 4:</strong></p> |
| 16 | +<pre><strong>Input:</strong> nums = [1,3,5,6], target = 0 |
| 17 | +<strong>Output:</strong> 0 |
| 18 | +</pre><p><strong>Example 5:</strong></p> |
| 19 | +<pre><strong>Input:</strong> nums = [1], target = 0 |
| 20 | +<strong>Output:</strong> 0 |
| 21 | +</pre> |
| 22 | +<p> </p> |
| 23 | +<p><strong>Constraints:</strong></p> |
| 24 | + |
| 25 | +<ul> |
| 26 | + <li><code>1 <= nums.length <= 10<sup>4</sup></code></li> |
| 27 | + <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li> |
| 28 | + <li><code>nums</code> contains <strong>distinct</strong> values sorted in <strong>ascending</strong> order.</li> |
| 29 | + <li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li> |
| 30 | +</ul> |
| 31 | +</div> |
0 commit comments