Skip to content

Commit a297b95

Browse files
committed
solve #74
1 parent cbe0df7 commit a297b95

File tree

3 files changed

+159
-0
lines changed

3 files changed

+159
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,5 @@ mod n0069_sqrtx;
7373
mod n0070_climbing_stairs;
7474
mod n0071_simplify_path;
7575
mod n0072_edit_distance;
76+
mod n0073_set_matrix_zeroes;
77+
mod n0074_search_a_2d_matrix;

src/n0073_set_matrix_zeroes.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* [73] Set Matrix Zeroes
3+
*
4+
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a>.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input:
10+
* [
11+
* [1,1,1],
12+
* [1,0,1],
13+
* [1,1,1]
14+
* ]
15+
* Output:
16+
* [
17+
* [1,0,1],
18+
* [0,0,0],
19+
* [1,0,1]
20+
* ]
21+
*
22+
*
23+
* Example 2:
24+
*
25+
*
26+
* Input:
27+
* [
28+
* [0,1,2,0],
29+
* [3,4,5,2],
30+
* [1,3,1,5]
31+
* ]
32+
* Output:
33+
* [
34+
* [0,0,0,0],
35+
* [0,4,5,0],
36+
* [0,3,1,0]
37+
* ]
38+
*
39+
*
40+
* Follow up:
41+
*
42+
*
43+
* A straight forward solution using O(mn) space is probably a bad idea.
44+
* A simple improvement uses O(m + n) space, but still not the best solution.
45+
* Could you devise a constant space solution?
46+
*
47+
*
48+
*/
49+
pub struct Solution {}
50+
51+
// submission codes start here
52+
53+
impl Solution {
54+
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
55+
56+
}
57+
}
58+
59+
// submission codes end
60+
61+
#[cfg(test)]
62+
mod tests {
63+
use super::*;
64+
65+
#[test]
66+
fn test_73() {
67+
}
68+
}

src/n0074_search_a_2d_matrix.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* [74] Search a 2D Matrix
3+
*
4+
* Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
5+
*
6+
*
7+
* Integers in each row are sorted from left to right.
8+
* The first integer of each row is greater than the last integer of the previous row.
9+
*
10+
*
11+
* Example 1:
12+
*
13+
*
14+
* Input:
15+
* matrix = [
16+
* [1, 3, 5, 7],
17+
* [10, 11, 16, 20],
18+
* [23, 30, 34, 50]
19+
* ]
20+
* target = 3
21+
* Output: true
22+
*
23+
*
24+
* Example 2:
25+
*
26+
*
27+
* Input:
28+
* matrix = [
29+
* [1, 3, 5, 7],
30+
* [10, 11, 16, 20],
31+
* [23, 30, 34, 50]
32+
* ]
33+
* target = 13
34+
* Output: false
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
impl Solution {
40+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
41+
if matrix.is_empty() { return false }
42+
let (height, width) = (matrix.len(), matrix[0].len());
43+
if height < 1 || width < 1 { return false }
44+
let mut size = height * width;
45+
let mut base = 0_usize;
46+
while size > 1 {
47+
let half = size / 2;
48+
let mid = base + half;
49+
if target == matrix[mid/width][mid%width] {
50+
return true
51+
} else if (target > matrix[mid/width][mid%width]) {
52+
base = mid;
53+
}
54+
size -= half;
55+
}
56+
target == matrix[base/width][base%width]
57+
}
58+
}
59+
60+
// submission codes end
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
fn test_74() {
68+
assert_eq!(
69+
Solution::search_matrix(
70+
vec![
71+
vec![1, 3, 5, 7],
72+
vec![10, 11, 16, 20],
73+
vec![23, 30, 34, 50]
74+
], 3
75+
),
76+
true
77+
);
78+
assert_eq!(
79+
Solution::search_matrix(
80+
vec![
81+
vec![1, 3, 5, 7],
82+
vec![10, 11, 16, 20],
83+
vec![23, 30, 34, 50]
84+
], 13
85+
),
86+
false
87+
);
88+
}
89+
}

0 commit comments

Comments
 (0)