leetcode001题目
两数之和,使用hashmap可以让时间复杂度为O(1)
class solution:
def twoSum(self,nums,target):
"""
built-in method .index
:param nums: list
:param target: int
:return:tuple, (index1, index2)
"""
hasp_map = {}
for index,value in enumerate(nums):
hasp_map[value] = index
for index,value in enumerate(nums):
if target - value in hasp_map:
if index != hasp_map[target-value]:
return index,hasp_map[target-value]
class Solution_another:
def twoSum(self, num, target):
"""
built-in method .index
:param num: list
:param target: int
:return: tuple, (index1, index2)
"""
nums = num
for ind1, val in enumerate(nums):
try:
ind2 = nums.index(target - val)
if ind1 != ind2:
return ind1, ind2
except ValueError:
continue
三数之和lletcode015
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
使用类似快排的方法,先排序,然后扫一遍,在左边和右边设立指针
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
res = []
for i, num in enumerate(nums):
if i == 0 or nums[i] > nums[i - 1]:
# num + b + c == 0
left = i + 1
right = len(nums) - 1
while(left < right):
s = num + nums[left] + nums[right]
if s == 0:
res.append([num, nums[left], nums[right]])
left += 1
right -= 1
while(left < right and nums[left] == nums[left - 1]):
left += 1
while(left < right and nums[right + 1] == nums[right]):
right -= 1
# break
elif s > 0:
right -= 1
else:
left += 1
return res
leetcode016最接近的三数之和
同理类似三数之和的套路
class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort()
res = nums[0] + nums[1] + nums[2]
for i, num in enumerate(nums):
left, right = i + 1, len(nums) - 1
while left < right:
s = num + nums[left] + nums[right]
# print s, res, abs(s - target), abs(res - target)
if abs(s - target) < abs(res - target):
res = s
if s == target:
return s
elif s < target:
left += 1
else:
right -= 1
return res
leetcode018四数之和
先计算所有的两数之和,然后在两数之和之中找到四数之和
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
"""
if len(nums) < 4:
return []
nums.sort()
res = []
def findThreeSum(num, l, t):
for i, n in enumerate(l):
if i == 0 or l[i] > l[i - 1]:
left, right = i + 1, len(l) - 1
while(left < right):
s = n + l[left] + l[right]
if s == t:
res.append([num, n, l[left], l[right]])
left += 1
right -= 1
while(left < right and l[left] == l[left - 1]):
left += 1
while(right > left and l[right] == l[right + 1]):
right -= 1
elif s > t:
right -= 1
else:
left += 1
for i, num in enumerate(nums):
if i == 0 or nums[i] > nums[i - 1]:
findThreeSum(num, nums[i + 1:], target - num)
return res
leetcode259 较小的三数之和
class Solution(object):
def threeSumSmaller(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
res = 0
nums.sort()
for i, num in enumerate(nums):
# 现在要在比i大的下标里,找能组合出target - num 的两个数的组合
t = target - num
left, right = i + 1, len(nums) - 1
while left < right:
if nums[left] + nums[right] >= t:
right -= 1 #和太大了,尝试把和缩小
elif nums[left] + nums[right] < t:
res += right - left #在这种情况下,left可以与【left + 1, right】间任意一个数组成一组答案
left += 1
return res