目录
- 第一章 红黑树和AVL树
- 第二章 排序算法
- 第三章 字符串
-
- 1、Leetcode 709 转换成小写字母
- 2、Leetcode 58:最后一个单词的长度
- 3、Leetcode 771:宝石与石头
- 4、剑指offer 50:第一个只出现一次的字符
- 5、Leetcode 8:字符串转换整数
- 6、Leetcode 14:最长公共前缀
- 7、Leetcode 344:反转字符串
- 8、Leetcode 541:反转字符串ii
- 9、Leetcode 151:反转字符串里的单词
- 10、Leetcode 557:反转字符串中的单词III
- 11、Leetcode 917:仅仅反转字母
- 12、Leetcode 242:有效的字母异位词重写
- 13、Leetcode 49:字母异位词分组:
- 14、Leetcode 438:找到字符串中的所有字母异位词
- 15、Leetcode 1143:最长公共子序列重写
- 16、Leetcode 125:验证回文串
- 17、Leetcode 680:验证回文字符串ii
- 18、Leetcode 5:最长回文子串
- 19、Leetcode 72:编辑距离重写
- 20、Leetcode 10:正则表达式匹配
- 21、Leetcode 44:通配符匹配
- 22、Leetcode 115:不同的子序列重写
第一章 红黑树和AVL树
树的旋转(当平衡因子不在-1,1,0中)
红黑树
第二章 排序算法
1、简单的排序
冒泡排序
遍历每个位置的元素,与相邻的数字作比较,不断交换位置。
def main(nums):
n=len(nums)
for i in range(n):
for j in range(0,n-i-1):
if nums[j]>nums[j+1]:
nums[j],nums[j+1]=nums[j+1],nums[j]
print(nums)
选择排序
每次遍历都找到当前最小值的下标,按照顺序依次放到列表对应位置
def main(nums):
n=len(nums)
for i in range(n):
cur_min = i
for j in range(i+1,n):
if nums[j]<nums[cur_min]:
cur_min=j
nums[i],nums[cur_min]=nums[cur_min],nums[i]
print(nums)
插入排序
def main(nums):
n=len(nums)
for i in range(1,n):
j=i
while j>0:
if nums[j]<nums[j-1]:
nums[j],nums[j-1]=nums[j-1],nums[j]
j-=1
else:
break
print(nums)
希尔排序
def main(list):
n=len(list)
gap=n//2
while gap>0:
for i in range(gap,n):
j=i
while j>=gap:
if list[j-gap]>list[j]:
list[j],list[j-gap]=list[j-gap],list[j]
j-=gap
else:
break
gap//=2
return list
2、高级排序
快速排序
def quick_sort(nums,left,right):
if left>=right:
return
mid=nums[left]
low=left
high=right
while low<high:
while nums[high]>=mid and low<high:
high-=1
nums[low]=nums[high]
while nums[low]<=mid and low < high:
low+=1
nums[high]=nums[low]
nums[low]=mid
quick_sort(nums,left,low)
quick_sort(nums,low+1,right)
归并排序
def merge_sort(list):
n=len(list)
mid=n//2
if n<=1:
return list
left_list=merge_sort(list[:mid])
right_list=merge_sort(list[mid:])
left_cur=0
right_cur=0
result=[]
while left_cur<len(left_list) and right_cur<len(right_list):
if left_list[left_cur]<right_list[right_cur]:
result.append(left_list[left_cur])
left_cur+=1
else:
result.append(right_list[right_cur])
right_cur+=1
result.extend(left_list[left_cur:])
result.extend(right_list[right_cur:])
return result
3、Leetcode 1122:数组的相对排序
题目描述
解题思路
将arr1中的值和对应出现的位置存储在字典中,建立一个cmp函数如果当前x在rank中返回0,rank[i],如果不在返回(1,x),根据这个key对arr1进行排序
代码实现
class Solution:
def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
def cmp(x):
return (0,rank[x]) if x in rank else(1,x)
rank={
k:i for i,k in enumerate(arr2)}
arr1.sort(key=cmp)
return arr1
4、Leetcode 242:有效的字母异位词
之前使用的是哈希表来存储s和t,这里使用排序对列表化字符串进行排序然后再变成字符串进行比较。
代码实现
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
def str2list(words):
li=list(words)
li.sort()
return "".join(li)
return str2list(s)==str2list(t)
5、Leetcode 1244:力扣排行榜
题目描述
解题思路
这道题其实没什么难度,主要是返回topk的总数,这里的做法参考了题解,即把字典中的value拿出来排序存储于列表,再统计k个的组合。也可以直接对字典进行排序然后遍历k次。感觉应该都差不多。如果用heap直接压堆可能会更快一点。
代码实现
class Leaderboard:
def __init__(self):
#建立一个字典
self.dic=collections.defaultdict(int)
def addScore(self, playerId: int, score: int) -> None:
self.dic[playerId]+=score
def top(self, K: int) -> int:
#先要根据value进行排序
dic1=sorted([v for v in self.dic.values()],reverse=True)
return sum(dic1[:K])
def reset(self, playerId: int) -> None:
self.dic[playerId]=0
6、Leetcode 56:合并区间
题目描述
解题思路
首先将区间根据区间起始位置进行排序,然后遍历每个区间,维护一个列表,如果当前列表没有或者当前区间的起始位置大于列表中最后一个区间的终止位置,那么就无需合并,直接添加。如果当前区间的起始位置小于了上个区间的终止位置,需要合并。将列表最后一个区间的终止位置改成当前区间的终止位置。
代码实现
class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
#先根据起始位置进行排序
intervals.sort(key=lambda x:x[0])
res=[]
#遍历每个区间
for interval in intervals:
#如果当前已经加入res的区间的结束位置小于当前interval的起始位置,直接添加即可
if not res or res[-1][-1]<interval[0]:
res.append(interval)
#如果当前interval的起始值小于merge中存储interval的终止位置,那要将它们合并
else:
res[-1][-1]=max(res[-1