因为暑期实习找得很不顺利,感觉自身最大的问题体现在刷题量偏少,操作系统,数据库基础不好,所以现在决定写博客来记录整个过程,希望能找到大厂offer,如果不能找到的话也算是为秋招做准备。
剑指offer的题我很早就刷完了,现在主要刷leetcode的题。今天更新的是我刷的数组的题,基本的思路有:
排序
查找
指针碰撞
滑动窗口
因为整个博客更多的给自己看,所以内容上可能可读性不是很好~ 大致格式为
(# leetcode题号,leetcode题目
代码)
#数组问题, 排序,查找,指针对撞,滑动窗口
#75. 三路快排 颜色分类
class Solution:
def sortColors(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
zero = -1
two = len(nums)
index = 0
while index < two:
if nums[index] == 1:
index += 1
elif nums[index] == 0:
zero += 1
nums[index], nums[zero] = nums[zero], nums[index]
index += 1
elif nums[index] == 2:
two -= 1
nums[index], nums[two] = nums[two], nums[index]
#88. 合并两个有序数组
class Solution:
def merge(self, nums1, m, nums2, n):
"""
Do not return anything, modify nums1 in-place instead.
"""
nums1_copy = nums1[:m]
nums1 = []
index1 = 0
index2 = 0
while index1 < m and index2 < n:
if nums1_copy[index1] < nums2[index2]:
nums1.append(nums1_copy[index1])
index1 += 1
else:
nums1.append(nums2[index2])
index2 += 1
if index1 < m:
nums1[index1 + index2:] = nums1_copy[index1:]
if index2 < n:
nums1[index1 + index2:] = nums2[index2:]
return nums1
#215. 利用快速排序,找到第k个最大元素
class Solution:
def quick_sort(self, l, k,start, end):
i = start
j = end
flag = l[start]
while i < j:
while flag <= l[j] and i < j:
j -= 1
l[i] = l[j]
while flag >= l[i] and i < j:
i += 1
l[j] = l[i]
l[i] = flag
if i == len(l)-k:
return l
elif i < len(l)-k:
self.quick_sort(l, k, i+1, end)
else:
self.quick_sort(l, k, start, i-1)
return l
def findKthLargest(self, nums, k):
return self.quick_sort(nums, k, 0, len(nums)-1)
#167. 使数组之和为sum
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
start = 0
end = len(numbers) -1
while start <= end:
if numbers[start] + numbers[end] == target:
return [start,end]
elif numbers[start] + numbers[end] < target:
start += 1
elif numbers[start] + numbers[end] > target:
end -= 1
#125 验证回文串
import re
class Solution:
def isPalindrome(self, s: str) -> bool:
if len(s) == 0:
return True
l = ''.join(re.findall(r'[a-zA-Z0-9]+',s)).lower()
start = 0
end = len(l)-1
while l[start] == l[end]:
start += 1
end -= 1
if start > end:
return True
return False
#344 翻转字符串
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
start = 0
end = len(s) - 1
while start < end:
s[start], s[end] = s[end], s[start]
start += 1
end -= 1
#345 翻转元音字母
class Solution:
def reverseString(self, s):
l = list(s)
start = 0
end = len(s) - 1
l_yuanyin = ['a','e','i','o','u', 'A','E','I','O','U']
while start < end:
while l[start] not in l_yuanyin:
start += 1
if start >= len(l):
break
while l[end] not in l_yuanyin and end > -1:
end -= 1
if end<=-1:
break
if start < end:
l[start], l[end] = l[end], l[start]
start += 1
end -= 1
s = ''.join(l)
return s
so = Solution()
print(so.reverseString(".,"))
#11
class Solution:
def maxArea(self, height):
left = 0
right = len(height)-1
result = (right - left) * min(height[right], height[left])
while left < right:
if height[right] > height[left]:
left += 1
else:
right -= 1
result = max(result, (right - left) * min(height[right], height[left]))
return result
#3 最长没有重复字母的子串
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
if len(s) == 0:
return 0
if len(s) == 1:
return 1
l = list(s)
substr = [1]
for i, v in enumerate(l):
if i>0:
tmp = []
for j in range(i, -1, -1):
if l[j] not in tmp:
tmp.append(l[j])
else:
break
substr.append(max(substr[i-1], len(tmp)))
return substr[-1]
#438 找到字符串中所有字母异位词
from collections import Counter
class Solution:
def findAnagrams(self, s, p):
sCounter = Counter(s[0:len(p)-1])
pCounter = Counter(p)
l = []
for i in range(len(p)-1, len(s)):
sCounter[s[i]] += 1
if sCounter == pCounter:
l.append(i-len(p)+1)
sCounter[s[i-len(p)+1]] -= 1
if sCounter[s[i-len(p)+1]] == 0:
del sCounter[s[i-len(p)+1]]
return l
#76最小覆盖子串
from collections import Counter
from collections import defaultdict
class Solution:
def issubstr(self, scounter, tcounter):
for i in tcounter:
if i in scounter and tcounter[i] <= scounter[i]:
continue
else:
return False
return True
def minWindow(self, s: str, t: str) -> str:
start = 0
tCounter = Counter(t)
res = ''
scounter = defaultdict(int)
for i in range(len(s)):
if s[i] in tCounter:
scounter[s[i]] += 1
print(i, s[i], s[start:i+1])
while start < len(s) and self.issubstr(scounter, tCounter):
## 这里需要重新写
print(start, res, s[start:i+1])
if res == '':
res = s[start:i+1]
else:
if len(s[start:i+1]) < len(res):
res = s[start:i+1]
if s[start] in tCounter:
scounter[s[start]] -= 1
start += 1
print(s[start:i+1], scounter)
return res
so = Solution()
print(so.minWindow("ADOBECODEBANC" , "ABC"))