int low = 0;
int high = list.size() - 1;
int mid = 0;
int matchMid = -1;
FightRequestReq matchFightRequestReq = null;
while (low <= high) {
mid = (low + high) >>> 1;
int midVal = list.get(mid).getCompetitionVo().getFightValue();
if (midVal >= req.getCompetitionVo().getPrefixValue() && midVal <= req.getCompetitionVo().getSuffixValue()) {
// 匹配成功
if (matchFightRequestReq == null) {
matchFightRequestReq = list.get(mid);
matchMid = mid;
} else {
if (Math.abs(midVal - req.getCompetitionVo().getFightValue()) < Math.abs(matchFightRequestReq.getCompetitionVo().getFightValue() - req.getCompetitionVo().getFightValue())) {
// 匹配到更接近的值
matchFightRequestReq = list.get(mid);
matchMid = mid;
} else {
// 非最优值,继续匹配
}
}
}
if (midVal < req.getCompetitionVo().getFightValue()) {
low = mid + 1;
if (low > high) {
if (midVal < req.getCompetitionVo().getPrefixValue() || midVal > req.getCompetitionVo().getSuffixValue()) {
if (matchFightRequestReq != null) {
return list.remove(matchMid);
} else {
return null;
}
} else {
if (matchFightRequestReq != null) {
if (Math.abs(midVal - req.getCompetitionVo().getFightValue()) < Math.abs(matchFightRequestReq.getCompetitionVo().getFightValue() - req.getCompetitionVo().getFightValue())) {
// 匹配到更接近的值
return list.remove(mid);
} else {
return list.remove(matchMid);
}
} else {
return list.remove(mid);
}
}
}
} else if (midVal > req.getCompetitionVo().getFightValue()) {
high = mid - 1;
if (low > high) {
if (midVal < req.getCompetitionVo().getPrefixValue() || midVal > req.getCompetitionVo().getSuffixValue()) {
if (matchFightRequestReq != null) {
return list.remove(matchMid);
} else {
return null;
}
} else {
if (matchFightRequestReq != null) {
if (Math.abs(midVal - req.getCompetitionVo().getFightValue()) < Math.abs(matchFightRequestReq.getCompetitionVo().getFightValue() - req.getCompetitionVo().getFightValue())) {
// 匹配到更接近的值
return list.remove(mid);
} else {
return list.remove(matchMid);
}
} else {
return list.remove(mid);
}
}
}
} else {
return list.remove(mid);
}
}
if (matchFightRequestReq != null) {
return list.remove(matchMid);
}
return null;
int high = list.size() - 1;
int mid = 0;
int matchMid = -1;
FightRequestReq matchFightRequestReq = null;
while (low <= high) {
mid = (low + high) >>> 1;
int midVal = list.get(mid).getCompetitionVo().getFightValue();
if (midVal >= req.getCompetitionVo().getPrefixValue() && midVal <= req.getCompetitionVo().getSuffixValue()) {
// 匹配成功
if (matchFightRequestReq == null) {
matchFightRequestReq = list.get(mid);
matchMid = mid;
} else {
if (Math.abs(midVal - req.getCompetitionVo().getFightValue()) < Math.abs(matchFightRequestReq.getCompetitionVo().getFightValue() - req.getCompetitionVo().getFightValue())) {
// 匹配到更接近的值
matchFightRequestReq = list.get(mid);
matchMid = mid;
} else {
// 非最优值,继续匹配
}
}
}
if (midVal < req.getCompetitionVo().getFightValue()) {
low = mid + 1;
if (low > high) {
if (midVal < req.getCompetitionVo().getPrefixValue() || midVal > req.getCompetitionVo().getSuffixValue()) {
if (matchFightRequestReq != null) {
return list.remove(matchMid);
} else {
return null;
}
} else {
if (matchFightRequestReq != null) {
if (Math.abs(midVal - req.getCompetitionVo().getFightValue()) < Math.abs(matchFightRequestReq.getCompetitionVo().getFightValue() - req.getCompetitionVo().getFightValue())) {
// 匹配到更接近的值
return list.remove(mid);
} else {
return list.remove(matchMid);
}
} else {
return list.remove(mid);
}
}
}
} else if (midVal > req.getCompetitionVo().getFightValue()) {
high = mid - 1;
if (low > high) {
if (midVal < req.getCompetitionVo().getPrefixValue() || midVal > req.getCompetitionVo().getSuffixValue()) {
if (matchFightRequestReq != null) {
return list.remove(matchMid);
} else {
return null;
}
} else {
if (matchFightRequestReq != null) {
if (Math.abs(midVal - req.getCompetitionVo().getFightValue()) < Math.abs(matchFightRequestReq.getCompetitionVo().getFightValue() - req.getCompetitionVo().getFightValue())) {
// 匹配到更接近的值
return list.remove(mid);
} else {
return list.remove(matchMid);
}
} else {
return list.remove(mid);
}
}
}
} else {
return list.remove(mid);
}
}
if (matchFightRequestReq != null) {
return list.remove(matchMid);
}
return null;
本文介绍了一种精确匹配算法的实现过程,该算法通过遍历列表并比较元素值来找到最佳匹配项,同时考虑到值的范围限制,并返回最接近目标值的元素。
595

被折叠的 条评论
为什么被折叠?



