File tree Expand file tree Collapse file tree 2 files changed +55
-2
lines changed Expand file tree Collapse file tree 2 files changed +55
-2
lines changed Original file line number Diff line number Diff line change @@ -29,4 +29,41 @@ impl Solution {
2929
3030```
3131
32- ## 学习感想
32+ ## 学习感想
33+
34+ Rust 一个char的大小永远就是4字节,在&str里,永远都是utf8编码的,但是占用的长度可能是1-4个字节
35+
36+ Sequences: Vec, VecDeque, LinkedList
37+ Maps: HashMap, BTreeMap
38+ Sets: HashSet, BTreeSet
39+ Misc: BinaryHeap
40+
41+ Borrow 的一个常见应用场景是 HashMap 之类的容器类型。在 HashMap 中,可以用 Borrow 来允许使用不同但兼容的类型进行查找或删除操作。比如,HashMap<String, V> 可以接受 &str 作为键,因为 String 实现了 Borrow<str >。
42+
43+ impl 块 在 Rust 中是全局可见的,因为它定义了类型的行为,Rust 需要确保这些行为在整个程序中保持一致。
44+ use 语句 只在局部可见,这样可以避免命名冲突,并且保持模块系统的封装性和灵活性。
45+
46+ ``` rust
47+ # struct Solution {}
48+ impl Solution {
49+ pub fn is_anagram (s : String , t : String ) -> bool {
50+ use std :: collections :: HashMap ;
51+ let mut m : HashMap <char , usize > = HashMap :: new ();
52+ for ch in s . chars () {
53+ m . entry (ch ). and_modify (| x | * x += 1usize ). or_insert (1usize );
54+ }
55+ for ch in t . chars () {
56+ use std :: collections :: hash_map :: Entry ;
57+ match m . entry (ch ) {
58+ Entry :: Occupied (mut o ) => {
59+ let x : & mut usize = o . get_mut ();
60+ * x -= 1usize ;
61+ if * x == 0usize { o . remove (); }
62+ },
63+ Entry :: Vacant (_ ) => { return false },
64+ }
65+ }
66+ m . len () == 0
67+ }
68+ }
69+ ```
Original file line number Diff line number Diff line change @@ -23,4 +23,20 @@ impl Solution {
2323}
2424
2525```
26- ## 学习感想
26+ ## 学习感想
27+
28+ 优雅
29+
30+ ``` rust
31+ # struct Solution {}
32+
33+ impl Solution {
34+ pub fn intersection (nums1 : Vec <i32 >, nums2 : Vec <i32 >) -> Vec <i32 > {
35+ use std :: collections :: HashSet ;
36+ let set1 : HashSet <i32 > = HashSet :: from_iter (nums1 . into_iter ());
37+ let set2 : HashSet <i32 > = HashSet :: from_iter (nums2 . into_iter ());
38+ set1 . intersection (& set2 ). map (| & x | x ). collect ()
39+ }
40+ }
41+
42+ ```
You can’t perform that action at this time.
0 commit comments