Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2754721

Browse files
author
guangsheng.li01
committed
Solved 0215
1 parent 1fb33f2 commit 2754721

File tree

4 files changed

+203
-56
lines changed

4 files changed

+203
-56
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,11 @@ doctest = false
1717
url = "2.1.1"
1818
handlebars = "~3.0.0"
1919
serde_json = "1.0"
20+
# criterion = "0.3"
21+
# bencher = "0.1.5"
22+
23+
# [[bench]]
24+
# name = "my_benchmark"
25+
# harness = false
2026

2127

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* [0215] kth-largest-element-in-an-array
3+
*/
4+
5+
struct Solution;
6+
7+
impl Solution {
8+
pub fn find_kth_largest(nums: Vec<i32>, k: i32) -> i32 {
9+
let mut nums = nums;
10+
let end = nums.len() - 1;
11+
Self::quick_select(&mut nums, 0usize, end, k as usize)
12+
}
13+
14+
fn quick_select(nums: &mut Vec<i32>, start: usize, end: usize, k: usize) -> i32 {
15+
let p = nums[start];
16+
let mut left = start + 1;
17+
let mut right = end;
18+
19+
while left <= right {
20+
while left <= right && nums[left] >= p {
21+
left += 1;
22+
}
23+
while left <= right && nums[right] <= p {
24+
right -= 1;
25+
}
26+
if left <= right && nums[left] < p && nums[right] > p {
27+
nums.swap(left, right);
28+
}
29+
}
30+
nums.swap(start, right);
31+
if right - start == k - 1 {
32+
let mut min = nums[0];
33+
for i in start..start + k as usize {
34+
if nums[i] < min {
35+
min = nums[i];
36+
}
37+
}
38+
return min;
39+
} else if right - start < k - 1 {
40+
return Self::quick_select(nums, right + 1, end, k + start - right - 1);
41+
} else if right - start > k - 1 {
42+
return Self::quick_select(nums, start, right - 1, k);
43+
}
44+
0
45+
}
46+
}
47+
48+
#[cfg(test)]
49+
mod tests {
50+
use super::*;
51+
52+
#[test]
53+
fn test_case0() {
54+
assert_eq!(
55+
Solution::find_kth_largest(
56+
vec![7, 3, 82, 9, 2, 17, 4, 29, 1, 8, 36, 33, 18, 22, 19],
57+
2
58+
),
59+
36
60+
);
61+
assert_eq!(Solution::find_kth_largest(vec![7, 6, 5, 4, 3, 2, 1], 5), 3);
62+
assert_eq!(Solution::find_kth_largest(vec![5, 2, 4, 1, 3, 6, 0], 4), 3)
63+
}
64+
}

src/a0226_invert_binary_tree.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct Solution {}
2828
use std::cell::RefCell;
2929
use std::rc::Rc;
3030
impl Solution {
31+
// 使用clone
3132
pub fn invert_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
3233
if let Some(root) = root {
3334
let mut root_mut = root.borrow_mut();
@@ -44,6 +45,41 @@ impl Solution {
4445
}
4546
Option::None
4647
}
48+
49+
// 使用unsafe
50+
pub fn invert_tree_2(mut root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
51+
if root.is_none() {
52+
return None;
53+
}
54+
55+
let ref_mut = root.as_mut().unwrap().as_ptr();
56+
unsafe {
57+
let l = &mut (*ref_mut).left;
58+
let r = &mut (*ref_mut).right;
59+
std::mem::swap(l, r);
60+
if l.is_some() {
61+
Self::invert_tree(Some(l.as_mut().unwrap().clone()));
62+
}
63+
if r.is_some() {
64+
Self::invert_tree(Some(r.as_mut().unwrap().clone()));
65+
}
66+
}
67+
68+
root
69+
}
70+
71+
// 使用take
72+
pub fn invert_tree_3(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
73+
if root.is_none() {
74+
return None;
75+
}
76+
let root = root.unwrap();
77+
let l = Self::invert_tree(root.borrow_mut().left.take());
78+
let r = Self::invert_tree(root.borrow_mut().right.take());
79+
root.borrow_mut().left = r;
80+
root.borrow_mut().right = l;
81+
Some(root)
82+
}
4783
}
4884

