<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刷題實戰(zhàn)35: 搜索插入位置

          共 1246字,需瀏覽 3分鐘

           ·

          2020-09-10 15:30

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


          今天和大家聊的問題叫做?搜索插入位置,我們先來看題面:

          https://leetcode-cn.com/problems/search-insert-position/

          Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

          You may assume no duplicates in the array.

          題意


          給定一個排序數(shù)組和一個目標值,在數(shù)組中找到目標值,并返回其索引。如果目標值不存在于數(shù)組中,返回它將會被按順序插入的位置。
          你可以假設數(shù)組中無重復元素。

          樣例


          示例 1:
          輸入: [1,3,5,6], 5
          輸出: 2

          示例?2:
          輸入: [1,3,5,6], 2
          輸出: 1

          示例 3:
          輸入: [1,3,5,6], 7
          輸出: 4

          示例 4:
          輸入: [1,3,5,6], 0
          輸出: 0


          題解

          這題應該算簡單題吧,主要有兩種解法:暴力解法和二分查找法 。

          一種方法是直接遍歷有序的數(shù)組,發(fā)現(xiàn)元素大于等于target,那么這個索引就是目標索引了。時間復雜度是O(n)

          public?int?searchInsert(int[] nums, int?target)?{
          ????????if?(nums.length == 0) {
          ????????????return?0;
          ????????}
          ????????for?(int?i = 0; i < nums.length; i++) {
          ????????????if?(nums[i] >= target)
          ????????????????return?i;
          ????????}
          ????????return?nums.length;
          ????}


          采用二分查找算法,定義和左右邊界縮小查找范圍。二分查找所需的時間復雜度為 O(logn)

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


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力。


          上期推文:


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


          瀏覽 42
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <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>
                  亚洲国产高清精品 | 淫色一区二区 | 日本18禁网站 | 99中文视频 | 欧美a一级片免费 |