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

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

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

問題描述

我正在修改一些代碼,但我意識到了一些我從來不知道的事情.正常的二分搜索將在數(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è)沒有的條目 匹配.

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

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

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)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(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ù)庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 国精产品一品二品国精在线观看 | 国产美女一区二区 | 请别相信他免费喜剧电影在线观看 | 黄视频国产| 天天爽天天操 | 97国产在线观看 | 亚洲一区二区在线视频 | 四季久久免费一区二区三区四区 | 国产精品久久99 | 91视频大全| 51ⅴ精品国产91久久久久久 | 亚洲视频免费在线观看 | 人人干人人爽 | 成年女人免费v片 | 日韩乱码一二三 | 毛片黄 | 亚洲一区中文字幕 | 伊人网在线综合 | 欧美aaa级 | 亚洲第一av | 中文字幕在线一区二区三区 | 日韩在线一区视频 | 久久久久亚洲国产| 欧美在线高清 | 久久精品欧美电影 | 免费看91 | 91精品国产色综合久久不卡98 | 久久久久国产精品 | 国产精品爱久久久久久久 | 国产午夜精品一区二区三区在线观看 | 黄色一级视频 | av一级在线观看 | 日韩字幕一区 | 自拍偷拍欧美 | 伊人精品一区二区三区 | 国产亚洲精品精品国产亚洲综合 | 日韩精品一区二区三区中文字幕 | 欧美在线一区二区三区 | 午夜精品久久久久久久久久久久 | 中文字幕视频在线 | 日本a在线|