Class Solution

java.lang.Object
g0001_0100.s0035_search_insert_position.Solution

public class Solution extends Object
35 - Search Insert Position.<p>Easy</p> <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> <p>You must write an algorithm with <code>O(log n)</code> runtime complexity.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,3,5,6], target = 5</p> <p><strong>Output:</strong> 2</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,3,5,6], target = 2</p> <p><strong>Output:</strong> 1</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,3,5,6], target = 7</p> <p><strong>Output:</strong> 4</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> nums = [1,3,5,6], target = 0</p> <p><strong>Output:</strong> 0</p> <p><strong>Example 5:</strong></p> <p><strong>Input:</strong> nums = [1], target = 0</p> <p><strong>Output:</strong> 0</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>4</sup></code></li> <li><code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code></li> <li><code>nums</code> contains <strong>distinct</strong> values sorted in <strong>ascending</strong> order.</li> <li><code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • searchInsert

      public int searchInsert(int[] nums, int target)