Skip to content

Commit 41271f6

Browse files
LetMeFly666Copilot
andauthored
update: 添加问题“3516.找到最近的人”的代码和题解 (#1111)
* 3025: toContinueOnAnotherComputer (#1110) (#1108) * 3025: CE.java (#1110) (#1108) - this->xxx * 3025: AC.java (#1110) (#1108) java - AC,15.00%,15.00% 写还比较快 * 3025: CE.rust (#1110) (#1108) Line 23: Char 21: error: cannot find function `check2` in this scope (solution.rs) | 23 | if !check2(&points, i, j) { | ^^^^^^ not found in this scope | help: consider using the associated function on `Self` | 23 | if !Self::check2(&points, i, j) { | ++++++ Line 31: Char 25: error: cannot find function `check3` in this scope (solution.rs) | 31 | if !check3(&points, i, j, k) { | ^^^^^^ not found in this scope | help: consider using the associated function on `Self` | 31 | if !Self::check3(&points, i, j, k) { | ++++++ For more information about this error, try `rustc --explain E0425`. error: could not compile `prog` (bin "prog") due to 2 previous errors * 3025: CE.rust (#1110) (#1108) Line 23: Char 21: error: cannot find function `check2` in this scope (solution.rs) | 8 | fn check2(&self, points: &Vec<Vec<i32>>, i: usize, j: usize) -> bool { | ------ a method by that name is available on `Self` here ... 23 | if !check2(&points, i, j) { | ^^^^^^ not found in this scope Line 31: Char 25: error: cannot find function `check3` in this scope (solution.rs) | 12 | fn check3(&self, points: &Vec<Vec<i32>>, i: usize, j: usize, k: usize) -> bool { | ------ a method by that name is available on `Self` here ... 31 | if !check3(&points, i, j, k) { | ^^^^^^ not found in this scope For more information about this error, try `rustc --explain E0425`. error: could not compile `prog` (bin "prog") due to 2 previous errors * 3025: AC.rust (#1110) (#1108) rust - AC,33.33%,0.00% * word: en afternoon (#1110) (#1108) * word: en afternoon (#1110) (#1108) * 3516: CE.cpp (#1110) * 3516: AC.cpp+py | CE.go (#1110) cpp - AC,100.00%,21.43% py - AC,100.00%,35.00% Line 10: Char 13: undefined: abs (solution.go) * 3516: AC.go+rust (#1110) go - AC,100.00%,25.00% rust - AC,100.00%,9.09% * update: 添加问题“3516.找到最近的人”的代码和题解 (#1111) Signed-off-by: LetMeFly666 <[email protected]> * typo: found by copilot Update Solutions/LeetCode 3516.找到最近的人.md Co-authored-by: Copilot <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 1d8056b commit 41271f6

15 files changed

+459
-21
lines changed

.commitmsg

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-02 20:09:04
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-02 20:14:52
6+
*/
7+
class Solution {
8+
private int[][] points;
9+
10+
private boolean check(int i, int j) {
11+
return points[i][0] <= points[j][0] && points[i][1] >= points[j][1];
12+
}
13+
14+
private boolean check(int i, int j, int k) {
15+
return !(points[i][0] <= points[k][0] && points[k][0] <= points[j][0] && points[i][1] >= points[k][1] && points[k][1] >= points[j][1]);
16+
}
17+
18+
public int numberOfPairs(int[][] points) {
19+
int ans = 0;
20+
this.points = points;
21+
for (int i = 0; i < points.length; i++) {
22+
for (int j = 0; j < points.length; j++) {
23+
if (i == j) {
24+
continue;
25+
}
26+
if (!check(i, j)) {
27+
continue;
28+
}
29+
boolean can = true;
30+
for (int k = 0; k < points.length; k++) {
31+
if (k == i || k == j) {
32+
continue;
33+
}
34+
if (!check(i, j, k)) {
35+
can = false;
36+
break;
37+
}
38+
}
39+
if (can) {
40+
ans ++;
41+
}
42+
}
43+
}
44+
return ans;
45+
}
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-02 20:09:04
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-02 20:26:39
6+
*/
7+
impl Solution {
8+
fn check2(points: &Vec<Vec<i32>>, i: usize, j: usize) -> bool {
9+
points[i][0] <= points[j][0] && points[i][1] >= points[j][1]
10+
}
11+
12+
fn check3(points: &Vec<Vec<i32>>, i: usize, j: usize, k: usize) -> bool {
13+
!(points[i][0] <= points[k][0] && points[k][0] <= points[j][0] && points[i][1] >= points[k][1] && points[k][1] >= points[j][1])
14+
}
15+
pub fn number_of_pairs(points: Vec<Vec<i32>>) -> i32 {
16+
let mut ans: i32 = 0;
17+
let n = points.len();
18+
for i in 0..n {
19+
for j in 0..n {
20+
if i == j {
21+
continue;
22+
}
23+
if !Self::check2(&points, i, j) {
24+
continue;
25+
}
26+
let mut can: bool = true;
27+
for k in 0..n {
28+
if k == i || k == j {
29+
continue;
30+
}
31+
if !Self::check3(&points, i, j, k) {
32+
can = false;
33+
break;
34+
}
35+
}
36+
if can {
37+
ans += 1;
38+
}
39+
}
40+
}
41+
ans
42+
}
43+
}

Codes/3516-find-closest-person.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-04 13:33:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-04 13:36:19
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
int findClosest(int x, int y, int z) {
14+
int diff = abs(x - z) - abs(y - z);
15+
return diff ? diff > 0 ? 2 : 1 : 0;
16+
}
17+
};

Codes/3516-find-closest-person.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-04 13:33:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-04 13:39:11
6+
*/
7+
package main
8+
9+
func abs3516(a int) int {
10+
if a < 0 {
11+
return -a
12+
}
13+
return a
14+
}
15+
16+
func findClosest(x int, y int, z int) int {
17+
diff := abs3516(x - z) - abs3516(y - z)
18+
if diff == 0 {
19+
return 0
20+
} else if diff > 0 {
21+
return 2
22+
}
23+
return 1
24+
}

Codes/3516-find-closest-person.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-09-04 13:33:33
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-09-04 13:37:22
6+
'''
7+
class Solution:
8+
def findClosest(self, x: int, y: int, z: int) -> int:
9+
diff = abs(x - z) - abs(y - z)
10+
return 2 if diff > 0 else 1 if diff else 0

Codes/3516-find-closest-person.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-09-04 13:33:33
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-09-04 13:40:19
6+
*/
7+
impl Solution {
8+
pub fn find_closest(x: i32, y: i32, z: i32) -> i32 {
9+
let diff: i32 = (x - z).abs() - (y - z).abs();
10+
if diff == 0 {
11+
return 0; // 这里必须带上return
12+
} else if diff > 0 {
13+
return 2;
14+
}
15+
1
16+
}
17+
}

Codes/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
* @LastEditTime: 2025-08-31 19:14:48
66
*/
77
pub struct Solution;
8-
include!("3000-maximum-area-of-longest-diagonal-rectangle.rs"); // 这个fileName是会被脚本替换掉的
8+
include!("3516-find-closest-person.rs"); // 这个fileName是会被脚本替换掉的

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,7 @@
10191019
|3439.重新安排会议得到最多空余时间I|中等|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/07/13/LeetCode%203439.%E9%87%8D%E6%96%B0%E5%AE%89%E6%8E%92%E4%BC%9A%E8%AE%AE%E5%BE%97%E5%88%B0%E6%9C%80%E5%A4%9A%E7%A9%BA%E4%BD%99%E6%97%B6%E9%97%B4I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/149317430" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reschedule-meetings-for-maximum-free-time-i/solutions/3722937/letmefly-3439zhong-xin-an-pai-hui-yi-de-r3t9e/" target="_blank">LeetCode题解</a>|
10201020
|3442.奇偶频次间的最大差值I|简单|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/06/10/LeetCode%203442.%E5%A5%87%E5%81%B6%E9%A2%91%E6%AC%A1%E9%97%B4%E7%9A%84%E6%9C%80%E5%A4%A7%E5%B7%AE%E5%80%BCI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148570777" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-difference-between-even-and-odd-frequency-i/solutions/3697605/letmefly-3442qi-ou-pin-ci-jian-de-zui-da-nwwx/" target="_blank">LeetCode题解</a>|
10211021
|3459.最长V形对角线段的长度|困难|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/08/31/LeetCode%203459.%E6%9C%80%E9%95%BFV%E5%BD%A2%E5%AF%B9%E8%A7%92%E7%BA%BF%E6%AE%B5%E7%9A%84%E9%95%BF%E5%BA%A6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151050078" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/length-of-longest-v-shaped-diagonal-segment/solutions/3768545/letmefly-3459zui-chang-v-xing-dui-jiao-x-gu8q/" target="_blank">LeetCode题解</a>|
1022+
|3516.找到最近的人|简单|<a href="https://leetcode.cn/problems/find-closest-person/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/04/LeetCode%203516.%E6%89%BE%E5%88%B0%E6%9C%80%E8%BF%91%E7%9A%84%E4%BA%BA/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151184074" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-closest-person/solutions/3772009/letmefly-3516zhao-dao-zui-jin-de-ren-ji-8rlbz/" target="_blank">LeetCode题解</a>|
10221023
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
10231024
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
10241025
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|

Solutions/LeetCode 3025.人员站位的方案数I.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,105 @@ func numberOfPairs(points [][]int) (ans int) {
323323
}
324324
```
325325

326+
#### Java
327+
328+
```java
329+
/*
330+
* @Author: LetMeFly
331+
* @Date: 2025-09-02 20:09:04
332+
* @LastEditors: LetMeFly.xyz
333+
* @LastEditTime: 2025-09-02 20:14:52
334+
*/
335+
class Solution {
336+
private int[][] points;
337+
338+
private boolean check(int i, int j) {
339+
return points[i][0] <= points[j][0] && points[i][1] >= points[j][1];
340+
}
341+
342+
private boolean check(int i, int j, int k) {
343+
return !(points[i][0] <= points[k][0] && points[k][0] <= points[j][0] && points[i][1] >= points[k][1] && points[k][1] >= points[j][1]);
344+
}
345+
346+
public int numberOfPairs(int[][] points) {
347+
int ans = 0;
348+
this.points = points;
349+
for (int i = 0; i < points.length; i++) {
350+
for (int j = 0; j < points.length; j++) {
351+
if (i == j) {
352+
continue;
353+
}
354+
if (!check(i, j)) {
355+
continue;
356+
}
357+
boolean can = true;
358+
for (int k = 0; k < points.length; k++) {
359+
if (k == i || k == j) {
360+
continue;
361+
}
362+
if (!check(i, j, k)) {
363+
can = false;
364+
break;
365+
}
366+
}
367+
if (can) {
368+
ans ++;
369+
}
370+
}
371+
}
372+
return ans;
373+
}
374+
}
375+
```
376+
377+
#### Rust
378+
379+
```rust
380+
/*
381+
* @Author: LetMeFly
382+
* @Date: 2025-09-02 20:09:04
383+
* @LastEditors: LetMeFly.xyz
384+
* @LastEditTime: 2025-09-02 20:26:39
385+
*/
386+
impl Solution {
387+
fn check2(points: &Vec<Vec<i32>>, i: usize, j: usize) -> bool {
388+
points[i][0] <= points[j][0] && points[i][1] >= points[j][1]
389+
}
390+
391+
fn check3(points: &Vec<Vec<i32>>, i: usize, j: usize, k: usize) -> bool {
392+
!(points[i][0] <= points[k][0] && points[k][0] <= points[j][0] && points[i][1] >= points[k][1] && points[k][1] >= points[j][1])
393+
}
394+
pub fn number_of_pairs(points: Vec<Vec<i32>>) -> i32 {
395+
let mut ans: i32 = 0;
396+
let n = points.len();
397+
for i in 0..n {
398+
for j in 0..n {
399+
if i == j {
400+
continue;
401+
}
402+
if !Self::check2(&points, i, j) {
403+
continue;
404+
}
405+
let mut can: bool = true;
406+
for k in 0..n {
407+
if k == i || k == j {
408+
continue;
409+
}
410+
if !Self::check3(&points, i, j, k) {
411+
can = false;
412+
break;
413+
}
414+
}
415+
if can {
416+
ans += 1;
417+
}
418+
}
419+
}
420+
ans
421+
}
422+
}
423+
```
424+
326425
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/151119511)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/09/02/LeetCode%203025.%E4%BA%BA%E5%91%98%E7%AB%99%E4%BD%8D%E7%9A%84%E6%96%B9%E6%A1%88%E6%95%B0I/)~
327426
>
328427
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

0 commit comments

Comments
 (0)