File tree Expand file tree Collapse file tree 3 files changed +85
-3
lines changed Expand file tree Collapse file tree 3 files changed +85
-3
lines changed Original file line number Diff line number Diff line change @@ -41,4 +41,30 @@ impl Solution {
4141}
4242```
4343
44- ## 学习感想
44+ ## 学习感想
45+
46+
47+ ``` rust
48+
49+ struct Solution {}
50+ impl Solution {
51+ pub fn is_valid (s : String ) -> bool {
52+ let mut stack : Vec <char > = Vec :: new ();
53+ for c in s . chars () {
54+ if ['{' , '(' , '[' ]. contains (& c ) {
55+ stack . push (c );
56+ } else {
57+ if let Some (x ) = stack . pop () {
58+ if x == '{' && c != '}' { return false }
59+ else if x == '(' && c != ')' { return false }
60+ else if x == '[' && c != ']' { return false }
61+ } else {
62+ return false
63+ }
64+ }
65+ }
66+ stack . is_empty ()
67+ }
68+ }
69+
70+ ```
Original file line number Diff line number Diff line change @@ -38,4 +38,33 @@ impl Solution {
3838
3939## 学习感想
4040
41- 一开始确实以为大顶堆就行了,其实要用单调栈,之前也有做过单调栈的题目
41+ 一开始确实以为大顶堆就行了,其实要用单调栈,之前也有做过单调栈的题目
42+
43+ 单调队列是不能够删除一个指定的元素的,单调栈里维护需要的最大值
44+
45+ ``` rust
46+ struct Solution {}
47+ impl Solution {
48+ pub fn max_sliding_window (nums : Vec <i32 >, k : i32 ) -> Vec <i32 > {
49+ use std :: collections :: VecDeque ;
50+ let mut v : VecDeque <i32 > = VecDeque :: new ();
51+ let k : usize = k as usize ;
52+ let mut res : Vec <i32 > = vec! [];
53+ for idx in 0 .. nums . len () {
54+ if idx >= k {
55+ let to_delete : i32 = nums [idx - k ];
56+ if to_delete == v [0 ] { v . pop_front (); }
57+ }
58+ let cur : i32 = nums [idx ];
59+ while let Some (& bck ) = v . back () {
60+ if bck < cur {
61+ v . pop_back ();
62+ } else { break }
63+ }
64+ v . push_back (cur );
65+ if idx >= k - 1 { res . push (v [0 ]); }
66+ }
67+ res
68+ }
69+ }
70+ ```
Original file line number Diff line number Diff line change @@ -39,4 +39,31 @@ impl Solution {
3939
4040## 学习感想
4141
42- map+小顶堆
42+ map+小顶堆
43+
44+
45+
46+ ``` rust
47+ struct Solution {}
48+
49+ impl Solution {
50+ pub fn top_k_frequent (nums : Vec <i32 >, k : i32 ) -> Vec <i32 > {
51+ use std :: collections :: {HashMap , BinaryHeap };
52+ let mut map : HashMap <i32 , usize > = HashMap :: new ();
53+ nums . iter (). for_each (| & i | {
54+ map . entry (i ). and_modify (| x | * x += 1usize ). or_default ();
55+ });
56+ let mut res : Vec <i32 > = Vec :: new ();
57+ let mut pq : BinaryHeap <(usize , i32 )> = BinaryHeap :: new ();
58+ for (k , v ) in map {
59+ pq . push ((v , k ));
60+ }
61+ (0i32 .. k ) . for_each (| _ | {
62+ res . push (pq . pop (). unwrap (). 1 );
63+ });
64+ res
65+ }
66+ }
67+
68+
69+ ```
You can’t perform that action at this time.
0 commit comments