Skip to content

Commit e8d6a74

Browse files
authored
feat: add solutions to lc problems: No.3021,3516 (#4671)
1 parent 0f30fe5 commit e8d6a74

File tree

11 files changed

+259
-0
lines changed

11 files changed

+259
-0
lines changed

solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,35 @@ function flowerGame(n: number, m: number): number {
151151
}
152152
```
153153

154+
#### Rust
155+
156+
```rust
157+
impl Solution {
158+
pub fn flower_game(n: i32, m: i32) -> i64 {
159+
let a1 = ((n + 1) / 2) as i64;
160+
let b1 = ((m + 1) / 2) as i64;
161+
let a2 = (n / 2) as i64;
162+
let b2 = (m / 2) as i64;
163+
a1 * b2 + a2 * b1
164+
}
165+
}
166+
```
167+
168+
#### JavaScript
169+
170+
```js
171+
/**
172+
* @param {number} n
173+
* @param {number} m
174+
* @return {number}
175+
*/
176+
var flowerGame = function (n, m) {
177+
const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1];
178+
const [a2, b2] = [n >> 1, m >> 1];
179+
return a1 * b2 + a2 * b1;
180+
};
181+
```
182+
154183
<!-- tabs:end -->
155184

156185
<!-- solution:end -->
@@ -220,6 +249,29 @@ function flowerGame(n: number, m: number): number {
220249
}
221250
```
222251

252+
#### Rust
253+
254+
```rust
255+
impl Solution {
256+
pub fn flower_game(n: i32, m: i32) -> i64 {
257+
(n as i64 * m as i64) / 2
258+
}
259+
}
260+
```
261+
262+
#### JavaScript
263+
264+
```js
265+
/**
266+
* @param {number} n
267+
* @param {number} m
268+
* @return {number}
269+
*/
270+
var flowerGame = function (n, m) {
271+
return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n);
272+
};
273+
```
274+
223275
<!-- tabs:end -->
224276

225277
<!-- solution:end -->

solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md

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

152+
#### Rust
153+
154+
```rust
155+
impl Solution {
156+
pub fn flower_game(n: i32, m: i32) -> i64 {
157+
let a1 = ((n + 1) / 2) as i64;
158+
let b1 = ((m + 1) / 2) as i64;
159+
let a2 = (n / 2) as i64;
160+
let b2 = (m / 2) as i64;
161+
a1 * b2 + a2 * b1
162+
}
163+
}
164+
```
165+
166+
#### JavaScript
167+
168+
```js
169+
/**
170+
* @param {number} n
171+
* @param {number} m
172+
* @return {number}
173+
*/
174+
var flowerGame = function (n, m) {
175+
const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1];
176+
const [a2, b2] = [n >> 1, m >> 1];
177+
return a1 * b2 + a2 * b1;
178+
};
179+
```
180+
152181
<!-- tabs:end -->
153182

154183
<!-- solution:end -->
@@ -218,6 +247,29 @@ function flowerGame(n: number, m: number): number {
218247
}
219248
```
220249

250+
#### Rust
251+
252+
```rust
253+
impl Solution {
254+
pub fn flower_game(n: i32, m: i32) -> i64 {
255+
(n as i64 * m as i64) / 2
256+
}
257+
}
258+
```
259+
260+
#### JavaScript
261+
262+
```js
263+
/**
264+
* @param {number} n
265+
* @param {number} m
266+
* @return {number}
267+
*/
268+
var flowerGame = function (n, m) {
269+
return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n);
270+
};
271+
```
272+
221273
<!-- tabs:end -->
222274

223275
<!-- solution:end -->
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} m
4+
* @return {number}
5+
*/
6+
var flowerGame = function (n, m) {
7+
const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1];
8+
const [a2, b2] = [n >> 1, m >> 1];
9+
return a1 * b2 + a2 * b1;
10+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn flower_game(n: i32, m: i32) -> i64 {
3+
let a1 = ((n + 1) / 2) as i64;
4+
let b1 = ((m + 1) / 2) as i64;
5+
let a2 = (n / 2) as i64;
6+
let b2 = (m / 2) as i64;
7+
a1 * b2 + a2 * b1
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} m
4+
* @return {number}
5+
*/
6+
var flowerGame = function (n, m) {
7+
return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n);
8+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn flower_game(n: i32, m: i32) -> i64 {
3+
(n as i64 * m as i64) / 2
4+
}
5+
}

solution/3500-3599/3516.Find Closest Person/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,52 @@ function findClosest(x: number, y: number, z: number): number {
184184
}
185185
```
186186

