美文网首页
二分查找类

二分查找类

作者: BigBig_Fish | 来源:发表于2017-07-13 21:52 被阅读0次

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<int>& nums, int target) {
        int res = 0;
        for (vector<int>::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<int>& 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<vector<int>>& matrix, int target) {
        for(vector<int> 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<vector<int>>& 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;
    }
};

相关文章

  • 消息传递-缓存-转发流程

    消息传递 缓存查找 哈希查找 三种查找方式缓存 -> 哈希算法查找当前类 -> 已排序 二分查找算法 未排序 ...

  • 二分查找类

    Search Insert Position Given a sorted array and a target ...

  • python二分查找算法

    文章概述 二分查找法介绍 简单查找与二分查找对比 二分查找  二分查找算法主要思想:在有序列表中查找指定元素,先从...

  • 数据结构和算法--二分查找

    二分查找 二分查找的思想 二分查找(Binary Search)算法,也叫折半查找算法。 二分查找针对的是一个有序...

  • Java学习笔记 - 第021天

    每日要点 折半查找(二分查找) 例子1: Collections工具类 例子1: 遗留容器 遗留容器 (自己写代码...

  • 二分查找

    [TOC] 二分查找的基础模板 二分查找靠左的Index基础模板 二分查找靠右的Index基础模板 二分查找插入t...

  • 二分查找法

    二分查找法 二分查找法(递归)

  • 二分查找(递归、非递归)

    二分查找(递归) 二分查找(非递归)

  • 二分查找(递归、非递归)

    二分查找(递归) 二分查找(非递归)

  • 二分查找

    什么是二分查找?二分查找,也叫折半查找(Binary Search),它是一种效率较高的查找方法。二分查找的条件:...

网友评论

      本文标题:二分查找类

      本文链接:https://www.haomeiwen.com/subject/ykouhxtx.html