Package g0701_0800.s0704_binary_search
Class Solution
java.lang.Object
g0701_0800.s0704_binary_search.Solution
704 - Binary Search.<p>Easy</p>
<p>Given an array of integers <code>nums</code> which is sorted in ascending order, and an integer <code>target</code>, write a function to search <code>target</code> in <code>nums</code>. If <code>target</code> exists, then return its index. Otherwise, return <code>-1</code>.</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,0,3,5,9,12], target = 9</p>
<p><strong>Output:</strong> 4</p>
<p><strong>Explanation:</strong> 9 exists in nums and its index is 4</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> nums = [-1,0,3,5,9,12], target = 2</p>
<p><strong>Output:</strong> -1</p>
<p><strong>Explanation:</strong> 2 does not exist in nums so return -1</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], target < 10<sup>4</sup></code></li>
<li>All the integers in <code>nums</code> are <strong>unique</strong>.</li>
<li><code>nums</code> is sorted in ascending order.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
search
public int search(int[] nums, int target)
-