mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@ -165,20 +165,17 @@ class Solution:
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
func removeNthFromEnd(head *ListNode, n int) *ListNode {
|
||||||
dummyHead := &ListNode{}
|
dummyNode := &ListNode{0, head}
|
||||||
dummyHead.Next = head
|
fast, slow := dummyNode, dummyNode
|
||||||
cur := head
|
for i := 0; i <= n; i++ { // 注意<=,否则快指针为空时,慢指针正好在倒数第n个上面
|
||||||
prev := dummyHead
|
fast = fast.Next
|
||||||
i := 1
|
|
||||||
for cur != nil {
|
|
||||||
cur = cur.Next
|
|
||||||
if i > n {
|
|
||||||
prev = prev.Next
|
|
||||||
}
|
}
|
||||||
i++
|
for fast != nil {
|
||||||
|
fast = fast.Next
|
||||||
|
slow = slow.Next
|
||||||
}
|
}
|
||||||
prev.Next = prev.Next.Next
|
slow.Next = slow.Next.Next
|
||||||
return dummyHead.Next
|
return dummyNode.Next
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -224,6 +224,27 @@ class Solution:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
``` python 3
|
||||||
|
# 相向双指针法
|
||||||
|
# 时间复杂度 O(n)
|
||||||
|
# 空间复杂度 O(1)
|
||||||
|
class Solution:
|
||||||
|
def removeElement(self, nums: List[int], val: int) -> int:
|
||||||
|
n = len(nums)
|
||||||
|
left, right = 0, n - 1
|
||||||
|
while left <= right:
|
||||||
|
while left <= right and nums[left] != val:
|
||||||
|
left += 1
|
||||||
|
while left <= right and nums[right] == val:
|
||||||
|
right -= 1
|
||||||
|
if left < right:
|
||||||
|
nums[left] = nums[right]
|
||||||
|
left += 1
|
||||||
|
right -= 1
|
||||||
|
return left
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -165,7 +165,21 @@ class climbStairs{
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python3:
|
### Python3:
|
||||||
|
```python3
|
||||||
|
def climbing_stairs(n,m):
|
||||||
|
dp = [0]*(n+1) # 背包总容量
|
||||||
|
dp[0] = 1
|
||||||
|
# 排列题,注意循环顺序,背包在外物品在内
|
||||||
|
for j in range(1,n+1):
|
||||||
|
for i in range(1,m+1):
|
||||||
|
if j>=i:
|
||||||
|
dp[j] += dp[j-i] # 这里i就是重量而非index
|
||||||
|
return dp[n]
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
n,m = list(map(int,input().split(' ')))
|
||||||
|
print(climbing_stairs(n,m))
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
@ -419,33 +419,6 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
回溯 使用used数组
|
|
||||||
|
|
||||||
```python
|
|
||||||
class Solution:
|
|
||||||
def findItinerary(self, tickets: List[List[str]]) -> List[str]:
|
|
||||||
tickets.sort() # 先排序,这样一旦找到第一个可行路径,一定是字母排序最小的
|
|
||||||
used = [0] * len(tickets)
|
|
||||||
path = ['JFK']
|
|
||||||
results = []
|
|
||||||
self.backtracking(tickets, used, path, 'JFK', results)
|
|
||||||
return results[0]
|
|
||||||
|
|
||||||
def backtracking(self, tickets, used, path, cur, results):
|
|
||||||
if len(path) == len(tickets) + 1: # 终止条件:路径长度等于机票数量+1
|
|
||||||
results.append(path[:]) # 将当前路径添加到结果列表
|
|
||||||
return True
|
|
||||||
|
|
||||||
for i, ticket in enumerate(tickets): # 遍历机票列表
|
|
||||||
if ticket[0] == cur and used[i] == 0: # 找到起始机场为cur且未使用过的机票
|
|
||||||
used[i] = 1 # 标记该机票为已使用
|
|
||||||
path.append(ticket[1]) # 将到达机场添加到路径中
|
|
||||||
state = self.backtracking(tickets, used, path, ticket[1], results) # 递归搜索
|
|
||||||
path.pop() # 回溯,移除最后添加的到达机场
|
|
||||||
used[i] = 0 # 标记该机票为未使用
|
|
||||||
if state:
|
|
||||||
return True # 只要找到一个可行路径就返回,不继续搜索
|
|
||||||
|
|
||||||
```
|
```
|
||||||
回溯 使用字典
|
回溯 使用字典
|
||||||
```python
|
```python
|
||||||
|
Reference in New Issue
Block a user