Leetcode Binary Search

29. Divide Two Integers

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

Input: dividend = 10, divisor = 3
Output: 3

class Solution {
    public int search(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int mid = (start + end) / 2;
            if (target == nums[mid]) {
                return mid;
            }
            //左半段是有序的
            if (nums[start] <= nums[mid]) {
                //target 在这段里
                if (target >= nums[start] && target < nums[mid]) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            //右半段是有序的
            } else {
                if (target > nums[mid] && target <= nums[end]) {
                    start = mid + 1;
                } else {
                    end = mid - 1;
                }
            }

        }
        return -1;
    }
    
}

33. Search in Rotated Sorted Array

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Your algorithm’s runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

class Solution {
    public int search(int[] nums, int target) {
        int start = 0;
        int end = nums.length - 1;
        while (start <= end) {
            int mid = (start + end) / 2;
            if (target == nums[mid]) {
                return mid;
            }
            //左半段是有序的
            if (nums[start] <= nums[mid]) {
                //target 在这段里
                if (target >= nums[start] && target < nums[mid]) {
                    end = mid - 1;
                } else {
                    start = mid + 1;
                }
            //右半段是有序的
            } else {
                if (target > nums[mid] && target <= nums[end]) {
                    start = mid + 1;
                } else {
                    end = mid - 1;
                }
            }

        }
        return -1;
    }
    
}

81. Search in Rotated Sorted Array II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

Example 1:

Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true

class Solution {
    public boolean search(int[] nums, int target) {
        int low = 0, high = nums.length-1;
        
        while(low<= high)
        {   
            while ((low < high) && nums[low] == nums[high]) low += 1;
            int mid = low+(high-low)/2;
            if(nums[mid]==target) return true;
            if(nums[low]<= nums[mid])
            {
                if(nums[low]<=target&& target<nums[mid])
                    high = mid-1;
                else low = mid+1;
            }
            
            if(nums[mid]<=nums[high])
            {
                if(target<=nums[high] && target>nums[mid])
                    low = mid+1;
                else high = mid-1;
            }
           
        }
        
        return false;
    }
}

34. Find First and Last Position of Element in Sorted Array

Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm’s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

class Solution{
    public int[] searchRange(int[] nums, int target) {
    int start = 0;
    int end = nums.length - 1;
    int[] ans = { -1, -1 };
    if (nums.length == 0) {
        return ans;
    }
    while (start <= end) {
        int mid = (start + end) / 2;
        if (target == nums[mid]) {
            end = mid - 1;
        } else if (target < nums[mid]) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    //考虑 tartget 是否存在,判断我们要找的值是否等于 target 并且是否越界
    if (start == nums.length || nums[ start ] != target) {
        return ans;
    } else {
        ans[0] = start;
    }
    ans[0] = start;
    start = 0;
    end = nums.length - 1;
    while (start <= end) {
        int mid = (start + end) / 2;
        if (target == nums[mid]) {
            start = mid + 1;
        } else if (target < nums[mid]) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    if (end <0  || nums[ end ] != target) {
        return ans;
    } else {
        ans[1] = end;
    }
    return ans;
    }
}

35. Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.
Example 1:

Input: [1,3,5,6], 5
Output: 2

class Solution {
    public int searchInsert(int[] nums, int target) 
    {
        int low = 0, high = nums.length-1;
        
        while(low<= high)
        {
            int mid = low + (high- low)/2;
            if(nums[mid]== target)   return mid;
            else if(nums[mid]>target) high = mid-1;
            else low = mid+1;
        }
        
        return low;
    }
}

69. Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x, where x is guaranteed to be a non-negative integer.

Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.

class Solution {
    public int mySqrt(int x) {
        if(x==0) return 0;
        int left = 1, right = x;
        while(true)
        {
            int mid = left+(right-left)/2;
            if(mid == x/mid ) return mid;  
            if(mid>x/mid)
                right = mid -1;
            if(mid<x/mid)
            {
                if((mid+1)>x/(mid+1)) return mid;
                left= mid+1;
            }
        }
        }
}

74. Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:

Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
Example 1:

Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        //matrix的 row, col
        if(matrix ==null || matrix.length==0  ) return false;
        int m = matrix.length;
        int n = matrix[0].length;
        return BS(matrix, 0, m*n-1 ,target, n);
        
        
    }
    
    private boolean BS(int[][] nums, int low, int high, int target, int n)
    {
        while(low<=high)
        {
            int mid = low+(high -low)/2;
            int mid_value = nums[mid/n][mid%n];
            if(mid_value==target) return true;
            else if(mid_value<target) low = mid+ 1;
            else high = mid-1;
        }
        return false;
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值