diff --git a/solution/2000-2099/2012.Sum of Beauty in the Array/README.md b/solution/2000-2099/2012.Sum of Beauty in the Array/README.md index 1dbeeb99ae4fa..204a16cb86afc 100644 --- a/solution/2000-2099/2012.Sum of Beauty in the Array/README.md +++ b/solution/2000-2099/2012.Sum of Beauty in the Array/README.md @@ -202,6 +202,33 @@ function sumOfBeauties(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn sum_of_beauties(nums: Vec) -> i32 { + let n = nums.len(); + let mut right: Vec = vec![0; n]; + right[n - 1] = nums[n - 1]; + for i in (1..n - 1).rev() { + right[i] = right[i + 1].min(nums[i]); + } + let mut ans = 0; + let mut l = nums[0]; + for i in 1..n - 1 { + let r = right[i + 1]; + if l < nums[i] && nums[i] < r { + ans += 2; + } else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] { + ans += 1; + } + l = l.max(nums[i]); + } + ans + } +} +``` + diff --git a/solution/2000-2099/2012.Sum of Beauty in the Array/README_EN.md b/solution/2000-2099/2012.Sum of Beauty in the Array/README_EN.md index 56cc3dfeae47e..08058860f164a 100644 --- a/solution/2000-2099/2012.Sum of Beauty in the Array/README_EN.md +++ b/solution/2000-2099/2012.Sum of Beauty in the Array/README_EN.md @@ -203,6 +203,33 @@ function sumOfBeauties(nums: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn sum_of_beauties(nums: Vec) -> i32 { + let n = nums.len(); + let mut right: Vec = vec![0; n]; + right[n - 1] = nums[n - 1]; + for i in (1..n - 1).rev() { + right[i] = right[i + 1].min(nums[i]); + } + let mut ans = 0; + let mut l = nums[0]; + for i in 1..n - 1 { + let r = right[i + 1]; + if l < nums[i] && nums[i] < r { + ans += 2; + } else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] { + ans += 1; + } + l = l.max(nums[i]); + } + ans + } +} +``` + diff --git a/solution/2000-2099/2012.Sum of Beauty in the Array/Solution.rs b/solution/2000-2099/2012.Sum of Beauty in the Array/Solution.rs new file mode 100644 index 0000000000000..de718f47a2d2a --- /dev/null +++ b/solution/2000-2099/2012.Sum of Beauty in the Array/Solution.rs @@ -0,0 +1,22 @@ +impl Solution { + pub fn sum_of_beauties(nums: Vec) -> i32 { + let n = nums.len(); + let mut right: Vec = vec![0; n]; + right[n - 1] = nums[n - 1]; + for i in (1..n - 1).rev() { + right[i] = right[i + 1].min(nums[i]); + } + let mut ans = 0; + let mut l = nums[0]; + for i in 1..n - 1 { + let r = right[i + 1]; + if l < nums[i] && nums[i] < r { + ans += 2; + } else if nums[i - 1] < nums[i] && nums[i] < nums[i + 1] { + ans += 1; + } + l = l.max(nums[i]); + } + ans + } +} diff --git a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README.md b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README.md index b22b64c660993..caae8d9331f8b 100644 --- a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README.md +++ b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README.md @@ -114,8 +114,7 @@ class Solution { int mask = 0; for (int x : nums) { mask ^= x; - ans += cnt.getOrDefault(mask, 0); - cnt.merge(mask, 1, Integer::sum); + ans += cnt.merge(mask, 1, Integer::sum) - 1; } return ans; } @@ -133,8 +132,7 @@ public: int mask = 0; for (int x : nums) { mask ^= x; - ans += cnt[mask]; - ++cnt[mask]; + ans += cnt[mask]++; } return ans; } @@ -173,6 +171,27 @@ function beautifulSubarrays(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn beautiful_subarrays(nums: Vec) -> i64 { + let mut cnt = HashMap::new(); + cnt.insert(0, 1); + let mut ans = 0; + let mut mask = 0; + for &x in nums.iter() { + mask ^= x; + ans += *cnt.get(&mask).unwrap_or(&0); + *cnt.entry(mask).or_insert(0) += 1; + } + ans + } +} +``` + diff --git a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README_EN.md b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README_EN.md index cd4420ece7bf7..1be32239f605f 100644 --- a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README_EN.md +++ b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/README_EN.md @@ -112,8 +112,7 @@ class Solution { int mask = 0; for (int x : nums) { mask ^= x; - ans += cnt.getOrDefault(mask, 0); - cnt.merge(mask, 1, Integer::sum); + ans += cnt.merge(mask, 1, Integer::sum) - 1; } return ans; } @@ -131,8 +130,7 @@ public: int mask = 0; for (int x : nums) { mask ^= x; - ans += cnt[mask]; - ++cnt[mask]; + ans += cnt[mask]++; } return ans; } @@ -171,6 +169,27 @@ function beautifulSubarrays(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn beautiful_subarrays(nums: Vec) -> i64 { + let mut cnt = HashMap::new(); + cnt.insert(0, 1); + let mut ans = 0; + let mut mask = 0; + for &x in nums.iter() { + mask ^= x; + ans += *cnt.get(&mask).unwrap_or(&0); + *cnt.entry(mask).or_insert(0) += 1; + } + ans + } +} +``` + diff --git a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.cpp b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.cpp index b64bb52a8e161..52e66710cfa14 100644 --- a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.cpp +++ b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.cpp @@ -6,9 +6,8 @@ class Solution { int mask = 0; for (int x : nums) { mask ^= x; - ans += cnt[mask]; - ++cnt[mask]; + ans += cnt[mask]++; } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.java b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.java index b44bb1d5dd339..65b03036ad692 100644 --- a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.java +++ b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.java @@ -6,9 +6,8 @@ public long beautifulSubarrays(int[] nums) { int mask = 0; for (int x : nums) { mask ^= x; - ans += cnt.getOrDefault(mask, 0); - cnt.merge(mask, 1, Integer::sum); + ans += cnt.merge(mask, 1, Integer::sum) - 1; } return ans; } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.rs b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.rs new file mode 100644 index 0000000000000..085b133b1606c --- /dev/null +++ b/solution/2500-2599/2588.Count the Number of Beautiful Subarrays/Solution.rs @@ -0,0 +1,16 @@ +use std::collections::HashMap; + +impl Solution { + pub fn beautiful_subarrays(nums: Vec) -> i64 { + let mut cnt = HashMap::new(); + cnt.insert(0, 1); + let mut ans = 0; + let mut mask = 0; + for &x in nums.iter() { + mask ^= x; + ans += *cnt.get(&mask).unwrap_or(&0); + *cnt.entry(mask).or_insert(0) += 1; + } + ans + } +}