1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實戰(zhàn)16: 最接近的三數(shù)之和

        共 684字,需瀏覽 2分鐘

         ·

        2020-08-21 21:47

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


        今天和大家聊的問題叫做最接近的三數(shù)之和?,我們先來看題面:

        https://leetcode-cn.com/problems/3sum-closest/


        Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.


        題意


        給定一個包括 n 個整數(shù)的數(shù)組 nums 和 一個目標(biāo)值 target。找出 nums 中的三個整數(shù),使得它們的和與 target 最接近。返回這三個數(shù)的和。假定每組輸入只存在唯一答案。

        樣例


        示例:

        輸入:nums = [-1,2,1,-4], target = 1
        輸出:2
        解釋:與 target 最接近的和是 2 (-1 + 2 + 1 = 2) 。
        ?

        提示:

        3 <= nums.length <= 10^3
        -10^3?<= nums[i]?<= 10^3
        -10^4?<= target?<= 10^4



        題解


        排序和雙指針


        • 本題目因為要計算三個數(shù),如果靠暴力枚舉的話時間復(fù)雜度會到 O(n3)

        • 首先進行數(shù)組排序,時間復(fù)雜度O(nlogn)

        • 在數(shù)組 nums 中,進行遍歷,每遍歷一個值利用其下標(biāo)i,形成一個固定值 nums[i]

        • 再使用前指針指向 start = i + 1 處,后指針指向 end = nums.length - 1 處,也就是結(jié)尾處

        • 根據(jù) sum = nums[i] + nums[start] + nums[end] 的結(jié)果,判斷 sum 與目標(biāo) target 的距離,如果更近則更新結(jié)果 ans

        • 同時判斷 sum 與 target 的大小關(guān)系,因為數(shù)組有序,如果 sum > target 則 end--,如果 sum < target 則 start++,如果 sum == target 則說明距離為 0 直接返回結(jié)果

        • 整個遍歷過程,固定值為 n 次,雙指針為 n 次,時間復(fù)雜度為 O(n2)

        • 總時間復(fù)雜度:O(nlogn) + O(n2) = O(n2)

        O(n^2)


        class?Solution?{
        ????public?int?threeSumClosest(int[] nums, int?target)?{
        ????????Arrays.sort(nums);
        ????????int?ans = nums[0] + nums[1] + nums[2];
        ????????for(int?i=0;i????????????int?start = i+1, end = nums.length - 1;
        ????????????while(start < end) {
        ????????????????int?sum = nums[start] + nums[end] + nums[i];
        ????????????????if(Math.abs(target - sum) < Math.abs(target - ans))
        ????????????????????ans = sum;
        ????????????????if(sum > target)
        ????????????????????end--;
        ????????????????else?if(sum < target)
        ????????????????????start++;
        ????????????????else
        ????????????????????return?ans;
        ????????????}
        ????????}
        ????????return?ans;
        ????}
        }



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


        上期推文:


        LeetCode刷題實戰(zhàn)1:在數(shù)組上遍歷出花樣

        LeetCode刷題實戰(zhàn)2:用鏈表模擬加法

        LeetCode刷題實戰(zhàn)3:最長不重復(fù)子串

        LeetCode刷題實戰(zhàn)4:兩個正序數(shù)組的中位數(shù)

        LeetCode刷題實戰(zhàn)5:判斷回文子串

        LeetCode刷題實戰(zhàn)6:Z字形變換

        LeetCode刷題實戰(zhàn)7:整數(shù)反轉(zhuǎn)

        LeetCode刷題實戰(zhàn)8:字符串轉(zhuǎn)換整數(shù)

        LeetCode刷題實戰(zhàn)9:求解回文數(shù)

        LeetCode刷題實戰(zhàn)10:字符串正則匹配

        LeetCode刷題實戰(zhàn)11: 盛最多水的容器

        LeetCode刷題實戰(zhàn)12: 整數(shù)轉(zhuǎn)羅馬數(shù)字

        LeetCode刷題實戰(zhàn)13: 羅馬數(shù)字轉(zhuǎn)整數(shù)


        瀏覽 36
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

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

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            人妻少妇无码精品 | 污网址| 靠逼视频网页观看 | 日韩一级影视 | 一级A片女处破 | 亚洲AV无码影片 | 俺去操逼| 欧美老女人性 | 青青草国产免费 | 成人午夜精品无码区毛片四季视频 |