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

使用二進(jìn)制搜索的多個(gè)鍵的最后索引?

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

問(wèn)題描述

我在排序數(shù)組中有多次出現(xiàn)的鍵,我想對(duì)它們執(zhí)行二進(jìn)制搜索,正常的二進(jìn)制搜索會(huì)為多次出現(xiàn)的鍵返回一些隨機(jī)索引,而我想要最后一次出現(xiàn)的索引那個(gè)鍵.

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,這個(gè)算法聽(tīng)起來(lái)不錯(cuò).無(wú)論如何,我已經(jīng)完成了自己的工作,這似乎我給出了更好的結(jié)果,但是如果有人可以幫助我衡量我的算法和@Mattias 的復(fù)雜性,或者任何人有更好的解決方案,歡迎.......無(wú)論如何,這是我為該問(wèn)題找到的解決方案,

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;
}

這是第一次出現(xiàn),我也更新了其他類(lèi)似的帖子 二分查找中的第一次出現(xiàn)

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;
}

這篇關(guān)于使用二進(jìn)制搜索的多個(gè)鍵的最后索引?的文章就介紹到這了,希望我們推薦的答案對(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(使用線程逐塊處理文件)
主站蜘蛛池模板: 国产高清亚洲 | 91视频在线看 | 一本一道久久a久久精品综合蜜臀 | 成人欧美一区二区 | 国产在线观看一区 | 一级黄色片一级黄色片 | 久久久久久久久国产 | 久久精品欧美视频 | 亚洲欧美激情四射 | 国产乱码精品1区2区3区 | 一区二区三区中文字幕 | 国产精品久久久久久久久久 | 日本精品一区二区三区视频 | 国产一区二区在线免费播放 | 日韩一区二区三区在线 | 97人人超碰| 色爱综合网| 亚洲h视频| 久久婷婷国产香蕉 | 韩日免费视频 | 亚洲精品一区二区在线观看 | 日屁视频 | 户外露出一区二区三区 | 99国内精品久久久久久久 | 91久久久久| 国产免费av在线 | 成人一区二区三区在线观看 | 色婷婷精品国产一区二区三区 | 成人在线观看网站 | 日韩欧美三区 | 麻豆久久久久久 | 国产精品久久久久一区二区三区 | 中文字幕av网站 | 99久久精品国产一区二区三区 | 亚洲国产aⅴ成人精品无吗 亚洲精品久久久一区二区三区 | 欧美亚洲国产成人 | 免费人成激情视频在线观看冫 | 国产精品久久久久久久久久久久久久 | 黄网站在线播放 | 国产极品粉嫩美女呻吟在线看人 | 欧美成人精品一区二区男人看 |