Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

Commit

Permalink
opt: findClosestNumber
Browse files Browse the repository at this point in the history
Signed-off-by: bggRGjQaUbCoE <[email protected]>
  • Loading branch information
bggRGjQaUbCoE committed Jan 8, 2025
1 parent 5982fd3 commit 56350b1
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -794,22 +794,9 @@ class Utils {
}

static int findClosestNumber(int target, List<int> numbers) {
return switch (numbers.length) {
0 => target,
1 => numbers.first,
_ => numbers.reduce((a, b) {
if (b > target) {
return a;
}
int diff1 = (a - target).abs();
int diff2 = (b - target).abs();
return diff1 == diff2
? max(a, b)
: diff1 < diff2
? a
: b;
}),
};
return numbers
.where((number) => number <= target)
.reduce((a, b) => a > b ? a : b);
}

// 检查更新
Expand Down

0 comments on commit 56350b1

Please sign in to comment.