首页 > 其他 > 详细

Lintcode28 Search a 2D Matrix solution 题解

时间:2017-05-05 15:23:24      阅读:295      评论:0      收藏:0      [点我收藏+]

【题目描述】

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.

写出一个高效的算法来搜索 m × n矩阵中的值。

这个矩阵具有以下特性:每行中的整数从左到右是排序的。每行的第一个数大于上一行的最后一个整数。

【题目链接】

http://www.lintcode.com/en/problem/search-a-2d-matrix/

【题目解析】

对于这个给定的矩阵,我们如果用brute force解法,用两个嵌套循环,O(n2)便可以得到答案.但是我们需要注意的是这道题已经给定了这个矩阵的两个特性,这两个特性对于提

高我们算法的时间复杂度有很大帮助,首先我们给出一个O(n)的解法,也就是说我们可以固定住右上角的元素,根据递增或者递减的规律,我们可以判断这个给定的数值是否存在于这个矩阵当中.

【参考答案】

http://www.jiuzhang.com/solutions/search-a-2d-matrix/


Lintcode28 Search a 2D Matrix solution 题解

原文:http://12799252.blog.51cto.com/12789252/1922217

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!