Skip to content

Commit 4587330

Browse files
committed
add solution for 13, 78, 704, 811 and remove 1737
1 parent 87f5c5c commit 4587330

7 files changed

+101
-86
lines changed

src/_0013_roman_to_integer.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn roman_to_int(s: String) -> i32 {
5+
let look_up = |c: &char| -> i32 {
6+
match c {
7+
'I' => 1,
8+
'V' => 5,
9+
'X' => 10,
10+
'L' => 50,
11+
'C' => 100,
12+
'D' => 500,
13+
'M' => 1000,
14+
_ => 0,
15+
}
16+
};
17+
18+
let chars = s.chars().collect::<Vec<char>>();
19+
let mut prev = 'I';
20+
let mut result = 0;
21+
for i in (0..s.len()).rev() {
22+
let c = &chars[i];
23+
if look_up(c) < look_up(&prev) {
24+
result -= look_up(c);
25+
} else {
26+
result += look_up(c);
27+
prev = *c;
28+
}
29+
}
30+
result
31+
}
32+
}
33+
34+
#[test]
35+
fn test() {
36+
assert_eq!(Solution::roman_to_int("III".to_string()), 3);
37+
assert_eq!(Solution::roman_to_int("LVIII".to_string()), 58);
38+
assert_eq!(Solution::roman_to_int("MCMXCIV".to_string()), 1994);
39+
}

src/_0078_subsets.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@ struct Solution;
22

33
impl Solution {
44
pub fn subsets(nums: Vec<i32>) -> Vec<Vec<i32>> {
5-
let mut result: Vec<Vec<i32>> = vec![];
6-
let mut current: Vec<i32> = vec![];
7-
let n: usize = nums.len();
8-
Self::dfs(&nums, 0, &n, &mut result, &mut current);
5+
let mut result = vec![];
6+
Self::dfs(&nums, 0, &mut vec![], &mut result);
97
result
108
}
119

12-
fn dfs(
13-
nums: &Vec<i32>,
14-
i: usize,
15-
n: &usize,
16-
result: &mut Vec<Vec<i32>>,
17-
current: &mut Vec<i32>,
18-
) {
19-
if i == *n {
20-
result.push(current.to_vec());
10+
fn dfs(nums: &Vec<i32>, i: usize, current: &mut Vec<i32>, result: &mut Vec<Vec<i32>>) {
11+
if i == nums.len() {
12+
result.push(current.clone());
2113
} else {
22-
Self::dfs(nums, i + 1, n, result, current);
2314
current.push(nums[i]);
24-
Self::dfs(nums, i + 1, n, result, current);
15+
Self::dfs(&nums, i + 1, current, result);
2516
current.pop();
17+
Self::dfs(&nums, i + 1, current, result);
2618
}
2719
}
2820
}

src/_0704_binary_search.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn search(nums: Vec<i32>, target: i32) -> i32 {
5+
let (mut i, mut j) = (0, (nums.len() - 1) as i32);
6+
while i <= j {
7+
let mid = (i + j) / 2;
8+
if nums[mid as usize] == target {
9+
return mid as i32;
10+
} else if nums[mid as usize] < target {
11+
i = mid + 1;
12+
} else {
13+
j = mid - 1;
14+
}
15+
}
16+
-1
17+
}
18+
}
19+
20+
#[test]
21+
fn test() {
22+
assert_eq!(Solution::search(vec![-1, 0, 3, 5, 9, 12], 9), 4);
23+
assert_eq!(Solution::search(vec![-1, 0, 3, 5, 9, 12], 2), -1);
24+
}

src/_0811_subdomain_visit_count.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// struct Solution;
2+
// use std::collections::HashMap;
3+
4+
// impl Solution {
5+
// pub fn subdomain_visits(cpdomains: Vec<String>) -> Vec<String> {
6+
// let mut map: HashMap<&str, i32> = HashMap::new();
7+
// for domain in cpdomains {
8+
// let domain_array: Vec<&str> = domain.split(" ").collect();
9+
// let occur: i32 = domain_array[0].parse().unwrap();
10+
// let d: Vec<&str> = domain_array[1].split(".").collect();
11+
// for v in d {
12+
// let count = map.entry(v).or_default();
13+
// *count += occur;
14+
// }
15+
// }
16+
// let mut result: Vec<String> = vec![];
17+
// for (domain, count) in map {
18+
// result.push(format!("{} {}", count, domain));
19+
// }
20+
// result
21+
// }
22+
// }
23+
24+
// #[test]
25+
// fn test() {
26+
// // assert_eq!(Solution::subdomain_visits(vec!["9001 discuss.leetcode.com".to_string()]), true);
27+
// }

src/_1737_change_minimum_characters_to_satisfy_one_of_three_conditions.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ mod _0009_palindrome_number;
2424
//
2525
mod _0011_container_with_most_water;
2626
//
27+
mod _0013_roman_to_integer;
28+
//
2729
mod _0014_longest_common_prefix;
2830
//
2931
mod _0015_3sum;
@@ -244,6 +246,8 @@ mod _0692_top_k_frequent_words;
244246
//
245247
mod _0696_count_binary_substrings;
246248
//
249+
mod _0704_binary_search;
250+
//
247251
mod _0724_find_pivot_index;
248252
//
249253
mod _0735_asteroid_collision;
@@ -380,8 +384,6 @@ mod _1726_tuple_with_same_product;
380384
//
381385
mod _1736_latest_time_by_replacing_hidden_digits;
382386
//
383-
mod _1737_change_minimum_characters_to_satisfy_one_of_three_conditions;
384-
//
385387
mod _1742_maximum_number_of_balls_in_a_box;
386388
//
387389
mod _1748_sum_of_unique_elements;

src/test.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)