4985
// solution impl ends here
@@ -53,6 +89,7 @@ impl Solution {
5389
#[cfg(test)]
5490
mod tests {
5591
use super::*;
92+
use test::Bencher;
5693

5794
#[test]
5895
fn test_case0() {
@@ -61,6 +98,42 @@ mod tests {
6198
tree![4, 7, 2, 9, 6, 3, 1]
6299
);
63100
}
101+
102+
#[bench]
103+
fn bench_invert_tree(b: &mut Bencher) {
104+
let t = tree![
105+
4, 1, 4, 6, 8, 9, 5, 3, 1, 3, 5, 6, 7, 8, 9, 7, 65, 3, 2, 2, 3, 4, 45, 6, 6, 78, 9, 0,
106+
8, 7, 6, 67, 8, 9, 9, 7, 5654, 43, 3, 2, 1, 1, 2, 3, 4, 4, 45, 5, 6, 6, 7, 78, 8, 76,
107+
5, 4, 67, 8, 9, 90, 009, 7, 65, 87, 654, 356, 789, 87, 6543, 4, 567, 89, 765, 4, 567,
108+
89, 87, 654, 35, 6789, 0, 876, 543, 4, 5678, 90, 876, 5432, 1, 345, 67890, 9876, 54321,
109+
2, 345, 6789, 87, 65432, 1, 234, 5678, 2, 7, 1, 3, 6, 9
110+
];
111+
b.iter(|| Solution::invert_tree(t.clone()));
112+
}
113+
114+
#[bench]
115+
fn bench_invert_tree_2(b: &mut Bencher) {
116+
let t = tree![
117+
4, 1, 4, 6, 8, 9, 5, 3, 1, 3, 5, 6, 7, 8, 9, 7, 65, 3, 2, 2, 3, 4, 45, 6, 6, 78, 9, 0,
118+
8, 7, 6, 67, 8, 9, 9, 7, 5654, 43, 3, 2, 1, 1, 2, 3, 4, 4, 45, 5, 6, 6, 7, 78, 8, 76,
119+
5, 4, 67, 8, 9, 90, 009, 7, 65, 87, 654, 356, 789, 87, 6543, 4, 567, 89, 765, 4, 567,
120+
89, 87, 654, 35, 6789, 0, 876, 543, 4, 5678, 90, 876, 5432, 1, 345, 67890, 9876, 54321,
121+
2, 345, 6789, 87, 65432, 1, 234, 5678, 2, 7, 1, 3, 6, 9
122+
];
123+
b.iter(|| Solution::invert_tree_2(t.clone()));
124+
}
125+
126+
#[bench]
127+
fn bench_invert_tree_3(b: &mut Bencher) {
128+
let t = tree![
129+
4, 1, 4, 6, 8, 9, 5, 3, 1, 3, 5, 6, 7, 8, 9, 7, 65, 3, 2, 2, 3, 4, 45, 6, 6, 78, 9, 0,
130+
8, 7, 6, 67, 8, 9, 9, 7, 5654, 43, 3, 2, 1, 1, 2, 3, 4, 4, 45, 5, 6, 6, 7, 78, 8, 76,
131+
5, 4, 67, 8, 9, 90, 009, 7, 65, 87, 654, 356, 789, 87, 6543, 4, 567, 89, 765, 4, 567,
132+
89, 87, 654, 35, 6789, 0, 876, 543, 4, 5678, 90, 876, 5432, 1, 345, 67890, 9876, 54321,
133+
2, 345, 6789, 87, 65432, 1, 234, 5678, 2, 7, 1, 3, 6, 9
134+
];
135+
b.iter(|| Solution::invert_tree_3(t.clone()));
136+
}
64137
}
65138

66139
// solution tests ends here

src/lib.rs

Lines changed: 60 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
11
#![allow(dead_code)]
2+
#![feature(test)]
3+
4+
extern crate test;
25

36
#[macro_use]
47
pub mod utils;
58

6-
mod a0000_sample;
7-
mod a0001_two_sum;
8-
mod a0004_median_of_two_sorted_arrays;
9-
mod a0007_reverse_integer;
10-
mod a0009_palindrome_number;
11-
mod a0019_binary_tree_inorder_traversal;
12-
mod a0026_remove_duplicates_from_sorted_array;
13-
mod a0027_remove_element;
14-
mod a0035_search_insert_position;
15-
mod a0042_trapping_rain_water;
16-
mod a0053_maximum_subarray;
17-
mod a0066_plus_one;
18-
mod a0088_merge_sorted_array;
19-
mod a0103_binary_tree_zigzag_level_order_traversal;
20-
mod a0118_pascals_triangle;
21-
mod a0119_pascals_triangle_ii;
22-
mod a0145_binary_tree_postorder_traversal;
23-
mod a0169_majority_element;
24-
mod a0172_factorial_trailing_zeroes;
25-
mod a0226_invert_binary_tree;
26-
mod a0322_coin_change;
27-
mod a0343_integer_break;
28-
mod a0392_is_subsequence;
29-
mod a0400_nth_digit;
30-
mod a0404_sum_of_left_leaves;
31-
mod a0405_convert_a_number_to_hexadecimal;
32-
mod a0617_merge_two_binary_trees;
33-
mod a0643_maximum_average_subarray_i;
34-
mod a0646_maximum_length_of_pair_chain;
35-
mod a0654_maximum_binary_tree;
36-
mod a0701_insert_into_a_binary_search_tree;
37-
mod a0726_number_of_atoms;
38-
mod a0761_special_binary_string;
39-
mod a0779_k_th_symbol_in_grammar;
40-
mod a0783_minimum_distance_between_bst_nodes;
41-
mod a0867_transpose_matrix;
42-
mod a0883_projection_area_of_3d_shapes;
43-
mod a0894_all_possible_full_binary_trees;
44-
mod a0929_unique_email_addresses;
45-
mod a0931_minimum_falling_path_sum;
46-
mod a0937_reorder_data_in_log_files;
47-
mod a0938_range_sum_of_bst;
48-
mod a0949_largest_time_for_given_digits;
49-
mod a0973_k_closest_points_to_origin;
50-
mod a0980_unique_paths_iii;
51-
mod a1012_numbers_with_repeated_digits;
52-
mod a1018_binary_prefix_divisible_by_5;
53-
mod a1021_remove_outermost_parentheses;
54-
mod a1047_remove_all_adjacent_duplicates_in_string;
55-
mod a1137_n_th_tribonacci_number;
56-
mod a1200_minimum_absolute_difference;
57-
mod a1287_element_appearing_more_than_25_in_sorted_array;
58-
mod a1302_deepest_leaves_sum;
59-
mod a1315_sum_of_nodes_with_even_valued_grandparent;
60-
mod a1317_convert_integer_to_the_sum_of_two_no_zero_integers;
61-
mod casting_between_types;
9+
pub mod a0000_sample;
10+
pub mod a0001_two_sum;
11+
pub mod a0004_median_of_two_sorted_arrays;
12+
pub mod a0007_reverse_integer;
13+
pub mod a0009_palindrome_number;
14+
pub mod a0019_binary_tree_inorder_traversal;
15+
pub mod a0026_remove_duplicates_from_sorted_array;
16+
pub mod a0027_remove_element;
17+
pub mod a0035_search_insert_position;
18+
pub mod a0042_trapping_rain_water;
19+
pub mod a0053_maximum_subarray;
20+
pub mod a0066_plus_one;
21+
pub mod a0088_merge_sorted_array;
22+
pub mod a0103_binary_tree_zigzag_level_order_traversal;
23+
pub mod a0118_pascals_triangle;
24+
pub mod a0119_pascals_triangle_ii;
25+
pub mod a0145_binary_tree_postorder_traversal;
26+
pub mod a0169_majority_element;
27+
pub mod a0172_factorial_trailing_zeroes;
28+
pub mod a0215_kth_largest_element_in_an_array;
29+
pub mod a0226_invert_binary_tree;
30+
pub mod a0322_coin_change;
31+
pub mod a0343_integer_break;
32+
pub mod a0392_is_subsequence;
33+
pub mod a0400_nth_digit;
34+
pub mod a0404_sum_of_left_leaves;
35+
pub mod a0405_convert_a_number_to_hexadecimal;
36+
pub mod a0617_merge_two_binary_trees;
37+
pub mod a0643_maximum_average_subarray_i;
38+
pub mod a0646_maximum_length_of_pair_chain;
39+
pub mod a0654_maximum_binary_tree;
40+
pub mod a0701_insert_into_a_binary_search_tree;
41+
pub mod a0726_number_of_atoms;
42+
pub mod a0761_special_binary_string;
43+
pub mod a0779_k_th_symbol_in_grammar;
44+
pub mod a0783_minimum_distance_between_bst_nodes;
45+
pub mod a0867_transpose_matrix;
46+
pub mod a0883_projection_area_of_3d_shapes;
47+
pub mod a0894_all_possible_full_binary_trees;
48+
pub mod a0929_unique_email_addresses;
49+
pub mod a0931_minimum_falling_path_sum;
50+
pub mod a0937_reorder_data_in_log_files;
51+
pub mod a0938_range_sum_of_bst;
52+
pub mod a0949_largest_time_for_given_digits;
53+
pub mod a0973_k_closest_points_to_origin;
54+
pub mod a0980_unique_paths_iii;
55+
pub mod a1012_numbers_with_repeated_digits;
56+
pub mod a1018_binary_prefix_divisible_by_5;
57+
pub mod a1021_remove_outermost_parentheses;
58+
pub mod a1047_remove_all_adjacent_duplicates_in_string;
59+
pub mod a1137_n_th_tribonacci_number;
60+
pub mod a1200_minimum_absolute_difference;
61+
pub mod a1287_element_appearing_more_than_25_in_sorted_array;
62+
pub mod a1302_deepest_leaves_sum;
63+
pub mod a1315_sum_of_nodes_with_even_valued_grandparent;
64+
pub mod a1317_convert_integer_to_the_sum_of_two_no_zero_integers;
65+
pub mod casting_between_types;

0 commit comments

Comments
 (0)