<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實(shí)戰(zhàn)34:在排序數(shù)組中查找元素的第一個(gè)和最后一個(gè)位置

          共 2357字,需瀏覽 5分鐘

           ·

          2020-09-10 15:31

          算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !


          今天和大家聊的問(wèn)題叫做在排序數(shù)組中查找元素的第一個(gè)和最后一個(gè)位置,我們先來(lái)看題面:

          https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array

          Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.

          Your algorithm's runtime complexity must be in the order of O(log n).

          If the target is not found in the array, return [-1, -1].

          題意


          給定一個(gè)按照升序排列的整數(shù)數(shù)組 nums,和一個(gè)目標(biāo)值 target。找出給定目標(biāo)值在數(shù)組中的開始位置和結(jié)束位置。
          你的算法時(shí)間復(fù)雜度必須是 O(log n) 級(jí)別。
          如果數(shù)組中不存在目標(biāo)值,返回 [-1, -1]。

          樣例


          示例 1:

          輸入: nums = [5,7,7,8,8,10], target = 8
          輸出: [3,4]

          示例?2:

          輸入: nums = [5,7,7,8,8,10], target = 6
          輸出: [-1,-1]

          題解

          思路1:手動(dòng)二分法!
          這里提供兩種方法,一種容易理解,一種萬(wàn)能公式!
          版本1:是指在二分法進(jìn)行時(shí),就判讀是否有等于target的,但是找到的target不知道是最左邊的數(shù)還是最右邊的數(shù),所以,通過(guò)找到這個(gè)數(shù)再找出相應(yīng)的邊界值.
          版本2:是指二分法執(zhí)行完畢,返回target在最左邊的位置,在求出另一個(gè)邊界!
          關(guān)于詳細(xì)說(shuō)明,請(qǐng)看這篇[二分搜索](二分查找有幾種寫法?它們的區(qū)別是什么?- Jason Li的回答 - 知乎
          https://www.zhihu.com/question/36132386/answer/530313852),讀完之后,可以加深二分搜索的理解!


          class?Solution?{
          ????public?int[] searchRange(int[] nums, int?target) {
          ????????int[] res = {-1, -1};
          ????????int?n = nums.length;
          ????????if?(n == 0) return?res;
          ????????int?left = 0;
          ????????int?right = n-1;
          ????????while?(left <= right){
          ????????????int?mid = left + (right - left) / 2;
          ????????????if?(nums[mid] == target) {
          ????????????????left = mid;
          ????????????????right = mid;
          ????????????????while?(left > 0?&& nums[left] == nums[left-1]) left--;
          ????????????????while?(right < n - 1?&& nums[right] == nums[right+1]) right++;
          ????????????????res[0] = left;
          ????????????????res[1] = right;
          ????????????????return?res;
          ????????????}
          ????????????else?if?(nums[mid] > target) right = mid - 1;
          ????????????else?left = mid + 1;
          ????????}
          ????????return?res;
          ????????
          ????}
          }


          思路2:庫(kù)函數(shù)
          python 的?bisect庫(kù)
          簡(jiǎn)要介紹一下,
          bisect.bisect_left(a,x,lo=0,hi=len(a))a中找x最左邊數(shù)的索引,如果找不到就返回插入的索引.
          bisect.bisect(a, x, lo=0, hi=len(a)) 找最右邊的!

          思路3:
          分而治之,參考鏈接:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/discuss/14707/9-11-lines-O(log-n)
          當(dāng)然上面的時(shí)間復(fù)雜度都是:O(logn)" role="presentation" style=" display: inline; line-height: normal; word-spacing: normal; overflow-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border-width: 0px; border-style: initial; border-color: initial; ">O(logn)

          本文有點(diǎn)參考于
          https://www.cnblogs.com/powercai/p/10826397.html
          好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力。


          上期推文:


          LeetCode1-20題匯總,速度收藏!
          LeetCode刷題實(shí)戰(zhàn)21:合并兩個(gè)有序鏈表
          LeetCode刷題實(shí)戰(zhàn)23:合并K個(gè)升序鏈表
          LeetCode刷題實(shí)戰(zhàn)24:兩兩交換鏈表中的節(jié)點(diǎn)
          LeetCode刷題實(shí)戰(zhàn)25:K 個(gè)一組翻轉(zhuǎn)鏈表
          LeetCode刷題實(shí)戰(zhàn)26:刪除排序數(shù)組中的重復(fù)項(xiàng)
          LeetCode刷題實(shí)戰(zhàn)27:移除元素
          LeetCode刷題實(shí)戰(zhàn)28:實(shí)現(xiàn) strStr()
          LeetCode刷題實(shí)戰(zhàn)29:兩數(shù)相除
          LeetCode刷題實(shí)戰(zhàn)30:串聯(lián)所有單詞的子串
          LeetCode刷題實(shí)戰(zhàn)31:下一個(gè)排列
          LeetCode刷題實(shí)戰(zhàn)32:最長(zhǎng)有效括號(hào)
          LeetCode刷題實(shí)戰(zhàn)33:搜索旋轉(zhuǎn)排序數(shù)組


          瀏覽 64
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天日日天天爱人人爱人人爽 | 午夜三级无码在线看 | 18xxxxxxxxx日本超碰 | 午夜精品一区二区三区视频免费看 | 五月天亚洲丁香无码 |