Skip to content

Commit 980c4bb

Browse files
committed
solve #77 and suppress warning of unused code
1 parent 810d73f commit 980c4bb

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
File renamed without changes.

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,5 @@ mod n0072_edit_distance;
7676
mod n0073_set_matrix_zeroes;
7777
mod n0074_search_a_2d_matrix;
7878
mod n0075_sort_colors;
79+
mod n0076_minimum_window_substring;
80+
mod n0077_combinations;

src/n0076_minimum_window_substring.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* [76] Minimum Window Substring
3+
*
4+
* Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: S = "ADOBECODEBANC", T = "ABC"
10+
* Output: "BANC"
11+
*
12+
*
13+
* Note:
14+
*
15+
*
16+
* If there is no such window in S that covers all characters in T, return the empty string "".
17+
* If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
18+
*
19+
*
20+
*/
21+
pub struct Solution {}
22+
23+
// submission codes start here
24+
use std::collections::HashMap;
25+
impl Solution {
26+
pub fn min_window(s: String, t: String) -> String {
27+
if t.is_empty() || t.len() > s.len() {
28+
return "".to_owned()
29+
}
30+
let (mut start, mut end) = (0_usize, 0_usize);
31+
let mut result = (0_usize,0_usize);
32+
loop {
33+
34+
}
35+
s[result.0..result.1].to_owned()
36+
}
37+
38+
fn count_char(s: String) -> HashMap<char, i32> {
39+
let mut res = HashMap::new();
40+
for ch in s.chars().into_iter() {
41+
*res.entry(ch).or_insert(0) += 1;
42+
}
43+
res
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn test_76() {
55+
}
56+
}

src/n0077_combinations.rs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* [77] Combinations
3+
*
4+
* Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: n = 4, k = 2
10+
* Output:
11+
* [
12+
* [2,4],
13+
* [3,4],
14+
* [2,3],
15+
* [1,2],
16+
* [1,3],
17+
* [1,4],
18+
* ]
19+
*
20+
*
21+
*/
22+
pub struct Solution {}
23+
24+
// submission codes start here
25+
26+
impl Solution {
27+
pub fn combine(n: i32, k: i32) -> Vec<Vec<i32>> {
28+
let mut res: Vec<Vec<i32>> = Vec::new();
29+
Solution::backtrack(1, n, k, vec![], &mut res);
30+
res
31+
}
32+
33+
fn backtrack(start: i32, end: i32, k: i32, curr: Vec<i32>, result: &mut Vec<Vec<i32>>) {
34+
if k < 1 {
35+
result.push(curr);
36+
return
37+
}
38+
if end - start + 1 < k {
39+
// elements is not enough, return quickly
40+
return
41+
}
42+
for i in start..end+1 {
43+
let mut vec = curr.clone();
44+
vec.push(i);
45+
Solution::backtrack(i+1, end, k-1, vec, result);
46+
}
47+
}
48+
}
49+
50+
// submission codes end
51+
52+
#[cfg(test)]
53+
mod tests {
54+
use super::*;
55+
56+
#[test]
57+
fn test_77() {
58+
assert_eq!(
59+
Solution::combine(4, 2),
60+
vec![vec![1, 2], vec![1, 3], vec![1, 4], vec![2, 3], vec![2, 4], vec![3, 4]]
61+
);
62+
assert_eq!(
63+
Solution::combine(1, 1),
64+
vec![vec![1]]
65+
);
66+
let empty: Vec<Vec<i32>> = vec![];
67+
assert_eq!(
68+
Solution::combine(0, 1),
69+
empty
70+
);
71+
assert_eq!(
72+
Solution::combine(2, 1),
73+
vec![vec![1], vec![2]]
74+
);
75+
}
76+
}

0 commit comments

Comments
 (0)