avatar

superorange0707

  • Homepage
  • Tag
  • Category
Home Leetcode-704-Binary Search
文章

Leetcode-704-Binary Search

Posted 2022-07-12
5~6 min read

Binary Search

leetcode: https://leetcode.com/problems/binary-search/

Description:

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.

Idea: the defination of the interval is important, each interval is defined with same rule([ ]or[ ):include the right bound or not), each operation in the while loop need consider the defination of interval.

Step1: Because the nums is sorted, if the target less than the first element in the nums or the target bigger than the last one, it doesn’t exist in the string

Step2: set the right,left and mid bound. make the mid bound = right + (left-right)/2

Step3: if the target bigger than middle, make the right bound index to the middle pointer +1, otherwise, make the left bound index to middle index -1;

Code:

class Solution {
    public int search(int[] nums, int target) {
//      because the nums is sorted, if the target less than the first element in the nums or the target bigger than the last one, it doesn't exist in the string
        if (target < nums[0] || target > nums[nums.length - 1]) {
            return -1;
        }
        
//      set the left bound and right bound
        int i=0;
        int j=nums.length-1;
        
//      set the mid pointer, because it's sorted, so if the middle one bigger than target, then make the left bound equal to middle-1, otherwise make the right bound to the middle+1;
        while(i<=j){
            int mid = i + (j-i)/2;
            if(nums[mid] == target){
                return mid;
            }
            else if(target < nums[mid]){
                j = mid-1;
            }
            else if(target > nums[mid]){
                i = mid+1;
            }
        }
//      this target doesn't exist in the nums
        return -1;
    }
}
Leetcode
Leetcode Array
Share

Further Reading

Apr 23, 2025

283 - Move Zero

[283 - Move Zero] 🔗 LeetCode Link Problem Description Given an integer array nums, move all 0's to the end of it while maintaining the relative order

Apr 23, 2025

27 - Remove Element

[27 - Remove Element] 🔗 LeetCode Link Problem Description Given an integer array nums and an integer val, remove all occurrences of val in nums in-pl

Apr 23, 2025

26 - Remove Duplicates from Sorted Array

[26 - Remove Duplicates from Sorted Array] 🔗 LeetCode Link Problem Description Given an integer array nums sorted in non-decreasing order, remove the

OLDER

DSA-Learning-https://programmercarl.com/

NEWER

Leetcode-27-Remove Element

Recently Updated

  • Migrating Jenkins SCM Using GitLab from Bitbucket: SCM URL Bulk Replacement
  • 283 - Move Zero
  • 27 - Remove Element
  • 26 - Remove Duplicates from Sorted Array
  • Migrating from Bitbucket to GitLab? Here’s how to keep your teams moving without missing a beat!

Trending Tags

Course two pointer Binary Tree Hash SQL Leetcode Error Recording Gitlab Bitbucket Devops

Contents