From 3deaf8ee9250ce7261a7051007b8aa51ef2690fa Mon Sep 17 00:00:00 2001 From: Libin YANG Date: 2025年10月29日 06:56:25 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3370,3371 --- .../README.md | 28 +++++++++ .../README_EN.md | 28 +++++++++ .../Solution.cs | 9 +++ .../Solution.rs | 9 +++ .../README.md | 62 +++++++++++++++++++ .../README_EN.md | 62 +++++++++++++++++++ .../Solution.cs | 25 ++++++++ .../Solution.rs | 27 ++++++++ 8 files changed, 250 insertions(+) create mode 100644 solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.cs create mode 100644 solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.rs create mode 100644 solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.cs create mode 100644 solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.rs diff --git a/solution/3300-3399/3370.Smallest Number With All Set Bits/README.md b/solution/3300-3399/3370.Smallest Number With All Set Bits/README.md index 02c0e8e6ee1fd..7291f3296819e 100644 --- a/solution/3300-3399/3370.Smallest Number With All Set Bits/README.md +++ b/solution/3300-3399/3370.Smallest Number With All Set Bits/README.md @@ -149,6 +149,34 @@ function smallestNumber(n: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn smallest_number(n: i32) -> i32 { + let mut x = 1; + while x - 1 < n { + x <<= 1; + } + x - 1 + } +} +``` + +#### C# + +```cs +public class Solution { + public int SmallestNumber(int n) { + int x = 1; + while (x - 1 < n) { + x <<= 1; + } + return x - 1; + } +} +``` + diff --git a/solution/3300-3399/3370.Smallest Number With All Set Bits/README_EN.md b/solution/3300-3399/3370.Smallest Number With All Set Bits/README_EN.md index 8fa50636abd67..722d8abde88b4 100644 --- a/solution/3300-3399/3370.Smallest Number With All Set Bits/README_EN.md +++ b/solution/3300-3399/3370.Smallest Number With All Set Bits/README_EN.md @@ -145,6 +145,34 @@ function smallestNumber(n: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn smallest_number(n: i32) -> i32 { + let mut x = 1; + while x - 1 < n { + x <<= 1; + } + x - 1 + } +} +``` + +#### C# + +```cs +public class Solution { + public int SmallestNumber(int n) { + int x = 1; + while (x - 1 < n) { + x <<= 1; + } + return x - 1; + } +} +``` + diff --git a/solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.cs b/solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.cs new file mode 100644 index 0000000000000..8f26a23219a95 --- /dev/null +++ b/solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.cs @@ -0,0 +1,9 @@ +public class Solution { + public int SmallestNumber(int n) { + int x = 1; + while (x - 1 < n) { + x <<= 1; + } + return x - 1; + } +} diff --git a/solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.rs b/solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.rs new file mode 100644 index 0000000000000..8ddc22703111a --- /dev/null +++ b/solution/3300-3399/3370.Smallest Number With All Set Bits/Solution.rs @@ -0,0 +1,9 @@ +impl Solution { + pub fn smallest_number(n: i32) -> i32 { + let mut x = 1; + while x - 1 < n { + x <<= 1; + } + x - 1 + } +} diff --git a/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README.md b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README.md index e774005c2a948..4175e95674ec5 100644 --- a/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README.md +++ b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README.md @@ -214,6 +214,68 @@ function getLargestOutlier(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn get_largest_outlier(nums: Vec) -> i32 { + let mut s = 0; + let mut cnt = HashMap::new(); + for &x in &nums { + s += x; + *cnt.entry(x).or_insert(0) += 1; + } + + let mut ans = i32::MIN; + for (&x, &v) in &cnt { + let t = s - x; + if t % 2 != 0 { + continue; + } + let y = t / 2; + if let Some(&count_y) = cnt.get(&y) { + if x != y || v> 1 { + ans = ans.max(x); + } + } + } + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int GetLargestOutlier(int[] nums) { + int s = 0; + var cnt = new Dictionary(); + foreach (int x in nums) { + s += x; + if (!cnt.ContainsKey(x)) cnt[x] = 0; + cnt[x]++; + } + + int ans = int.MinValue; + foreach (var kv in cnt) { + int x = kv.Key, v = kv.Value; + int t = s - x; + if (t % 2 != 0) continue; + int y = t / 2; + if (cnt.ContainsKey(y)) { + if (x != y || v> 1) { + ans = Math.Max(ans, x); + } + } + } + return ans; + } +} +``` + diff --git a/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README_EN.md b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README_EN.md index 8c2e198301be9..dca811af763cc 100644 --- a/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README_EN.md +++ b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/README_EN.md @@ -212,6 +212,68 @@ function getLargestOutlier(nums: number[]): number { } ``` +#### Rust + +```rust +use std::collections::HashMap; + +impl Solution { + pub fn get_largest_outlier(nums: Vec) -> i32 { + let mut s = 0; + let mut cnt = HashMap::new(); + for &x in &nums { + s += x; + *cnt.entry(x).or_insert(0) += 1; + } + + let mut ans = i32::MIN; + for (&x, &v) in &cnt { + let t = s - x; + if t % 2 != 0 { + continue; + } + let y = t / 2; + if let Some(&count_y) = cnt.get(&y) { + if x != y || v> 1 { + ans = ans.max(x); + } + } + } + ans + } +} +``` + +#### C# + +```cs +public class Solution { + public int GetLargestOutlier(int[] nums) { + int s = 0; + var cnt = new Dictionary(); + foreach (int x in nums) { + s += x; + if (!cnt.ContainsKey(x)) cnt[x] = 0; + cnt[x]++; + } + + int ans = int.MinValue; + foreach (var kv in cnt) { + int x = kv.Key, v = kv.Value; + int t = s - x; + if (t % 2 != 0) continue; + int y = t / 2; + if (cnt.ContainsKey(y)) { + if (x != y || v> 1) { + ans = Math.Max(ans, x); + } + } + } + return ans; + } +} +``` + diff --git a/solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.cs b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.cs new file mode 100644 index 0000000000000..5a4d53a29f760 --- /dev/null +++ b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.cs @@ -0,0 +1,25 @@ +public class Solution { + public int GetLargestOutlier(int[] nums) { + int s = 0; + var cnt = new Dictionary(); + foreach (int x in nums) { + s += x; + if (!cnt.ContainsKey(x)) cnt[x] = 0; + cnt[x]++; + } + + int ans = int.MinValue; + foreach (var kv in cnt) { + int x = kv.Key, v = kv.Value; + int t = s - x; + if (t % 2 != 0) continue; + int y = t / 2; + if (cnt.ContainsKey(y)) { + if (x != y || v> 1) { + ans = Math.Max(ans, x); + } + } + } + return ans; + } +} diff --git a/solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.rs b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.rs new file mode 100644 index 0000000000000..a6c14df425c14 --- /dev/null +++ b/solution/3300-3399/3371.Identify the Largest Outlier in an Array/Solution.rs @@ -0,0 +1,27 @@ +use std::collections::HashMap; + +impl Solution { + pub fn get_largest_outlier(nums: Vec) -> i32 { + let mut s = 0; + let mut cnt = HashMap::new(); + for &x in &nums { + s += x; + *cnt.entry(x).or_insert(0) += 1; + } + + let mut ans = i32::MIN; + for (&x, &v) in &cnt { + let t = s - x; + if t % 2 != 0 { + continue; + } + let y = t / 2; + if let Some(&count_y) = cnt.get(&y) { + if x != y || v> 1 { + ans = ans.max(x); + } + } + } + ans + } +}

AltStyle によって変換されたページ (->オリジナル) /