Skip to content

Commit d2b7dbd

Browse files
authored
feat: add solutions to lc problems: No.3370,3371 (#4804)
1 parent f37ecf3 commit d2b7dbd

File tree

8 files changed

+250
-0
lines changed

8 files changed

+250
-0
lines changed

solution/3300-3399/3370.Smallest Number With All Set Bits/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ function smallestNumber(n: number): number {
149149
}
150150
```
151151

152+
#### Rust
153+
154+
```rust
155+
impl Solution {
156+
pub fn smallest_number(n: i32) -> i32 {
157+
let mut x = 1;
158+
while x - 1 < n {
159+
x <<= 1;
160+
}
161+
x - 1
162+
}
163+
}
164+
```
165+
166+
#### C#
167+
168+
```cs
169+
public class Solution {
170+
public int SmallestNumber(int n) {
171+
int x = 1;
172+
while (x - 1 < n) {
173+
x <<= 1;
174+
}
175+
return x - 1;
176+
}
177+
}
178+
```
179+
152180
<!-- tabs:end -->
153181

154182
<!-- solution:end -->

solution/3300-3399/3370.Smallest Number With All Set Bits/README_EN.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ function smallestNumber(n: number): number {
145145
}
146146
```
147147

148+
#### Rust
149+
150+
```rust
151+
impl Solution {
152+
pub fn smallest_number(n: i32) -> i32 {
153+
let mut x = 1;
154+
while x - 1 < n {
155+
x <<= 1;
156+
}
157+
x - 1
158+
}
159+
}
160+
```
161+
162+
#### C#
163+
164+
```cs
165+
public class Solution {
166+
public int SmallestNumber(int n) {
167+
int x = 1;
168+
while (x - 1 < n) {
169+
x <<= 1;
170+
}
171+
return x - 1;
172+
}
173+
}
174+
```
175+
148176
<!-- tabs:end -->
149177

150178
<!-- solution:end -->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
public class Solution {
2+
public int SmallestNumber(int n) {
3+
int x = 1;
4+
while (x - 1 < n) {
5+
x <<= 1;
6+
}
7+
return x - 1;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn smallest_number(n: i32) -> i32 {
3+
let mut x = 1;
4+
while x - 1 < n {
5+
x <<= 1;
6+
}
7+
x - 1
8+
}
9+
}

solution/3300-3399/3371.Identify the Largest Outlier in an Array/README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,68 @@ function getLargestOutlier(nums: number[]): number {
214214
}
215215
```
216216

217+
#### Rust
218+
219+
```rust
220+
use std::collections::HashMap;
221+
222+
impl Solution {
223+
pub fn get_largest_outlier(nums: Vec<i32>) -> i32 {
224+
let mut s = 0;
225+
let mut cnt = HashMap::new();
226+
for &x in &nums {
227+
s += x;
228+
*cnt.entry(x).or_insert(0) += 1;
229+
}
230+
231+
let mut ans = i32::MIN;
232+
for (&x, &v) in &cnt {
233+
let t = s - x;
234+
if t % 2 != 0 {
235+
continue;
236+
}
237+
let y = t / 2;
238+
if let Some(&count_y) = cnt.get(&y) {
239+
if x != y || v > 1 {
240+
ans = ans.max(x);
241+
}
242+
}
243+
}
244+
ans
245+
}
246+
}
247+
```
248+
249+
#### C#
250+
251+
```cs
252+
public class Solution {
253+
public int GetLargestOutlier(int[] nums) {
254+
int s = 0;
255+
var cnt = new Dictionary<int, int>();
256+
foreach (int x in nums) {
257+
s += x;
258+
if (!cnt.ContainsKey(x)) cnt[x] = 0;
259+
cnt[x]++;
260+
}
261+
262+
int ans = int.MinValue;
263+
foreach (var kv in cnt) {
264+
int x = kv.Key, v = kv.Value;
265+
int t = s - x;
266+
if (t % 2 != 0) continue;
267+
int y = t / 2;
268+
if (cnt.ContainsKey(y)) {
269+
if (x != y || v > 1) {
270+
ans = Math.Max(ans, x);
271+
}
272+
}
273+
}
274+
return ans;
275+
}
276+
}
277+
```
278+
217279
<!-- tabs:end -->
218280

219281
<!-- solution:end -->

solution/3300-3399/3371.Identify the Largest Outlier in an Array/README_EN.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,68 @@ function getLargestOutlier(nums: number[]): number {
212212
}
213213
```
214214

215+
#### Rust
216+
217+
```rust
218+
use std::collections::HashMap;
219+
220+
impl Solution {
221+
pub fn get_largest_outlier(nums: Vec<i32>) -> i32 {
222+
let mut s = 0;
223+
let mut cnt = HashMap::new();
224+
for &x in &nums {
225+
s += x;
226+
*cnt.entry(x).or_insert(0) += 1;
227+
}
228+
229+
let mut ans = i32::MIN;
230+
for (&x, &v) in &cnt {
231+
let t = s - x;
232+
if t % 2 != 0 {
233+
continue;
234+
}
235+
let y = t / 2;
236+
if let Some(&count_y) = cnt.get(&y) {
237+
if x != y || v > 1 {
238+
ans = ans.max(x);
239+
}
240+
}
241+
}
242+
ans
243+
}
244+
}
245+
```
246+
247+
#### C#
248+
249+
```cs
250+
public class Solution {
251+
public int GetLargestOutlier(int[] nums) {
252+
int s = 0;
253+
var cnt = new Dictionary<int, int>();
254+
foreach (int x in nums) {
255+
s += x;
256+
if (!cnt.ContainsKey(x)) cnt[x] = 0;
257+
cnt[x]++;
258+
}
259+
260+
int ans = int.MinValue;
261+
foreach (var kv in cnt) {
262+
int x = kv.Key, v = kv.Value;
263+
int t = s - x;
264+
if (t % 2 != 0) continue;
265+
int y = t / 2;
266+
if (cnt.ContainsKey(y)) {
267+
if (x != y || v > 1) {
268+
ans = Math.Max(ans, x);
269+
}
270+
}
271+
}
272+
return ans;
273+
}
274+
}
275+
```
276+
215277
<!-- tabs:end -->
216278

217279
<!-- solution:end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class Solution {
2+
public int GetLargestOutlier(int[] nums) {
3+
int s = 0;
4+
var cnt = new Dictionary<int, int>();
5+
foreach (int x in nums) {
6+
s += x;
7+
if (!cnt.ContainsKey(x)) cnt[x] = 0;
8+
cnt[x]++;
9+
}
10+
11+
int ans = int.MinValue;
12+
foreach (var kv in cnt) {
13+
int x = kv.Key, v = kv.Value;
14+
int t = s - x;
15+
if (t % 2 != 0) continue;
16+
int y = t / 2;
17+
if (cnt.ContainsKey(y)) {
18+
if (x != y || v > 1) {
19+
ans = Math.Max(ans, x);
20+
}
21+
}
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn get_largest_outlier(nums: Vec<i32>) -> i32 {
5+
let mut s = 0;
6+
let mut cnt = HashMap::new();
7+
for &x in &nums {
8+
s += x;
9+
*cnt.entry(x).or_insert(0) += 1;
10+
}
11+
12+
let mut ans = i32::MIN;
13+
for (&x, &v) in &cnt {
14+
let t = s - x;
15+
if t % 2 != 0 {
16+
continue;
17+
}
18+
let y = t / 2;
19+
if let Some(&count_y) = cnt.get(&y) {
20+
if x != y || v > 1 {
21+
ans = ans.max(x);
22+
}
23+
}
24+
}
25+
ans
26+
}
27+
}

0 commit comments

Comments
 (0)