国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

[Leetcode] 15. 3Sum

2019-11-14 09:39:45
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

PRoblem:

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]

思路 1. 暴力枚舉法+分類(lèi)討論法。這種題直接暴力枚舉的話,時(shí)間復(fù)雜度高達(dá)O(n^3),所以我一開(kāi)始對(duì)暴力枚舉加上了分類(lèi)討論。三個(gè)數(shù)之和為0有以下幾種情況:

情況 負(fù)數(shù)個(gè)數(shù) 零個(gè)數(shù) 正數(shù)個(gè)數(shù)
1 2個(gè) 0個(gè) 1個(gè)
2 1個(gè) 0個(gè) 2個(gè)
3 1個(gè) 1個(gè) 1個(gè)
4 0個(gè) 3個(gè) 0個(gè)

因此,首先對(duì)輸入的數(shù)組進(jìn)行“正數(shù),零,負(fù)數(shù)”分類(lèi),然后再窮舉這四種情況的所有組合。最后發(fā)現(xiàn),時(shí)間復(fù)雜度仍然接近O(n^3),這優(yōu)化意義不大。

#Note: 以下兩種方法是題目“2sum”的延伸。“2sum”求的是:對(duì)于輸入的數(shù)組,求出兩數(shù)之和等于某特定值的所有組合。在這題目“3sum”中,因?yàn)轭}目是求a,b,c使得a+b+c=0,所以我們可以看作是求a,b使得a+b=-c。這樣其實(shí)就是窮舉版的“2sum”問(wèn)題。 2. 借助hash表。對(duì)輸入的數(shù)組放入一個(gè)哈希表中,然后遍歷哈希表的每一個(gè)數(shù)a,然后調(diào)用2sum函數(shù):新建一個(gè)用于標(biāo)記的集合set,遍歷輸入數(shù)組的每一個(gè)值b(此時(shí)要去掉一個(gè)數(shù)a,因?yàn)閿?shù)具有不可復(fù)用性),判斷集合set中是否存在一個(gè)數(shù)c,使得c = -a-b。如果有,則為目標(biāo)答案,加到result list中;如果沒(méi)有,則把b數(shù)加入到集合set中。 這樣做的好處在于減少了一層循環(huán),使得時(shí)間復(fù)雜度降為O(n^2),但同時(shí)空間復(fù)雜度是O(n)。另外,調(diào)用2sum函數(shù)時(shí),也可以遍歷已建立好的hash表,可以更節(jié)省時(shí)間。不過(guò)需要注意的是要考慮[0,0,0]這種情況。 3. 雙向游標(biāo)法。首先將輸入數(shù)組nums排序,然后遍歷輸入數(shù)組的每個(gè)數(shù)a作為target值,然后調(diào)用2sum函數(shù):對(duì)于有序數(shù)組(除去數(shù)a),定義兩個(gè)指針指向首尾兩端,即,一個(gè)指向nums[0],一個(gè)指向num[len(nums)]。然后有如下三種情況: (1) 若當(dāng)前和小于-a,則左指針右移; (2) 若當(dāng)前和大于-a,則右指針左移; (3) 若當(dāng)前和等于-a,則為目標(biāo)答案。 這里排序的時(shí)間復(fù)雜度為O(nlogn),遍歷的時(shí)間復(fù)雜度為O(n^2),所以總的時(shí)間復(fù)雜度近似于O(n^2).

Solution :

方法2

# -*- coding: utf-8 -*-"""Created on Sat Feb 04 22:30:34 2017@author: liangsht"""def find2Sum(self,target,newdict): hashset = set() for item in newdict.keys(): remain = target - item if remain in hashset: if remain != item: self.myappend([-target,item,remain]) elif newdict[item] >= 2: self.myappend([-target,item,remain]) else: hashset.add(item)class Solution(object): res = [] def myappend(self,occupylist): occupylist.sort() if occupylist not in self.lll: self.res.append(occupylist) def threeSum(self, nums): self.res = [] numslen = len(nums) if numslen <= 2: return [] hashdict = dict() hashdict[int(nums[0])] = 1 for i in xrange(1, numslen): #construct a hash map for list nums if nums[i] in hashdict.keys(): hashdict[nums[i]] += 1 else: hashdict[nums[i]] = 1 for item in hashdict.keys(): if item == 0 and hashdict[item] >=3: self.myappend([0,0,0]) continue target = 0-item newdict = hashdict.copy() hashdict[item] -= 1 if hashdict[item] == 0: newdict.pop(item) find2Sum(self,target,newdict) return self.res

方法3

def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i > 0 and nums[i] == nums[i-1]: continue l, r = i+1, len(nums)-1 while l < r: s = nums[i] + nums[l] + nums[r] if s < 0: l +=1 elif s > 0: r -= 1 else: res.append((nums[i], nums[l], nums[r])) while l < r and nums[l] == nums[l+1]: l += 1 while l < r and nums[r] == nums[r-1]: r -= 1 l += 1; r -= 1 return res

ref: 1. CSDN bolg 2. leetcode discussion


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜南县| 湖南省| 砀山县| 兴业县| 麻江县| 黔东| 潮州市| 阿克苏市| 临颍县| 大足县| 金昌市| 安庆市| 阳信县| 威海市| 巨野县| 石林| 松江区| 宁乡县| 南丰县| 海原县| 泰安市| 延安市| 喀喇| 嫩江县| 平邑县| 长垣县| 罗江县| 乐至县| 绥滨县| 绍兴市| 山西省| 肃南| 甘德县| 罗山县| 奇台县| 利津县| 青州市| 调兵山市| 汉川市| 桃源县| 宜都市|