<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)16: 最接近的三數(shù)之和

          共 684字,需瀏覽 2分鐘

           ·

          2020-08-21 21:47

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號后續(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.


          題意


          給定一個(gè)包括 n 個(gè)整數(shù)的數(shù)組 nums 和 一個(gè)目標(biāo)值 target。找出 nums 中的三個(gè)整數(shù),使得它們的和與 target 最接近。返回這三個(gè)數(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



          題解


          排序和雙指針


          • 本題目因?yàn)橐?jì)算三個(gè)數(shù),如果靠暴力枚舉的話時(shí)間復(fù)雜度會到 O(n3)

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

          • 在數(shù)組 nums 中,進(jìn)行遍歷,每遍歷一個(gè)值利用其下標(biāo)i,形成一個(gè)固定值 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

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

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

          • 總時(shí)間復(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;
          ????}
          }



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


          上期推文:


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

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

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

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

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

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

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

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

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

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

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

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

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


          瀏覽 36
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

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

          手機(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>
                  蜜桃传媒一区二区亚洲AV | 99热这里都是精品 | 成人一区二区三区四区五区91电影 | 久成人肜视 | 亚洲中文免费 |