SRC 二分插入代码

来源:互联网 发布:java报500错误怎么解决 编辑:程序博客网 时间:2024/06/05 21:05
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;
0 0
原创粉丝点击