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.

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.

First read discussions

http://articles.leetcode.com/searching-2d-sorted-matrix

http://articles.leetcode.com/searching-2d-sorted-matrix-part-ii

https://leetcode.com/discuss/51108/is-theres-a-o-log-m-log-n-solution-i-know-o-n-m-and-o-m-log-n

Solution 1

// O(M + N) Divide and conquer
	public static boolean searchMatrix(int[][] matrix, int target) {
		if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
			return false;
		}
		int col = matrix[0].length - 1;
		int row = 0;
		while (col >= 0 && row <= matrix.length - 1) {
			if (target == matrix[row][col]) {
				return true;
			} else if (target < matrix[row][col]) {
				col--;
			} else if (target > matrix[row][col]) {
				row++;
			}
		}
		return false;
	}


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、付费专栏及课程。

余额充值