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)259:較小的三數(shù)之和

        共 3656字,需瀏覽 8分鐘

         ·

        2021-05-10 14:31

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

        今天和大家聊的問題叫做 較小的三數(shù)之和,我們先來看題面:
        https://leetcode-cn.com/problems/3sum-smaller/

        Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target.

        給定一個長度為 n 的整數(shù)數(shù)組和一個目標值 target,尋找能夠使條件 nums[i] + nums[j] + nums[k] < target 成立的三元組 i, j, k 個數(shù)(0 <= i < j < k < n)。

        示例


        示例:
        輸入: nums = [-2,0,1,3], target = 2
        輸出: 2 
        解釋: 因為一共有兩個三元組滿足累加和小于 2:
             [-2,0,1]
             [-2,0,3]
        進階:是否能在 O(n2) 的時間復(fù)雜度內(nèi)解決?


        解題

        https://segmentfault.com/a/1190000003794736

        先對整個數(shù)組排序,然后一個外層循環(huán)確定第一個數(shù),然后里面使用頭尾指針left和right進行夾逼,得到三個數(shù)的和。如果這個和大于或者等于目標數(shù),說明我們選的三個數(shù)有點大了,就把尾指針right向前一位(因為是排序的數(shù)組,所以向前肯定會變小)。如果這個和小于目標數(shù),那就有right - left個有效的結(jié)果。為什么呢?因為假設(shè)我們此時固定好外層的那個數(shù),還有頭指針left指向的數(shù)不變,那把尾指針向左移0位一直到左移到left之前一位,這些組合都是小于目標數(shù)的。

        public class Solution {
            public int threeSumSmaller(int[] nums, int target) {
                // 先將數(shù)組排序
                Arrays.sort(nums);
                int cnt = 0;
                for(int i = 0; i < nums.length - 2; i++){
                    int left = i + 1, right = nums.length - 1;
                    while(left < right){
                        int sum = nums[i] + nums[left] + nums[right];
                        // 如果三個數(shù)的和大于等于目標數(shù),那將尾指針向左移
                        if(sum >= target){
                            right--;
                        // 如果三個數(shù)的和小于目標數(shù),那將頭指針向右移
                        } else {
                            // right - left個組合都是小于目標數(shù)的
                            cnt += right - left;
                            left++;
                        }
                    }
                }
                return cnt;
            }
        }


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

        上期推文:
        LeetCode1-240題匯總,希望對你有點幫助!
        LeetCode刷題實戰(zhàn)241:為運算表達式設(shè)計優(yōu)先級
        LeetCode刷題實戰(zhàn)242:有效的字母異位詞
        LeetCode刷題實戰(zhàn)243:最短單詞距離
        LeetCode刷題實戰(zhàn)244:最短單詞距離 II
        LeetCode刷題實戰(zhàn)245:最短單詞距離 III
        LeetCode刷題實戰(zhàn)246:中心對稱數(shù)
        LeetCode刷題實戰(zhàn)247:中心對稱數(shù)II
        LeetCode刷題實戰(zhàn)248:中心對稱數(shù)III
        LeetCode刷題實戰(zhàn)249:移位字符串分組
        LeetCode刷題實戰(zhàn)250:統(tǒng)計同值子樹
        LeetCode刷題實戰(zhàn)251:展開二維向量
        LeetCode刷題實戰(zhàn)252:會議室
        LeetCode刷題實戰(zhàn)253:會議室II
        LeetCode刷題實戰(zhàn)254:因子的組合
        LeetCode刷題實戰(zhàn)255:驗證前序遍歷序列二叉搜索樹
        LeetCode刷題實戰(zhàn)256:粉刷房子
        LeetCode刷題實戰(zhàn)257:二叉樹的所有路徑

        LeetCode刷題實戰(zhàn)258:各位相加


        瀏覽 48
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

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

        手機掃一掃分享

        分享
        舉報
        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>
            91黄色视频免费观看 | 大香蕉色综合av中文网 | 精品 99| 国产又粗又猛又爽视频 | 国产6区| 极品少妇被粗大爽 | 吻胸摸腿娇喘揉屁股娇喘软件 | 久久女人 | 老司机精品91 | 大香蕉插逼|