-
Notifications
You must be signed in to change notification settings - Fork 161
/
binary-search.md
29 lines (21 loc) · 1.12 KB
/
binary-search.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<p>Given a <strong>sorted</strong> (in ascending order) integer array <code>nums</code> of <code>n</code> elements and a <code>target</code> value, 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><br />
<strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> <code>nums</code> = [-1,0,3,5,9,12], <code>target</code> = 9
<strong>Output:</strong> 4
<strong>Explanation:</strong> 9 exists in <code>nums</code> and its index is 4
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> <code>nums</code> = [-1,0,3,5,9,12], <code>target</code> = 2
<strong>Output:</strong> -1
<strong>Explanation:</strong> 2 does not exist in <code>nums</code> so return -1
</pre>
<p> </p>
<p><strong>Note:</strong></p>
<ol>
<li>You may assume that all elements in <code>nums</code> are unique.</li>
<li><code>n</code> will be in the range <code>[1, 10000]</code>.</li>
<li>The value of each element in <code>nums</code> will be in the range <code>[-9999, 9999]</code>.</li>
</ol>