Skip to content

Commit 3158bd2

Browse files
committed
1
1 parent cbbbe72 commit 3158bd2

File tree

17 files changed

+1166
-0
lines changed

17 files changed

+1166
-0
lines changed

4二维数组中的查找/searcharray/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

4二维数组中的查找/searcharray/.idea/dbnavigator.xml

Lines changed: 458 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

4二维数组中的查找/searcharray/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

4二维数组中的查找/searcharray/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

4二维数组中的查找/searcharray/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @Description: 公用常量
3+
* @Date: 2021/1/3 21:47
4+
* @Pacakge: PACKAGE_NAME
5+
* @ClassName: Solution1
6+
* @Version: v1.0.0
7+
* @Author: ccc
8+
*/
9+
public class Solution1 {
10+
public boolean findNumberIn2DArray(int[][] matrix, int target) {
11+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
12+
return false;
13+
}
14+
int hang = 0;
15+
int lie = matrix[0].length-1;
16+
while(hang<matrix.length && lie>=0){
17+
if(matrix[hang][lie] == target){
18+
return true;
19+
}else if(matrix[hang][lie] > target){
20+
lie--;
21+
}else{
22+
hang++;
23+
}
24+
}
25+
return false;
26+
}
27+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#### 二维数组中的查找
2+
在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
3+
4+
 
5+
6+
示例:
7+
8+
现有矩阵 matrix 如下:
9+
10+
[
11+
[1, 4, 7, 11, 15],
12+
[2, 5, 8, 12, 19],
13+
[3, 6, 9, 16, 22],
14+
[10, 13, 14, 17, 24],
15+
[18, 21, 23, 26, 30]
16+
]
17+
给定 target = 5,返回 true。
18+
19+
给定 target = 20,返回 false。
20+
21+
#### 解法如下:
22+
从右上角开始判别,若是该数字等于要查找的数字,则查找过程结束;如果该数字大于要查找的数字,则剔除该数字所在的列,如果该数字小于要查找的数字,则剔除这个数字所在的行。
23+
代码如下:
24+
```Java
25+
public class Solution {
26+
public boolean findNumberIn2DArray(int[][] matrix, int target) {
27+
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
28+
return false;
29+
}
30+
int hang = 0;
31+
int lie = matrix[0].length-1;
32+
while(hang<matrix.length && lie>=0){
33+
if(matrix[hang][lie] == target){
34+
return true;
35+
}else if(matrix[hang][lie] > target){
36+
lie--;
37+
}else{
38+
hang++;
39+
}
40+
}
41+
return false;
42+
}
43+
}
44+
```

5替换空格/blankspace/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)