704. Binary Search Easy

@problem@discussion
#Array#Binary Search



1/**
2 * [704] Binary Search
3 *
4 * Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1.
5 * You must write an algorithm with O(log n) runtime complexity.
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [-1,0,3,5,9,12], target = 9
10 * Output: 4
11 * Explanation: 9 exists in nums and its index is 4
12 * 
13 * Example 2:
14 * 
15 * Input: nums = [-1,0,3,5,9,12], target = 2
16 * Output: -1
17 * Explanation: 2 does not exist in nums so return -1
18 * 
19 *  
20 * Constraints:
21 * 
22 * 	1 <= nums.length <= 10^4
23 * 	-10^4 < nums[i], target < 10^4
24 * 	All the integers in nums are unique.
25 * 	nums is sorted in ascending order.
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/binary-search/
31// discuss: https://leetcode.com/problems/binary-search/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn search(nums: Vec<i32>, target: i32) -> i32 {
37        0
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_704() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.