【20】240. Search a 2D Matrix II

本文介绍了一种高效的算法,用于在一个具有特殊排序属性的二维矩阵中查找特定值。该矩阵的特点是每一行从左到右递增排序,每一列从上到下递增排序。通过利用这些特性,文章提出了一种基于比较左下角元素的方法来快速定位目标值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

240. Search a 2D Matrix II

  • Total Accepted: 64544
  • Total Submissions: 169839
  • Difficulty: Medium
  • Contributors: Admin

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.

Solution:

 1 class Solution {
 2 public:
 3 //我们可以发现有两个位置的数字很有特点,左下角和右上角的数。左下角的18,往上所有的数变小,往右所有数增加,那么我们就可以和目标数相比较,如果目标数大,就往右搜,如果目标数小,就往左搜。这样就可以判断目标数是否存在。当然我们也可以把起始数放在右上角,往左和下搜,停止条件设置正确就行。
 4     bool searchMatrix(vector<vector<int>>& matrix, int target) {
 5         if(matrix.size() == 0 || matrix[0].size() == 0) return false;
 6         int m = matrix.size();
 7         int n = matrix[0].size();
 8         if(target < matrix[0][0] || target > matrix[m - 1][n - 1]) return false;
 9         int x = m - 1;
10         int y = 0;
11         while(true){
12             if(target < matrix[x][y]){
13                 x --;
14             }else if(target > matrix[x][y]){
15                 y ++;
16             }else if(target == matrix[x][y]){
17                 return true;
18             }
19             if(x < 0 || y >= n) break;
20         }
21         return false;
22     }
23 };

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/93scarlett/p/6372383.html

transform:matrix是CSS3中的一个属性,它用于对元素进行变形操作。matrix()方法是transform属性中的一个函数,它可以通过一个矩阵来实现元素的旋转、缩放、倾斜和平移等变换效果。matrix()方法的参数由六个数字组成,分别代表矩阵的六个值,即a、b、c、d、e和f。通过调整这些值,可以实现不同的变形效果。例如,transform:matrix(1, 0, 0, 1, x, y)表示对元素进行平移操作,其水平偏移量为x,垂直偏移量为y。 通过理解transform中的matrix()矩阵方法,我们可以更深入地理解CSS3中的transform属性,并利用它来实现更丰富多样的元素变形效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [CSS3矩阵理解———transform: matrix()改变元素运动的本质](https://blog.csdn.net/weixin_44309019/article/details/88722453)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [HTML 学习笔记 CSS3 (2D Matrix)](https://blog.csdn.net/ddiv24492/article/details/102234967)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值