久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

二分查找中的第一次出現(xiàn)

First occurrence in a binary search(二分查找中的第一次出現(xiàn))
本文介紹了二分查找中的第一次出現(xiàn)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在修改一些代碼,但我意識(shí)到了一些我從來(lái)不知道的事情.正常的二分搜索將在數(shù)據(jù)集中為多次出現(xiàn)的鍵返回隨機(jī)索引.如何修改下面的代碼以返回第一次出現(xiàn)?這是人們做的事情嗎?

I'm tinkering with some code and I realized something I never knew. A normal binary search will return a random index in a data set for a key that occurs more than once. How can I modify this code below to return the first occurrence? Is this something people do?

//ripped from the JDK
public static int binarySearchValue(InvertedContainer.InvertedIndex[] a, long key) {
    return bSearchVal(a, 0, a.length, key);
}

private static int bSearchVal(InvertedContainer.InvertedIndex[] a, int fromIndex,
                                 int toIndex, long key) {
    int low = fromIndex;
    int high = toIndex - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        long midVal = a[mid].val;

        if (midVal < key)
            low = mid + 1;
        else if (midVal > key)
            high = mid - 1;
        else
            return mid; // key found
    }
    return (low); // key not found. return insertion point
}

推薦答案

找到一個(gè)匹配的值,你基本上需要遍歷集合,直到找到一個(gè)沒(méi)有的條目 匹配.

Having found a matching value, you basically need to walk up the collection until you find an entry which doesn't match.

您可以潛在地通過(guò)立即獲取低于您要查找的鍵的索引來(lái)使其更快,然后在兩者之間進(jìn)行二進(jìn)制切割 - 但我可能會(huì)選擇更簡(jiǎn)單的版本,除非您有大量相等的條目,否則它可能足夠高效".

You could potentially make it faster by fetching the index of a key immediately lower than the one you were looking for, then do a binary chop between the two - but I'd probably go for the simpler version, which is likely to be "efficient enough" unless you've got a really large number of equal entries.

這篇關(guān)于二分查找中的第一次出現(xiàn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周?chē)h(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 欧美一区二区三区日韩 | 麻豆av片| 中文二区 | 日韩中文字幕2019 | 操久久 | 一区久久 | 亚洲国产精品第一区二区 | 91视频网址| 欧美精品一二三区 | 韩日免费视频 | 国产美女一区二区 | 亚洲精品欧美 | 亚洲视频一区二区三区四区 | 国产精品免费福利 | 日韩视频中文字幕 | 久久亚洲精品视频 | 欧美精品一二三区 | 91www在线观看 | 91极品欧美视频 | 亚洲精品免费视频 | 黄色av网站免费看 | 密室大逃脱第六季大神版在线观看 | 美女艹b| 免费观看一级特黄欧美大片 | 免费在线观看成人av | 国产在线不卡视频 | 欧美视频免费在线观看 | 在线中文视频 | 日韩成人免费在线视频 | 久久精品伊人 | 国产91在线精品 | 超碰婷婷 | 91日日| 国产欧美精品一区二区色综合朱莉 | 九九久久精品 | 99免费精品视频 | 伊人狠狠操 | 国产精品久久久久久久久免费桃花 | 亚洲 中文 欧美 日韩 在线观看 | 亚洲精品久久久一区二区三区 | 欧美精品一区二区三区在线播放 |