csdn_spider/blog/ds19991999/原创-- 1.剑指Offer编程题之二维数组中的查找.md

197 lines
5.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 原创
1.剑指Offer编程题之二维数组中的查找
# 1.剑指Offer编程题之二维数组中的查找
**题目描述:** <br/> 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数.
**解题思路1** <br/> - 从左下角元素开始往上查找右边元素比这个元素大上边元素比这个元素小于是target比这个元素小就往上找比这个元素大就往右找.如果出现边界,则查找失败.
C/C++
```
class Solution{
public:
//二维向量这里最外的&lt;&gt;要有空格。否则在比较旧的编译器下无法通过
bool Find(int target, vector&lt; vector&lt;int&gt; &gt;array){
if(array.empty()) return false;
int rows = array.size();
int cols = array[0].size();
int i=rows-1,j=0; //左下标元素坐标
while(i&gt;=0&amp;&amp;j&lt;cols){
if(target&lt;array[i][j]) --i;
else if(target&gt;array[i][j]) ++j;
else return true;
}
return false;
}
};
```
Python2
```
class Solution:
def Find(self,target,array):
rows = len(array)
cols = len(array[0])
i = rows-1
j = 0
while j&lt;cols and i&gt;=0:
if target&lt;array[i][j]:
i -= 1
elif target&gt;array[i][j]:
j += 1
else:
return True
return False
```
Java
```
public class Solution{
public boolean Find(int target, int [][] array){
int rows = array.length;
int cols = array[0].length;
int i = rows-1,j=0;
while(i&gt;=0&amp;&amp;j&lt;cols){
if(target&lt;array[i][j])--i;
else if(target&gt;array[i][j])++j;
else return true;
}
return false;
}
}
```
**解题思路2** <br/> 从右上角开始查找左边元素比这个元素小下边元素比这个元素大于是target比这个元素大则往下找比这个元素小则往左找出现边界查找失败.
C/C++
```
class Solution{
public:
//二维向量这里最外的&lt;&gt;要有空格。否则在比较旧的编译器下无法通过
bool Find(int target, vector&lt; vector&lt;int&gt; &gt;array){
if(array.empty())return false;
int rows = array.size();
int cols = array[0].size();
int i=0,j=cols-1;
while(i&lt;=rows-1&amp;&amp;j&gt;=0){
if (target&lt;array[i][j])--j;
else if(target&gt;array[i][j])++i;
else return true;
}
return false;
}
};
```
Python2
```
class Solution:
def Find(self,target,array):
rows = len(array)
cols = len(array[0])
i = 0
j = cols-1
while i&lt;rows-1 and j&gt;=0:
if target&lt;array[i][j]:
j -= 1
elif target&gt;array[i][j]:
i += 1
else:
return True
return False
```
Java
```
public class Solution{
public boolean Find(int target, int [][] array){
int rows = array.length;
int cols = array[0].length;
int i=0,j=cols-1;
while(i&lt;=rows-1&amp;&amp;j&gt;=0){
if(target&lt;array[i][j])--j;
else if(target&gt;array[i][j])++i;
else return true;
}
return false;
}
}
```
**解题思路3** <br/> 把每一行看成是一个有序递增数组利用二分法查找通过遍历每一行得到答案时间复杂度nlogn.
C/C++
```
class Solution{
public:
bool Find(int target,vector&lt; vector&lt;int&gt; &gt;array){
if(array.empty()) return false;
int length=array.size();
for(int i=0;i&lt;length;++i){
int low = 0;
int high = array[i].size()-1;
while(low&lt;=high){
int mid = (low+high)/2;
if(target&gt;array[i][mid]) low=mid+1;
else if(target&lt;array[i][mid]) high=mid-1;
else return true;
}
}
return false;
}
};
```
Python2
```
class Solution:
def Find(self,target,array):
length=len(array)
for i in range(length):
low = 0
high = len(array[i])-1
while low&lt;=high:
mid = (low+high)/2
if target&gt;array[i][mid]:
low = mid + 1
elif target&lt;array[i][mid]:
high = mid-1
else:
return True
return False
```
Jave
```
public class Solution{
public boolean Find(int target, int [][] array){
int length=array.length;
for(int i=0;i&lt;length;++i){
int low = 0;
int high = array[i].length-1;
while(low&lt;=high){
int mid = (low+high)/2;
if(target&gt;array[i][mid])low=mid+1;
else if(target&lt;array[i][mid])high=mid-1;
else return true;
}
}
return false;
}
}
```
>
本实例均通过牛客网在线编译测试。