Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

思路:二维数组中的查找 #1

Open
YZcxy opened this issue Mar 26, 2019 · 0 comments
Open

思路:二维数组中的查找 #1

YZcxy opened this issue Mar 26, 2019 · 0 comments

Comments

@YZcxy
Copy link
Owner

YZcxy commented Mar 26, 2019

题目描述

在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

思路

在二维数组中查找一个值,最直接的方式就是一行一行遍历,直到找到对应的值。但是如果从数组的左上角入手,就没有办法利用上这个左右、上下递增的数组特征。
所以我们可以选择从左下角或者右上角开始比较,这样每次比较的大小都可以让搜索的范围更小。
例如从左下角开始比较,目标大于当前数则向右查找,目标小于当前数则向上查找。直到找到对应元素或者出界。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant