zoukankan      html  css  js  c++  java
  • LeetCode(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.
    For example,

    Consider the following matrix:

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

    分析

    二分搜索算法在二维矩阵中的应用;

    两次二分搜索:

    第一次,二分搜索第一列(借助另一个数组)找出小于等于target的下标pos

    第二次,二分搜索pos行,搜索target

    AC代码

    
    class Solution {
    public:
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
            if (matrix.empty())
                return false;
    
            int m = matrix.size();
    
            //将矩阵首列,压入临时vector,寻找,目标元素所在的行
            vector<int> v;
            for (int i = 0; i < m; i++)
                v.push_back(matrix[i][0]);
    
            //寻找 <=target 最近的元素下标
            int pos = binarySearchPos(v, 0, m - 1, target);
    
            //不存在任何一行中
            if (pos == -1)
                return false;
            else if(matrix[pos][0] == target)
                return true;
            else
                return binarySearch(matrix[pos], 0, matrix[pos].size() - 1, target);
    
        }
    
        int binarySearchPos(vector<int> &nums, int lhs, int rhs, int &target)
        {
            if (nums.empty())
                return -1;
    
            while (lhs <= rhs)
            {
                int mid = (lhs + rhs) / 2;
    
                if (nums[mid] == target)
                    return mid;
    
                else if (nums[mid] < target)
                {
                    lhs = mid + 1;
                }
                else{
                    rhs = mid - 1;
                }//else
            }//while
    
            if (lhs < rhs && nums[lhs] < target)
            {
                return lhs;
            }
            else{
                return lhs - 1;
            }
        }
    
        bool binarySearch(vector<int> &nums, int lhs, int rhs, int &target)
        {
            if (nums.empty())
                return false;
    
            while (lhs <= rhs)
            {
                int mid = (lhs + rhs) / 2;
    
                if (nums[mid] == target)
                    return true;
    
                else if (nums[mid] < target)
                {
                    lhs = mid + 1;
                }
                else{
                    rhs = mid - 1;
                }//else
            }//while
            return false;
        }
    };
    

    GitHub测试程序源码

  • 相关阅读:
    更准确的mysql全文索引
    range
    牛顿冷却定律 使用
    Servo: The Embeddable Browser Engine
    Node.js V0.12新特性之性能优化
    Lodash,你正在使用的JavaScript库
    Python on Android
    Microsoft HoloLens 技术解谜(下)
    Microsoft HoloLens 技术解谜(上)
    市售体感设备横评
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214857.html
Copyright © 2011-2022 走看看