【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

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.
Example 1:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 3
Output: true

Example 2:
Input:
matrix = [
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
target = 13
Output: false

/*
问题:在有序矩阵中查找某数(每行增序,且行首元素大于前一行的行尾元素,蛇形有序)
方法:将二维数组看成一维数组用二分查找即可
*/
class Solution
{
public:
    bool searchMatrix(vector<vector<int>>& matrix, int target)
    {
        if(matrix.empty() || matrix[0].empty()) return false;
           
        int rows = matrix.size();
        int cols = matrix[0].size();
        int left = 0, right = rows*cols - 1;
        while(left <= right)
        {
            int mid = (left+right) / 2;
            if(matrix[mid/cols][mid%cols] < target) left = mid+1;
            else if(matrix[mid/cols][mid%cols] > target) right = mid - 1;
            else return true;
        }
        return false;
    }
};

240. Search a 2D Matrix II
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 in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]

Example 1:
Input: matrix, target = 5Output: true
Example 2:
Input: matrix, target = 20
Output: false
//问题:有序矩阵中查找(非蛇形有序)
/*
方法一:扫描行,每行进行二分查找
O(r*logc)
*/
class Solution
{
public:
    bool searchMatrix(vector<vector<int>>& a, int target)
    {
        if(a.empty() || a[0].empty()) return false; //若为空,则返回false
       
        int row = a.size(); //行数
        int col = a[0].size(); //列数
        for(int i = 0; i < row; i++)//遍历行,target要处于行首和行尾元素之间
        {
            if(a[i][0]<=target&&a[i][col-1] >= target) //不能放在for语句里,因为一旦条件不满足就跳出整个for循环了
            {
                if(a[i][0] == target || a[i][col-1] == target) return true;//如果行首或行尾元素等于target则返回true
               // cout<<"行数为"<<i<<endl;
                int left = 0, right = col-1; //遍历列,用二分查找算法查找
                while(left<=right)
                {
                    int mid = (left+right)/2;
                    if(a[i][mid] < target) left = mid+1;
                    else if(a[i][mid] > target) right = mid-1;
                    else return true;
                }              
            }
        }
        return false;
    }
};
/*
* 方法二:缩小范围,从左下角元素(该行中最小数,该列中最大数)开始比较(利用L型有序)
* 过程:选取矩阵左下角元素,如果等于要查找的数,查找结束,
        如果小于目标数,找下一列,j++
        如果大于目标数,找上一行,i--
* 此方法效率较第一种高
O(r)或O(c)
*/
class Solution
{
public:
    bool searchMatrix(vector<vector<int>>& a, int target)
    {
        if(a.empty() || a[0].empty()) return false; //若为空,则返回false
      
        int row = a.size(); //行数
        int col = a[0].size(); //列数
      
        int i = row-1, j = 0;//从左下角元素开始比较
        while(i >= 0 && j <= col-1)
        {
            if(a[i][j] < target) //如果小于,则找下一列
                j++;
            else if(a[i][j] > target) //如果大于,找上一行
                i--;
            else
                return true;
        }
        return false;
      
    }
};

转载于:https://www.cnblogs.com/wikiwen/p/10225002.html

【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...相关推荐

  1. 【LeetCode 剑指offer刷题】回溯法与暴力枚举法题6:Number of Islands

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Number of Islands Given a 2d grid map of '1's (land) and ' ...

  2. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  3. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

  4. 【LeetCode 剑指offer刷题】字符串题6:67 把字符串转成整数

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 67 把字符串转成整数 题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符 ...

  5. 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...

  6. 【LeetCode 剑指offer刷题】查找与排序题14:Wiggle Sort(系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Wiggle Sort II Given an unsorted array nums, reorder it su ...

  7. 【LeetCode 剑指offer刷题】查找与排序题12:Top K Frequent Elements

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Top K Frequent Elements Given a non-empty array of integer ...

  8. 【LeetCode 剑指offer刷题】树题19:8 二叉树中序遍历的下一个结点

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 8 二叉树中序遍历的下一个结点 题目描述 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注 ...

  9. 【LeetCode 剑指offer刷题】字符串题12:Valid Palindrome(回文词系列)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Valid Palindrome Given a string, determine if it is a pali ...

最新文章

  1. Linux的10个游戏
  2. 1滴血,2小时,验13种癌症,精度99%!日本东芝新技术引热议
  3. Android 体系结构和应用程序组成
  4. 套接字设置为(非)阻塞模式
  5. 一键离线下载python安装包:
  6. “两小学生研究喝茶抗癌获奖”,官方回应:经老师培训独立完成
  7. saltstack中grains简介
  8. 《spring-boot学习》-08-spring boot 优雅的使用mybatis
  9. 有关SQL Server 2008你一定要知道的八件事 之三
  10. 适配器模式之状态模式
  11. 制作 maxdos 启动盘 Linux 安装盘
  12. Java算法与数据结构、设计模式、高并发视频教程免费下载
  13. html九宫格实现人像拼图游戏,实例分享jQuery+vue.js实现的九宫格拼图游戏
  14. 270天不回家的“空中飞人们” 下一步要去哪里?
  15. 注册华为云用户: 访问官网 https://huaweicloud.com/ 注册华为云用户(需手机号验证) 登录并完成实名认证 为账号充值不少于100元(不用时可提现
  16. IPTV系统中EPG模块的设计与实现
  17. 【媒体聚焦】“我们为什么要为景安点赞”——记景安网络十四年峥嵘岁月
  18. flutter - 图文讲解表单组件基本使用 注册实战
  19. 燕大计算机研究生毕业待遇,研究生人均“月薪上万”是真是假,过来人坦言:想想就好,别认真...
  20. java课程设计-音乐播放器,基于java的音乐播放器设计.doc

热门文章

  1. AI队列长度检测:R-CNN用于使用Keras进行自定义对象检测
  2. 深度学习框架 TensorFlow.NET 0.1.0,完善变量更新操作
  3. TypeScript 2019 路线图:更效率,更易用!
  4. shel脚本中怎么引用文件_Linux shell脚本中如何读取跟shell脚本同一目录下的配置文件...
  5. echart仪表盘旋转_使用echart仪表盘
  6. 用python生成九九乘法表的指令_Python中生成九九乘法表的方法有哪几种?
  7. div 隐藏_SEO优化,隐藏文本与隐藏链接对SEO的影响!
  8. jquery开关灯案例_jquery图文开关灯切换特效
  9. python爬取音乐_Python现学现用xpath爬取豆瓣音乐
  10. win10家庭版远程桌面_win10 家庭版使用RDPWrap开通远程桌面服务