亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

leetcode16 3Sum Closest

Blackjun / 2334人閱讀

摘要:返回這三個(gè)值的和。思路一三指針這里的思路和是一樣的,就是用三個(gè)指針獲得三個(gè)值并計(jì)算他們的和。

題外話

鑒于這一題的核心思路和leetcode15的思路相同,可以先寫一下15題并參考一下我之前的一篇博客

題目要求

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

For example, given array S = {-1 2 1 -4}, and target = 1.

The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

翻譯過(guò)來(lái)就是,假設(shè)一個(gè)有n的元素的整數(shù)數(shù)組S,從S中找到三個(gè)值,這三個(gè)值的距離輸入的目標(biāo)值最近。返回這三個(gè)值的和。

思路一:三指針

這里的思路和leetcode15是一樣的,就是用三個(gè)指針獲得三個(gè)值并計(jì)算他們的和。不同的是和當(dāng)前的最小距離比較,代碼如下

public int threeSumClosest(int[] nums, int target) {
        Arrays.sort(nums);
        int length = nums.length;
        int closest = nums[0]+nums[1]+nums[2] - target;
        for(int i = 0 ; i0){
                    while(nums[k--] == nums[k] && j < k);
                }
                if(value==0){
                    return target;
                }
                if(Math.abs(value) < Math.abs(closest)){
                    closest = value;
                }        
            }
            while(nums[i] == nums[++i] && i < nums.length - 2);
        }
            
        return closest + target;
    }
思路二:hashmap等

同leetcode15,請(qǐng)直接參考我的博客


想要了解更多開發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號(hào)!將會(huì)不定期的發(fā)放福利哦~

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/67013.html

相關(guān)文章

  • leetcode 16 3Sum Closest

    摘要:題目詳情給定一個(gè)整數(shù)數(shù)組,我們需要找出數(shù)組中三個(gè)元素的加和,使這個(gè)加和最接近于輸入的數(shù)值。返回這個(gè)最接近的加和。找后兩個(gè)元素的時(shí)候,使用左右指針從兩端查找。 題目詳情 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, targe...

    atinosun 評(píng)論0 收藏0
  • [LintCode/LeetCode] 3Sum Closest

    摘要:這個(gè)題和的做法基本一樣,只要在循環(huán)內(nèi)計(jì)算和最接近的和,并賦值更新返回值即可。 Problem Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three intege...

    ShevaKuilin 評(píng)論0 收藏0
  • [Leetcode] 3Sum 4Sum 3Sum Closet 多數(shù)和

    摘要:為了避免得到重復(fù)結(jié)果,我們不僅要跳過(guò)重復(fù)元素,而且要保證找的范圍要是在我們最先選定的那個(gè)數(shù)之后的。而計(jì)算則同樣是先選一個(gè)數(shù),然后再剩下的數(shù)中計(jì)算。 2Sum 在分析多數(shù)和之前,請(qǐng)先看Two Sum的詳解 3Sum 請(qǐng)參閱:https://yanjia.me/zh/2019/01/... 雙指針?lè)?復(fù)雜度 時(shí)間 O(N^2) 空間 O(1) 思路 3Sum其實(shí)可以轉(zhuǎn)化成一個(gè)2Sum的題,...

    trigkit4 評(píng)論0 收藏0
  • leetcode 部分解答索引(持續(xù)更新~)

    摘要:前言從開始寫相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)懍F(xiàn)在翻起來(lái)覺得蠻亂的。可能大家看著也非常不方便。所以在這里做個(gè)索引嘻嘻。順序整理更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新更新 前言 從開始寫leetcode相關(guān)的博客到現(xiàn)在也蠻多篇了。而且當(dāng)時(shí)也沒有按順序?qū)憽F(xiàn)在翻起來(lái)覺得蠻亂的??赡艽蠹铱粗卜浅2环奖?。所以在這里做個(gè)索引嘻嘻。 順序整理 1~50 1...

    leo108 評(píng)論0 收藏0
  • 【LC總結(jié)】K Sum (Two Sum I II/3Sum/4Sum/3Sum Closest)

    摘要:找符合條件的總數(shù)。雙指針區(qū)間考慮邊界,長(zhǎng)度,為空,等。之后的范圍用雙指針和表示。若三個(gè)指針的數(shù)字之和為,加入結(jié)果數(shù)組。不要求,所以不用判斷了。同理,頭部?jī)蓚€(gè)指針向后推移,后面建立左右指針夾逼,找到四指針和為目標(biāo)值的元素。 Two Sum Problem Given an array of integers, find two numbers such that they add up ...

    awesome23 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<