1. 两数之和
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
result = []
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if target == (nums[i] + nums[j]) and i!=j:
return [i,j]
2. 49. 字母异位词分组
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
map1 = {}
for s in strs:
s1 = sorted(list(s))
s1 = ''.join(s1)
map1[s1] = map1.get(s1, []) + [s]
return [item for item in map1.values()]
3. 128. 最长连续序列
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
nums1 = sorted(list(set(nums)))
count = 0
result = 0
if nums == []:
return 0
for i in range(len(nums1)-1):
if nums1[i] +1 == nums1[i+1]:
count+=1
else:
count = 0
result = max(count, result)
return result + 1
4. 283. 移动零
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
for i in range(len(nums)):
if nums[i]==0:
nums.remove(nums[i])
nums.append(0)
return nums
5. 11. 盛最多水的容器
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right, res = 0, len(height)-1, 0
while(left<right):
if height[left] < height[right]:
res = max(res, height[left]*(right-left))
left += 1
else:
res = max(res, height[right]*(right-left))
right -= 1
return res
6. 15. 三数之和
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res, k =[], 0
for k in range(len(nums)-2):
i, j = k+1, len(nums)-1
if nums[k]>0: break
if k> 0 and nums[k]==nums[k-1]:continue
while i< j:
s = nums[i] + nums[j] + nums[k]
if s < 0:
i+=1
while i < j and nums[i]==nums[i-1]:i+=1
elif s > 0:
j-=1
while i<j and nums[j]==nums[j+1]:j-=1
else:
res.append([nums[i],nums[j],nums[k]])
i+=1
j-=1
while i < j and nums[i]==nums[i-1]:i+=1
while i < j and nums[j]==nums[j+1]:j-=1
return res
思路:滑动窗口,用window存遍历过的元素,一旦碰到相同的元素,就进入while会不断删除重复的那个元素之前的以及这个重复的元素。
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
left, result = 0, 0
window = []
for right, c in enumerate(s):
while c in window:
window.remove(window[0])
left+=1
window.append(c)
result = max(result, right-left+1)
return result
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
slow = 0
res = []
list2 = list(p)
list2.sort()
for fast in range(len(p)-1,len(s)):
list1 = (list(s[slow:fast+1]))
list1.sort()
if list1==list2:
res.append(slow)
slow+=1
return res
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
s_len, p_len = len(s), len(p)
if s_len < p_len:
return []
s_count = [0]*26
p_count = [0]*26
ans = []
for i in range(p_len):
s_count[ord(s[i])-97] +=1
p_count[ord(p[i])-97] +=1
if p_count == s_count:
ans.append(0)
for i in range(s_len-p_len):
s_count[ord(s[i])-97]-=1
s_count[ord(s[i+p_len])-97]+=1
if s_count == p_count:
ans.append(i+1)
return ans
class Solution:
def subarraySum(nums,k):
ps_count = {0:1}
res = 0
ps = 0
for num in nums:
ps += num
res += ps_count(ps-k,0)
ps_count = ps_count.get(ps,0)+1
return res
看到的非常简洁的写法,记录前缀和 和 查找是一起进行的,也就是 ps_count 和 res 一起,这是由于题目需要找的是一个连续子串,很明显当遍历到当前情况下的前缀和如果大于k只需要找之前的是否存在等于ps-k的前缀和,而不是说往后面查找;
这里用的是前缀和,需要注意的就是S[0]需要定义才能保证统一性。
解释:s[left]实际上是a[0]+...+a[left-1] 如果left==0那么会导致失效,只有left>0才成立,这就需要将left==0这种情况单独拿出来讨论,但其实left=0,要计算的子数组是一个前缀(从 a[0] 到 a[right]),所以现在我直接给S[0]==0;
class NumArray:
def __init__(self, nums: List[int]):
s = [0]*(len(nums)+1)
for i, x in enumerate(nums):
s[i+1] = s[i] + x
self.s = s
def sumRange(self, left: int, right: int) -> int:
result = self.s[right+1] - self.s[left]
return result
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
intervals.sort(key=lambda p:p[0])
ans = []
for p in intervals:
if ans and ans[-1][1]>p[0]:
ans[-1][1] = max(ans[-1][1],p[1])
else:
ans.append(p)
return ans