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

使用二進制搜索的多個鍵的最后索引?

Last index of multiple keys using binary-search?(使用二進制搜索的多個鍵的最后索引?)
本文介紹了使用二進制搜索的多個鍵的最后索引?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在排序數組中有多次出現的鍵,我想對它們執行二進制搜索,正常的二進制搜索會為多次出現的鍵返回一些隨機索引,而我想要最后一次出現的索引那個鍵.

i have multiple occurrences of a key in a sorted array, and i want to perform binary search on them, a normal binary search returns some random index for the key having multiple occurrences, where as i want the index of the last occurrence of that key.

int data[] = [1,2,3,4,4,4,4,5,5,6,6];
int key = 4;
int index = upperBoundBinarySearch(data, 0, data.length-1, key);

Index Returned = 6

推薦答案

好吧,特別感謝@Mattias,這個算法聽起來不錯.無論如何,我已經完成了自己的工作,這似乎我給出了更好的結果,但是如果有人可以幫助我衡量我的算法和@Mattias 的復雜性,或者任何人有更好的解決方案,歡迎.......無論如何,這是我為該問題找到的解決方案,

Well, thanks to all especially @Mattias, that algo sounds good. anyway i have done with my own, that seem me to give better result, but if some one can help me to measure out the complexity of both algos mine and @Mattias, or any one has some better solution, it welcome..... anyhow here is the solution i found for the problem,

int upperBound(int[] array,int lo, int hi, int key)
{
    int low = lo-1, high = hi;
    while (low+1 != high)
    {
        int mid = (low+high)>>>1;
        if (array[mid]> key) high=mid;
        else low=mid;
    }
    int p = low;
    if ( p >= hi || array[p] != key )
        p=-1;//no key found
    return p;
}

這是第一次出現,我也更新了其他類似的帖子 二分查找中的第一次出現

this is for first occurrence, i also update the same with one other similar post First occurrence in a binary search

int lowerBound(int[] array,int lo, int hi, int key)
{
    int low = lo-1, high = hi;
    while (low+1 != high)
    {
        int mid = (low+high)>>>1;
        if (array[mid]< key) low=mid;
        else high=mid;
    }
    int p = high;
    if ( p >= hi || array[p] != key )
        p=-1;//no key found
    return p;
}

這篇關于使用二進制搜索的多個鍵的最后索引?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 草久久| 久热m3u8| 9999精品视频 | 国产一区二区影院 | 日本精品一区二区 | 亚洲高清成人在线 | 亚洲精品一区二区三区 | 蜜桃视频在线观看免费视频网站www | 欧美激情亚洲激情 | 亚洲www啪成人一区二区 | 日韩电影一区 | 久久精品国产一区老色匹 | 在线视频99 | 中文字幕日韩一区二区 | 人人种亚洲 | 欧美精品一区在线 | 中文字幕亚洲视频 | 国产精品久久久久久久 | 韩国av一区二区 | 高清亚洲| 日韩欧美精品在线 | 国产亚洲精品成人av久久ww | 中文字幕视频一区 | 一区二区三区四区在线 | 国产精品久久久久久久久久免费看 | 午夜在线免费观看 | 亚洲啊v在线 | 国产精品a级 | 亚洲欧美日韩中文字幕一区二区三区 | 一区二区在线不卡 | 国产一级视频免费播放 | 日批日韩在线观看 | 免费 视频 1级 | 日本电影韩国电影免费观看 | 羞羞视频免费观 | 亚洲精品视频在线观看视频 | 久草电影网 | 成人亚洲 | 国产精久久久久久久妇剪断 | 美女久久视频 | 欧美成人免费在线视频 |