File tree 6 files changed +159
-0
lines changed
6 files changed +159
-0
lines changed Original file line number Diff line number Diff line change
1
+ # [ 面试题04. 二维数组中的查找] ( https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof/ )
2
+
3
+ ## 题目描述
4
+ 在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
5
+
6
+ ** 示例:**
7
+
8
+ 现有矩阵 matrix 如下:
9
+
10
+ ```
11
+ [
12
+ [1, 4, 7, 11, 15],
13
+ [2, 5, 8, 12, 19],
14
+ [3, 6, 9, 16, 22],
15
+ [10, 13, 14, 17, 24],
16
+ [18, 21, 23, 26, 30]
17
+ ]
18
+ ```
19
+
20
+ 给定 target = 5,返回 ` true ` 。
21
+
22
+ 给定 target = 20,返回 ` false ` 。
23
+
24
+ ** 限制:**
25
+
26
+ - ` 0 <= n <= 1000 `
27
+
28
+ - ` 0 <= m <= 1000 `
29
+
30
+ ## 解法
31
+ ### Python3
32
+ ``` python
33
+ class Solution :
34
+ def findNumberIn2DArray (self , matrix : List[List[int ]], target : int ) -> bool :
35
+ if not matrix:
36
+ return False
37
+
38
+ i = len (matrix) - 1
39
+ j = 0
40
+ while i >= 0 and j < len (matrix[0 ]):
41
+ if matrix[i][j] == target:
42
+ return True
43
+ if matrix[i][j] < target:
44
+ j += 1
45
+ else :
46
+ i -= 1
47
+ return False
48
+ ```
49
+
50
+ ### Java
51
+ ``` java
52
+ class Solution {
53
+ public boolean findNumberIn2DArray (int [][] matrix , int target ) {
54
+ if (matrix == null || matrix. length == 0 ) {
55
+ return false ;
56
+ }
57
+ int rows = matrix. length, cols = matrix[0 ]. length;
58
+
59
+ int i = rows - 1 , j = 0 ;
60
+ while (i >= 0 && j < cols) {
61
+ if (matrix[i][j] == target) {
62
+ return true ;
63
+ }
64
+ if (matrix[i][j] < target) {
65
+ ++ j;
66
+ } else {
67
+ -- i;
68
+ }
69
+ }
70
+ return false ;
71
+ }
72
+ }
73
+ ```
74
+
75
+ ### ...
76
+ ```
77
+
78
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean findNumberIn2DArray (int [][] matrix , int target ) {
3
+ if (matrix == null || matrix .length == 0 ) {
4
+ return false ;
5
+ }
6
+ int rows = matrix .length , cols = matrix [0 ].length ;
7
+
8
+ int i = rows - 1 , j = 0 ;
9
+ while (i >= 0 && j < cols ) {
10
+ if (matrix [i ][j ] == target ) {
11
+ return true ;
12
+ }
13
+ if (matrix [i ][j ] < target ) {
14
+ ++j ;
15
+ } else {
16
+ --i ;
17
+ }
18
+ }
19
+ return false ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def findNumberIn2DArray (self , matrix : List [List [int ]], target : int ) -> bool :
3
+ if not matrix :
4
+ return False
5
+
6
+ i = len (matrix ) - 1
7
+ j = 0
8
+ while i >= 0 and j < len (matrix [0 ]):
9
+ if matrix [i ][j ] == target :
10
+ return True
11
+ if matrix [i ][j ] < target :
12
+ j += 1
13
+ else :
14
+ i -= 1
15
+ return False
Original file line number Diff line number Diff line change
1
+ # [ 面试题05. 替换空格] ( https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof/ )
2
+
3
+ ## 题目描述
4
+ 请实现一个函数,把字符串 ` s ` 中的每个空格替换成"%20"。
5
+
6
+ ** 示例 1:**
7
+
8
+ ```
9
+ 输入:s = "We are happy."
10
+ 输出:"We%20are%20happy."
11
+ ```
12
+
13
+ **限制:**
14
+
15
+ - `0 <= s 的长度 <= 10000`
16
+
17
+ ## 解法
18
+ ### Python3
19
+ ```python
20
+ class Solution:
21
+ def replaceSpace(self, s: str) -> str:
22
+ return s.replace(' ', '%20')
23
+ ```
24
+
25
+ ### Java
26
+ ``` java
27
+ class Solution {
28
+ public String replaceSpace (String s ) {
29
+ return s. replaceAll(" " , " %20" );
30
+ }
31
+ }
32
+ ```
33
+
34
+ ### ...
35
+ ```
36
+
37
+ ```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String replaceSpace (String s ) {
3
+ return s .replaceAll (" " , "%20" );
4
+ }
5
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def replaceSpace (self , s : str ) -> str :
3
+ return s .replace (' ' , '%20' )
You can’t perform that action at this time.
0 commit comments