每天打卡做题,我这样的懒癌晚期编程渣渣还有救吗???
各种list求和
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums)):
if nums[i]<=target:
res=target-nums[i]
if res in nums:
j=nums.index(res)
return [i,j]
else:
print('wrong')
特值测试不过,e.g:[3,3],6;[3,2,4],6 为什么特闷都想得到这么仔细呢
好评答案(我是想不到的5555):O(n)
class Solution(object):
def twoSum(self, nums, target):
if len(nums) <= 1:
return False
buff_dict = {}
for i in range(len(nums)):
if nums[i] in buff_dict:
return [buff_dict[nums[i]], i]
else:
buff_dict[target - nums[i]] = i
还有用hash的,不过Python的字典不就这想法?
以此开始好好学习
three sum
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] ]
这是参考了昨天的高分回答做的,这个改进并没有用,还是缺解,我的脑子里的窟窿大概比陨石还大吧
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ if len(nums)<3: print('nums too short to oprate...') return None res_dict={} #nums=num.sort() for i in range(len(nums)): for j in range(len(nums)): a=nums[i]+nums[j] if -a in res_dict: return [nums[i],nums[j],-a] elif -a in nums: res_dict[-a]=[nums[i],nums[j]]
class Solution(object): def threeSum(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ if len(nums)<3: print('nums too short to oprate...') return None #nums=num.sort() for i in range(len(nums)): for j in range(len(nums)): a=nums[i]+nums[j] if -a in nums: return [nums[i],nums[j]-a] #最初的版本,真希望我以后可以好好嘲笑我现在的思维有多单蠢
别人家的孩子,不不不,别人家的答案,人家就想到了用两个额外的指针操作,像极了快速排序,你们这活学活用的脑子哪儿买的还是国家发的?我还来得及吗:
def threeSum(self, nums): res = [] nums.sort()#排序处理的好 for i in xrange(len(nums)-2): if i > 0 and nums[i] == nums[i-1]: #注意是i!=i-1,而不是i+1,要确保整个list的遍历,不然会丢解的 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
终极的各种求和,根本是两个数求和,然后递归调用,递归,两数求和利用指针
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
def find_Nsums(nums,target,N,per_container,results):
#N:几个数
#per_container:每次递归调用返回的当前(两个)数值,初始为空(list),这个巨坑,反正我想不到,看成buff,每次递归结束就填进去俩数
if len(nums)<N or target<nums[0]*N or target>nums[-1]*N: return if N==2: l=0 r=len(nums)-1 while(l<r): s=nums[l]+nums[r] if s==target: resualts.append((per_container+[nums[l],nums[r]])) l+=1 #每次左边指针往后走,就相当于for了 while l<r and nums[l]==nums[l-1]:#前面先做了l+1,这里就是nums[l]==nums[l-1]的判断了,不然会出现重复解,如果结果要求返回index,那还不能加这句话 l+=1 elif s<target: l+=1 else: r-=1 else: for i in range(len(nums)-N+1):#注意这个len if i==0 or(i>0 and nums[i-1]!=nums[i]):#好好想想递归的条件 find_Nsums(nums[i+1:],target-nums[i],N-1,per_container+[nums[i]],results) resualts=[] find_Nsums(sorted(nums),target,4,[],results) return results