Skip to content

Commit f686ba4

Browse files
committed
fix: file structure
1 parent 3e09deb commit f686ba4

File tree

4 files changed

+80
-84
lines changed

4 files changed

+80
-84
lines changed

src/solution/lc228.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
mod lc228 {
2-
pub struct Solution;
3-
impl Solution {
4-
pub fn summary_ranges(nums: Vec<i32>) -> Vec<String> {
5-
let mut res = Vec::new();
6-
let mut left = 0;
7-
let mut right = 0;
8-
let n = nums.len();
9-
while left < n {
10-
while right < n && nums[right] - nums[left] == (right - left) as i32 {
11-
right += 1;
12-
}
1+
use crate::solution::Solution;
132

14-
res.push(if right - left == 1 {
15-
nums[left].to_string()
16-
} else {
17-
format!("{}->{}", nums[left], nums[right - 1])
18-
});
19-
left = right;
3+
impl Solution {
4+
pub fn summary_ranges(nums: Vec<i32>) -> Vec<String> {
5+
let mut res = Vec::new();
6+
let mut left = 0;
7+
let mut right = 0;
8+
let n = nums.len();
9+
while left < n {
10+
while right < n && nums[right] - nums[left] == (right - left) as i32 {
11+
right += 1;
2012
}
2113

22-
res
14+
res.push(if right - left == 1 {
15+
nums[left].to_string()
16+
} else {
17+
format!("{}->{}", nums[left], nums[right - 1])
18+
});
19+
left = right;
2320
}
21+
22+
res
2423
}
2524
}
2625

@@ -46,6 +45,6 @@ fn test() {
4645
},
4746
];
4847
for test in tests {
49-
assert_eq!(lc228::Solution::summary_ranges(test.nums), test.expect);
48+
assert_eq!(Solution::summary_ranges(test.nums), test.expect);
5049
}
5150
}

src/solution/lc56.rs

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
mod lc56 {
2-
use std::cmp::max;
1+
use crate::solution::Solution;
2+
use std::cmp::max;
33

4-
pub struct Solution;
5-
impl Solution {
6-
pub fn merge(mut intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
7-
let mut res = Vec::new();
8-
intervals.sort_by(|a, b| {
9-
if a[0] == b[0] {
10-
b[1].cmp(&a[1])
11-
} else {
12-
a[0].cmp(&b[0])
13-
}
14-
});
15-
16-
let mut left = 0;
17-
let mut right = 0;
18-
let n = intervals.len();
19-
while left < n {
20-
let mut upper = intervals[left][1];
21-
while right < n && intervals[right][0] <= upper {
22-
upper = max(upper, intervals[right][1]);
23-
right += 1;
24-
}
4+
impl Solution {
5+
pub fn merge(mut intervals: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
6+
let mut res = Vec::new();
7+
intervals.sort_by(|a, b| {
8+
if a[0] == b[0] {
9+
b[1].cmp(&a[1])
10+
} else {
11+
a[0].cmp(&b[0])
12+
}
13+
});
2514

26-
res.push(vec![intervals[left][0], upper]);
27-
left = right;
15+
let mut left = 0;
16+
let mut right = 0;
17+
let n = intervals.len();
18+
while left < n {
19+
let mut upper = intervals[left][1];
20+
while right < n && intervals[right][0] <= upper {
21+
upper = max(upper, intervals[right][1]);
22+
right += 1;
2823
}
2924

30-
res
25+
res.push(vec![intervals[left][0], upper]);
26+
left = right;
3127
}
28+
29+
res
3230
}
3331
}
3432

@@ -69,6 +67,6 @@ fn test() {
6967
];
7068

7169
for test in tests {
72-
assert_eq!(lc56::Solution::merge(test.intervals), test.expect);
70+
assert_eq!(Solution::merge(test.intervals), test.expect);
7371
}
7472
}

src/solution/lc70.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
mod lc70 {
2-
pub struct Solution;
3-
impl Solution {
4-
pub fn climb_stairs(n: i32) -> i32 {
5-
if n < 4 {
6-
return n;
7-
}
1+
use crate::solution::Solution;
82

9-
let mut sum = vec![0; n as usize + 1];
10-
sum[1] = 1;
11-
sum[2] = 2;
12-
for i in 3..n + 1 {
13-
let idx = i as usize;
14-
sum[idx] = sum[idx - 1] + sum[idx - 2];
15-
}
3+
impl Solution {
4+
pub fn climb_stairs(n: i32) -> i32 {
5+
if n < 4 {
6+
return n;
7+
}
168

17-
println!("{:?}", sum);
18-
sum[n as usize]
9+
let mut sum = vec![0; n as usize + 1];
10+
sum[1] = 1;
11+
sum[2] = 2;
12+
for i in 3..n + 1 {
13+
let idx = i as usize;
14+
sum[idx] = sum[idx - 1] + sum[idx - 2];
1915
}
16+
17+
println!("{:?}", sum);
18+
sum[n as usize]
2019
}
2120
}
2221

2322
#[test]
2423
fn test() {
25-
assert_eq!(lc70::Solution::climb_stairs(2), 2);
26-
assert_eq!(lc70::Solution::climb_stairs(4), 5);
24+
assert_eq!(Solution::climb_stairs(2), 2);
25+
assert_eq!(Solution::climb_stairs(4), 5);
2726
}

src/solution/lc74.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
mod lc74 {
2-
pub struct Solution;
3-
impl Solution {
4-
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
5-
let m = matrix.len() as i32;
6-
let n = matrix[0].len() as i32;
7-
let mut x = 0_i32;
8-
let mut y = n as i32 - 1;
9-
while x < m && y >= 0 {
10-
if matrix[x as usize][y as usize] == target {
11-
return true;
12-
}
1+
#![allow(dead_code)]
132

14-
if matrix[x as usize][y as usize] < target {
15-
x += 1;
16-
} else {
17-
y -= 1;
18-
}
3+
pub struct Solution {}
4+
impl Solution {
5+
pub fn search_matrix(matrix: Vec<Vec<i32>>, target: i32) -> bool {
6+
let m = matrix.len() as i32;
7+
let n = matrix[0].len() as i32;
8+
let mut x = 0_i32;
9+
let mut y = n as i32 - 1;
10+
while x < m && y >= 0 {
11+
if matrix[x as usize][y as usize] == target {
12+
return true;
1913
}
2014

21-
false
15+
if matrix[x as usize][y as usize] < target {
16+
x += 1;
17+
} else {
18+
y -= 1;
19+
}
2220
}
21+
22+
false
2323
}
2424
}
2525

2626
#[test]
2727
fn test() {
2828
let matrix = vec![vec![1, 3, 5, 7], vec![10, 11, 16, 20], vec![23, 30, 34, 60]];
29-
assert_eq!(lc74::Solution::search_matrix(matrix, 3), true);
29+
assert_eq!(Solution::search_matrix(matrix, 3), true);
3030
}

0 commit comments

Comments
 (0)