LeetCode //C - 704. Binary Search

704. Binary Search

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.

You must write an algorithm with O(log n) runtime complexity.
 

Example 1:

Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Constraints:
  • 1 < = n u m s . l e n g t h < = 1 0 4 1 <= nums.length <= 10^4 1<=nums.length<=104
  • − 1 0 4 < n u m s [ i ] , t a r g e t < 1 0 4 -10^4 < nums[i], target < 10^4 104<nums[i],target<104
  • All the integers in nums are unique.
  • nums is sorted in ascending order.

From: LeetCode
Link: 704. Binary Search


Solution:

Ideas:

1. Initialize Pointers: It starts by initializing two pointers, left and right, which represent the indices of the start and end of the segment of the array currently being considered. Initially, left is 0 (the first index of the array) and right is numsSize - 1 (the last index of the array).

2. Loop Until Found or Exhausted: The search continues as long as left is less than or equal to right, meaning there is still a portion of the array left to be examined.

3. Calculate Midpoint: Inside the loop, it calculates the midpoint mid of the current segment as left + (right - left) / 2. This avoids potential overflow that could occur if left and right are large integers.

4. Check Midpoint Value:

  • If the value at mid is equal to the target, the search is successful, and the index mid is returned.
  • If the value at mid is less than the target, the target can only be in the right segment of the current midpoint. Therefore, the left pointer is moved to mid + 1.
  • If the value at mid is greater than the target, the target can only be in the left segment of the current midpoint. Therefore, the right pointer is moved to mid - 1.

5. Target Not Found: If the loop ends without finding the target (meaning left becomes greater than right), the function returns -1, indicating that the target is not present in the array.

Code:
int search(int* nums, int numsSize, int target) {
    int left = 0, right = numsSize - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2; // Avoid potential overflow
        if (nums[mid] == target) {
            return mid; // Target found
        } else if (nums[mid] < target) {
            left = mid + 1; // Focus on the right half
        } else {
            right = mid - 1; // Focus on the left half
        }
    }
    return -1; // Target not found
}

相关推荐

  1. leetcode704. 二分查找

    2024-03-27 15:16:03       37 阅读
  2. 704. 二分查找

    2024-03-27 15:16:03       35 阅读
  3. Leetcode:704. 二分查找

    2024-03-27 15:16:03       45 阅读
  4. 704.二分查找

    2024-03-27 15:16:03       39 阅读
  5. LeetCode 704 二分查找

    2024-03-27 15:16:03       18 阅读
  6. Leetcode704_二分查找

    2024-03-27 15:16:03       21 阅读
  7. leetcode 704. 二分查找

    2024-03-27 15:16:03       14 阅读
  8. Leetcode704_二分查找

    2024-03-27 15:16:03       9 阅读
  9. 704. 二分查找

    2024-03-27 15:16:03       9 阅读
  10. 704. Binary Search

    2024-03-27 15:16:03       7 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 15:16:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 15:16:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 15:16:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 15:16:03       18 阅读

热门阅读

  1. 008-如何支持各种语言的项目

    2024-03-27 15:16:03       14 阅读
  2. unity中平台判断

    2024-03-27 15:16:03       14 阅读
  3. .NET Core教程:深入实践与实例解析

    2024-03-27 15:16:03       18 阅读
  4. Gartner发布2024年影响技术提供商的重大趋势

    2024-03-27 15:16:03       18 阅读
  5. Windows CMD命令大全(快速上手)

    2024-03-27 15:16:03       18 阅读
  6. 深入理解 C++ 中的 IO 流【iostream篇】

    2024-03-27 15:16:03       16 阅读
  7. Canathus 一个简单的React表单验证工具

    2024-03-27 15:16:03       15 阅读
  8. python教程(3更新中)

    2024-03-27 15:16:03       16 阅读