From c209807121e1ce2877396e061d32cab27b261b52 Mon Sep 17 00:00:00 2001 From: algorithmzuo Date: Tue, 9 May 2023 21:21:46 +0800 Subject: [PATCH] modify code --- 大厂刷题班/class07/Code03_MaxGap.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/大厂刷题班/class07/Code03_MaxGap.java b/大厂刷题班/class07/Code03_MaxGap.java index 486c6d3..4bb3566 100644 --- a/大厂刷题班/class07/Code03_MaxGap.java +++ b/大厂刷题班/class07/Code03_MaxGap.java @@ -39,8 +39,13 @@ public class Code03_MaxGap { return res; } - public static int bucket(long num, long len, long min, long max) { - return (int) ((num - min) * len / (max - min)); + public static int bucket(int num, int len, int min, int max) { + // 一个桶的范围 + double range = (double) (max - min) / (double) len; + // num和min之间的距离 + double distance = (double) (num - min); + // 返回桶的编号,向下取整 + return (int) (distance / range); } }