二分查找类

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.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0

题目分析

普通方法就是按个找时间O(N),二分搜索O(log n)。

代码

普通方法

class Solution {
public:
    int searchInsert(vector& nums, int target) {
        int res = 0;
        for (vector::iterator iter = nums.begin(); iter != nums.end(); iter++) {
            if (*iter == target)
            {
                return res;
            }
            if (*iter > target) {
                return res;
            }
            res++;
        }
        return res;
    }
};

二分搜索

class Solution {
public:
    int searchInsert(vector& nums, int target) {
        int low = 0, high = nums.size()-1;

        // Invariant: the desired index is between [low, high+1]
        while (low <= high) {
            int mid = low + (high-low)/2;

            if (nums[mid] < target)
                low = mid+1;
            else
                high = mid-1;
        }

        // (1) At this point, low > high. That is, low >= high+1
        // (2) From the invariant, we know that the index is between [low, high+1], so low <= high+1. Follwing from (1), now we know low == high+1.
        // (3) Following from (2), the index is between [low, high+1] = [low, low], which means that low is the desired index
        //     Therefore, we return low as the answer. You can also return high+1 as the result, since low == high+1
        return low;
    }
};

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.
For example,

Consider the following matrix:

[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.

题目分析

这道题是在矩阵中找到目标元素,我的思路是先通过每一行的末元素找到目标函数可能在的行,然后在行内使用二分查找。

代码

class Solution {
public:
    bool searchMatrix(vector>& matrix, int target) {
        for(vector vec : matrix){
            if(vec.size() == 0) return false;
            if(target == vec[vec.size()-1]) return true;
            if(target > vec[vec.size()-1]){
                continue;
            }
            else{
                int low=0, high=vec.size()-1;
                while(low < high){
                    int mid = (low+high) >> 1;
                    if(target == vec[mid]) return true;
                    else if(target > vec[mid]){ 
                        low = mid+1;
                    }
                    else{
                        high = mid;
                    }
                }
                return false;
            }
        }
        return false;
    }
};

真正的二分查找

算出总共有多少的元素,然后利用偏移算出坐标,进行对比,这才是二分法啊!

class Solution {
public:
    bool searchMatrix(vector>& matrix, int target) {
        if(matrix.empty()) return false;
        int m = matrix.size();
        int n = matrix[0].size();
        int first = 0, last = m * n;
        while(first < last){
            int mid = (first + last) / 2;
            int value = matrix[mid / n][mid % n];
            if(value == target) return true;
            else if(value > target) last = mid;
            else first = mid + 1;
        }
        return false;
    }
};

你可能感兴趣的:(二分查找类)