187+
#### Rust
188+
189+
```rust
190+
impl Solution {
191+
pub fn find_closest(x: i32, y: i32, z: i32) -> i32 {
192+
let a = (x - z).abs();
193+
let b = (y - z).abs();
194+
if a == b {
195+
0
196+
} else if a < b {
197+
1
198+
} else {
199+
2
200+
}
201+
}
202+
}
203+
```
204+
205+
#### JavaScript
206+
207+
```js
208+
/**
209+
* @param {number} x
210+
* @param {number} y
211+
* @param {number} z
212+
* @return {number}
213+
*/
214+
var findClosest = function (x, y, z) {
215+
const a = Math.abs(x - z);
216+
const b = Math.abs(y - z);
217+
return a === b ? 0 : a < b ? 1 : 2;
218+
};
219+
```
220+
221+
#### C#
222+
223+
```cs
224+
public class Solution {
225+
public int FindClosest(int x, int y, int z) {
226+
int a = Math.Abs(x - z);
227+
int b = Math.Abs(y - z);
228+
return a == b ? 0 : (a < b ? 1 : 2);
229+
}
230+
}
231+
```
232+
187233
<!-- tabs:end -->
188234

189235
<!-- solution:end -->

solution/3500-3599/3516.Find Closest Person/README_EN.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,52 @@ function findClosest(x: number, y: number, z: number): number {
182182
}
183183
```
184184

185+
#### Rust
186+
187+
```rust
188+
impl Solution {
189+
pub fn find_closest(x: i32, y: i32, z: i32) -> i32 {
190+
let a = (x - z).abs();
191+
let b = (y - z).abs();
192+
if a == b {
193+
0
194+
} else if a < b {
195+
1
196+
} else {
197+
2
198+
}
199+
}
200+
}
201+
```
202+
203+
#### JavaScript
204+
205+
```js
206+
/**
207+
* @param {number} x
208+
* @param {number} y
209+
* @param {number} z
210+
* @return {number}
211+
*/
212+
var findClosest = function (x, y, z) {
213+
const a = Math.abs(x - z);
214+
const b = Math.abs(y - z);
215+
return a === b ? 0 : a < b ? 1 : 2;
216+
};
217+
```
218+
219+
#### C#
220+
221+
```cs
222+
public class Solution {
223+
public int FindClosest(int x, int y, int z) {
224+
int a = Math.Abs(x - z);
225+
int b = Math.Abs(y - z);
226+
return a == b ? 0 : (a < b ? 1 : 2);
227+
}
228+
}
229+
```
230+
185231
<!-- tabs:end -->
186232

187233
<!-- solution:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Solution {
2+
public int FindClosest(int x, int y, int z) {
3+
int a = Math.Abs(x - z);
4+
int b = Math.Abs(y - z);
5+
return a == b ? 0 : (a < b ? 1 : 2);
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number} x
3+
* @param {number} y
4+
* @param {number} z
5+
* @return {number}
6+
*/
7+
var findClosest = function (x, y, z) {
8+
const a = Math.abs(x - z);
9+
const b = Math.abs(y - z);
10+
return a === b ? 0 : a < b ? 1 : 2;
11+
};

0 commit comments

Comments
 (0)