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.

Subscribe to see which companies asked this question

思路分析:

设置一个变量i表示行,变量j表示列,j初始指向向量尾。当matrix[i][j] < value,则 i++,否则 j--

代码如下:

class Solution {
public:
    bool searchMatrix(vector<vector<int> > &matrix, int target) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int i = 0, j = matrix[0].size() - 1;
        
        while (i < matrix.size() && j >= 0)
        {
            if (target == matrix[i][j])
                return true;
            else if (target < matrix[i][j])
                j--;
            else
                i++;
        }
        
        return false;
    }
};


apt-cache search opencv cl-opencv-apps - opencv_apps Robot OS package - LISP bindings gstreamer1.0-opencv - GStreamer OpenCV plugins gyoto-bin - General relativistic ray-tracing command-line interface libcv-bridge-dev - cv_bridge Robot OS package - development libcv-bridge2d - cv_bridge Robot OS package libgstreamer-opencv1.0-0 - GStreamer OpenCV libraries libimage-geometry-dev - image_geometry Robot OS package - development libimage-geometry1d - image_geometry Robot OS package - runtime libmrgingham-dev - Chessboard finder for visual calibration routines libmrgingham2 - Chessboard finder for visual calibration routines libmrpt-img-dev - Mobile Robot Programming Toolkit - img development package libmrpt-img2.4 - Mobile Robot Programming Toolkit - img library libopencv-apps-dev - Opencv_apps Robot OS package - development files libopencv-apps2d - opencv_apps Robot OS package - runtime files libopencv-calib3d-dev - development files for libopencv-calib3d4.5d libopencv-calib3d4.5d - computer vision Camera Calibration library libopencv-contrib-dev - development files for libopencv-contrib4.5d libopencv-contrib4.5d - computer vision contrlib library libopencv-core-dev - development files for libopencv-core4.5d libopencv-core4.5d - computer vision core library libopencv-dev - development files for opencv libopencv-dnn-dev - development files for libopencv-dnn4.5d libopencv-dnn4.5d - computer vision Deep neural network module libopencv-features2d-dev - development files for libopencv-features2d4.5d libopencv-features2d4.5d - computer vision Feature Detection and Descriptor Extraction library libopencv-flann-dev - development files for libopencv-flann4.5d libopencv-flann4.5d - computer vision Clustering and Search in Multi-Dimensional spaces library libopencv-highgui-dev - development files for libopencv-highgui4.5d libopencv-highgui4.5d - computer vision High-level GUI and Media I/O library libopencv-imgcodecs-dev - development files for libopencv-imgcodecs4.5d libopencv-imgcodecs4.5d - computer vision Image Codecs library libopencv-imgproc-dev - development files for libopencv-imgproc4.5d libopencv-imgproc4.5d - computer vision Image Processing library libopencv-ml-dev - development files for libopencv-ml4.5d libopencv-ml4.5d - computer vision Machine Learning library libopencv-objdetect-dev - development files for libopencv-objdetect4.5d libopencv-objdetect4.5d - computer vision Object Detection library libopencv-photo-dev - development files for libopencv-photo4.5d libopencv-photo4.5d - computer vision computational photography library libopencv-shape-dev - development files for libopencv-shape4.5d libopencv-shape4.5d - computer vision shape descriptors and matchers library libopencv-stitching-dev - development files for libopencv-stitching4.5d libopencv-stitching4.5d - computer vision image stitching library libopencv-superres-dev - development files for libopencv-superres4.5d libopencv-superres4.5d - computer vision Super Resolution library libopencv-video-dev - development files for libopencv-video4.5d libopencv-video4.5d - computer vision Video analysis library libopencv-videoio-dev - development files for libopencv-videoio4.5d libopencv-videoio4.5d - computer vision Video I/O library libopencv-videostab-dev - development files for libopencv-videostab4.5d libopencv-videostab4.5d - computer vision video stabilization library libopencv-viz-dev - development files for libopencv-viz4.5d libopencv-viz4.5d - computer vision 3D data visualization library libopencv4.5-java - Java bindings for the computer vision library libopencv4.5d-jni - Java jni library for the computer vision library mrgingham - Chessboard finder for visual calibration routines node-opencv - OpenCV Bindings for node.js octave-video - video file reader/writer for Octave opencv-data - development data for opencv opencv-doc - OpenCV documentation and examples php-facedetect - Detect faces with PHP python-willow-doc - Python image library (documentation) python3-cv-bridge - cv_bridge ROS package - Python 3 bindings python3-gyoto - General relativistic geodesic integration for the Python 3 language python3-image-geometry - image_geometry Robot OS package - Python 3 bindings python3-mrgingham - Chessboard finder for visual calibration routines python3-opencv - Python 3 bindings for the computer vision library python3-opencv-apps - opencv_apps Robot OS package - Python 3 bindings python3-pylibdmtx - Read Data Matrix barcodes (Python3 version) python3-pyzbar - Python bindings for libzbar python3-willow - Python image library combining Pillow, Wand and OpenCV (Python 3) ros-opencv-apps - opencv_apps Robot OS package - apps sightcalibrator - Camera calibration software ros-humble-aruco-opencv - ArUco marker detection using aruco module from OpenCV libraries. ros-humble-aruco-opencv-dbgsym - debug symbols for ros-humble-aruco-opencv ros-humble-aruco-opencv-msgs - Message definitions for aruco_opencv package. ros-humble-aruco-opencv-msgs-dbgsym - debug symbols for ros-humble-aruco-opencv-msgs ros-humble-cv-bridge - This contains CvBridge, which converts between ROS2 Image messages and OpenCV images. ros-humble-grid-map-cv - Conversions between grid maps and OpenCV images. ros-humble-image-geometry - `image_geometry` contains C++ and Python libraries for interpreting images geometrically. ros-humble-swri-opencv-util - A package with commonly used OpenCV functionality. ros-humble-swri-opencv-util-dbgsym - debug symbols for ros-humble-swri-opencv-util ros-humble-vision-opencv - Packages for interfacing ROS2 with OpenCV, a library of programming functions for real time computer vision. ros-iron-aruco-opencv - ArUco marker detection using aruco module from OpenCV libraries. ros-iron-aruco-opencv-dbgsym - debug symbols for ros-iron-aruco-opencv ros-iron-aruco-opencv-msgs - Message definitions for aruco_opencv package. ros-iron-aruco-opencv-msgs-dbgsym - debug symbols for ros-iron-aruco-opencv-msgs ros-iron-cv-bridge - This contains CvBridge, which converts between ROS2 Image messages and OpenCV images. ros-iron-grid-map-cv - Conversions between grid maps and OpenCV images. ros-iron-image-geometry - `image_geometry` contains C++ and Python libraries for interpreting images geometrically. ros-iron-swri-opencv-util - A package with commonly used OpenCV functionality. ros-iron-swri-opencv-util-dbgsym - debug symbols for ros-iron-swri-opencv-util ros-iron-vision-opencv - Packages for interfacing ROS2 with OpenCV, a library of programming functions for real time computer vision. ros-rolling-aruco-opencv - ArUco marker detection using aruco module from OpenCV libraries. ros-rolling-aruco-opencv-dbgsym - debug symbols for ros-rolling-aruco-opencv ros-rolling-aruco-opencv-msgs - Message definitions for aruco_opencv package. ros-rolling-aruco-opencv-msgs-dbgsym - debug symbols for ros-rolling-aruco-opencv-msgs ros-rolling-cv-bridge - This contains CvBridge, which converts between ROS2 Image messages and OpenCV images. ros-rolling-image-geometry - `image_geometry` contains C++ and Python libraries for interpreting images geometrically. ros-rolling-swri-opencv-util - swri_opencv_util ros-rolling-swri-opencv-util-dbgsym - debug symbols for ros-rolling-swri-opencv-util ros-rolling-vision-opencv - Packages for interfacing ROS2 with OpenCV, a library of programming functions for real time computer vision. robotyfw@yfw:~$
最新发布
12-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值