1、三数之和
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
# import itertools
# import operator
# # product 笛卡尔积 (有放回抽样排列)
# # permutations 排列 (不放回抽样排列)
# # combinations 组合,没有重复 (不放回抽样组合)
# # combinations_with_replacement 组合,有重复 (有放回抽样组合)
# result = []
# for i in itertools.combinations(nums, 3):
# if sum(i)==0:
# if len(result)==0:
# result.append(list(i))
# else:
# flag = True
# j = 0
# while j<len(result) and flag:
# # print(result[j],list(i),operator.eq(sorted(result[j]), sorted(list(i))))
# if operator.eq(sorted(result[j]), sorted(list(i))):
# flag = False
# j = j+1
# if flag:
# result.append(list(i))
n = len(nums)
nums.sort()
ans = list()
# 枚举 a
for first in range(n):
# 需要和上一次枚举的数不相同
if first > 0 and nums[first] == nums[first - 1]:
continue
# c 对应的指针初始指向数组的最右端
third = n - 1
target = -nums[first]
# 枚举 b
for second in range(first + 1, n):
# 需要和上一次枚举的数不相同
if second > first + 1 and nums[second] == nums[second - 1]:
continue
# 需要保证 b 的指针在 c 的指针的左侧
while second < third and nums[second] + nums[third] > target:
third -= 1
# 如果指针重合,随着 b 后续的增加
# 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
if second == third:
break
if nums[second] + nums[third] == target:
ans.append([nums[first], nums[second], nums[third]])
return ans
2、矩阵置零
class Solution(object):
def setZeroes(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
R = len(matrix)
C = len(matrix[0])
rows, cols = set(), set()
# Essentially, we mark the rows and columns that are to be made zero
for i in range(R):
for j in range(C):
if matrix[i][j] == 0:
rows.add(i)
cols.add(j)
# Iterate over the array once again and using the rows and cols sets, update the elements
for i in range(R):
for j in range(C):
if i in rows or j in cols:
matrix[i][j] = 0
3、字母异位词分组
class Solution(object):
def groupAnagrams(self, strs):
"""
:type strs: List[str]
:rtype: List[List[str]]
"""
if len(strs)==0:
return []
mode = []
result = []
mode.append(sorted(list(strs[0])))
temp = []
temp.append(strs[0])
result.append(temp)
for i in range(1,len(strs)):
Set = sorted(list(strs[i]))
if Set in mode:
index = mode.index(Set)
result[index].append(strs[i])
else:
mode.append(Set)
temp = []
temp.append(strs[i])
result.append(temp)
return result
4、无重复字符的最长字串
class Solution:
def lengthOfLongestSubstring(self,s):
if not s:
return 0
left = 0
lookup = set()
n = len(s)
max_len = 0
cur_len = 0
for i in range(n):
cur_len += 1
while s[i] in lookup:
lookup.remove(s[left])
left += 1
cur_len -= 1
if cur_len > max_len:max_len = cur_len
lookup.add(s[i])
return max_len
5、最长回文子串
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
for length in range(len(s), -1, -1):
print(length)
for index in range(0, len(s) - length + 1):
sub_string = s[index:length + index]
if sub_string == sub_string[::-1]:
return sub_string
return ""
6、递增的三元子序列
class Solution(object):
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
# length = len(nums)
# if length<3:
# return False
# firstIndex = nums.index(min(nums))
# # print(firstIndex)
# if firstIndex+3>length:
# return False
# while firstIndex<length-3+1:
# firstNum = nums[firstIndex]
# secondIndex = firstIndex+1
# while secondIndex<length-3+2:
# secondNum = nums[secondIndex]
# if secondNum>firstNum:
# thirdIndex = -1
# while thirdIndex>secondIndex-length:
# thirdNum = nums[thirdIndex]
# if thirdNum>secondNum:
# return True
# else:
# thirdIndex = thirdIndex-1
# secondIndex = secondIndex + 1
# else:
# secondIndex = secondIndex+1
# firstIndex = firstIndex+1
# return False
a, b = float("inf"), float("inf")
for v in nums:
if v <= a: a = v
elif v <= b: b = v
else: return True
return False