当前位置: 首页 > news >正文

LeetCode -- 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.
For example,


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]
]
Given target = 5, return true.


Given target = 20, return false.


本题看起来是Search a 2D Matrix的扩展问题,其实和第一题的思路完全不一样。比较精简的思路是:
1.从左下开始找,如果matrix[r,c](r为行,c为列) < target,c++;否则r--。
2.直到r或c越界为止。




实现代码:


public class Solution {
    public bool SearchMatrix(int[,] matrix, int target) 
    {
        var rowLen = matrix.GetLength(0);
        var colLen = matrix.GetLength(1);
        
        var row = rowLen - 1;
        var col = 0;
        
        while(col <= colLen - 1 && row >= 0){
            if(matrix[row,col] == target){
                return true;
            }
            else if(matrix[row,col] < target){
                col ++;
            }
            else{
                row --;
            }
        }
        return false;
    }


}


相关文章:

  • [IE编程] 打开/关闭IE8的光标浏览模式(Caret Browsing)
  • LeetCode -- 3Sum Closest
  • 使用反射将业务对象绑定到 ASP.NET 窗体控件
  • LeetCode -- 4Sum
  • LeetCode -- Binary Tree Level Order Traversal II
  • (ZT)一个美国文科博士的YardLife
  • LeetCode -- Clone Graph
  • Oracle 中的nvl() 函数 相当于Sql Server 的 isnull()
  • LeetCode -- Combinations
  • [IE编程] WebBrowser控件中设置页面的缩放
  • LeetCode -- Find the Duplicate Number
  • LeetCode -- Group Anagrams
  • LeetCode -- Kth Largest Element in an Array
  • 最近评论回复汇总
  • LeetCode -- Maximum Gap
  • [ 一起学React系列 -- 8 ] React中的文件上传
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • 2017-08-04 前端日报
  • Django 博客开发教程 8 - 博客文章详情页
  • Intervention/image 图片处理扩展包的安装和使用
  • php的插入排序,通过双层for循环
  • RxJS: 简单入门
  • 简单实现一个textarea自适应高度
  • 码农张的Bug人生 - 初来乍到
  • 责任链模式的两种实现
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • ​马来语翻译中文去哪比较好?
  • #define
  • (2022 CVPR) Unbiased Teacher v2
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)linux下的时间函数使用
  • (转)setTimeout 和 setInterval 的区别
  • (转)创业的注意事项
  • .net 程序 换成 java,NET程序员如何转行为J2EE之java基础上(9)
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .net和jar包windows服务部署
  • .NET平台开源项目速览(15)文档数据库RavenDB-介绍与初体验
  • /etc/motd and /etc/issue
  • @angular/cli项目构建--Dynamic.Form
  • @Autowired 与@Resource的区别
  • @RequestBody与@ModelAttribute
  • @RequestMapping处理请求异常
  • [ 常用工具篇 ] POC-bomber 漏洞检测工具安装及使用详解
  • [Android]如何调试Native memory crash issue
  • [Android实例] 保持屏幕长亮的两种方法 [转]
  • [C++]C++入门--引用
  • [C++]运行时,如何确保一个对象是只读的
  • [JS真好玩] 掘金创作者必备: 监控每天是谁取关了你?
  • [Linux] PXE批量装机
  • [PyQt] 使用.qrc 生成资源文件供程序中使用