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测试程序源码

  • 相关阅读:
    U3D开发中关于脚本方面的限制-有关IOS反射和JIT的支持问题
    APP发行渠道
    在WINDOWS上开发IOS应用的方法
    如何安全的在不同工程间安全地迁移asset数据?三种方法
    UNITY 的GC ALLOC到底是什么
    Dictionary,hashtable, stl:map有什么异同?
    如何成为一个优秀的高级C++程序员
    两点间所有路径的遍历算法
    技术人员的未来:做技术还是做管理?
    技术人员如何去面试?
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214857.html
Copyright © 2011-2022 走看看