刷题记录合辑

标题不含题号,题号写在正文与 description 中。


228. 汇总区间

思路
尝试1-双指针: 通过

  • 左指针固定右指针走,右指针走不动就做指针跳到右指针下一个
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution:
def summaryRanges(self, nums: List[int]) -> List[str]:
left, right = 0,0
size = len(nums)
res = []
while left < size and right < size:
if right == size -1 or nums[right+1] != nums[right]+1 :
if left == right:
res.append(f"{nums[left]}")
else:
res.append(f"{nums[left]}->{nums[right]}")
left = right + 1
right = left
else:
right = right + 1
return res


77. 组合

思路
尝试1-dfs: 使用dfs进行搜索,选够了就判断是否已经存在

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution:
def combine(self, n: int, k: int) -> List[List[int]]:
res = []
def dfs(path: List[int],x:int) :
if len(path) == k:
res.append(path)
return
else:
for i in range(x,n+1):
path.append(i)
dfs(path, i+1)
path.pop()
s = []
dfs(path, 1)
return res



1
2
3
4
```

<!-- tab 题解 -->
```python

总结


相关题目

*


知识点小结

*


模板

xx. xxx

思路

1
        
1
2
3
4
```

<!-- tab 题解 -->
```python

总结