leetcode 61. 旋转链表 62. 不同路径
61.旋转链表
难度中等681收藏分享切换为英文接收动态反馈
给你一个链表的头节点 head
,旋转链表,将链表每个节点向右移动 k
个位置。
示例 1:
输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]
示例 2:
输入:head = [0,1,2], k = 4
输出:[2,0,1]
提示:
- 链表中节点的数目在范围
[0, 500]
内 -100 <= Node.val <= 100
0 <= k <= 2 * 109
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Author : mtl
# @Desc : ***
# @File : 61.py
# @Software: PyCharm
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
from typing import Optional
class Solution:
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
print(type(head))
if k == 0 or head is None or head.next is None:
return head
node_list = [head]
n = 1
while head.next:
head = head.next
node_list.append(head)
n += 1
for i in range(k % n):
node_list.insert(0, node_list[-1])
node_list.pop()
node_list[-1].next = None
node_list[0].next = node_list[1]
return node_list[0]
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if k == 0 or head is None or head.next is None:
return head
node_list = [head]
n = 1
while head.next:
head = head.next
node_list.append(head)
n += 1
if k % n == 0:
return node_list[0]
node_list[-1].next = node_list[0]
node_list[n - (k % n) - 1].next = None
return node_list[n - (k % n)]
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if k == 0 or head is None or head.next is None:
return head
start, end, valid = head, None, False
i = 0
def dfs(node: ListNode):
nonlocal k, start, end, valid, i
i += 1
if not node.next:
end = node
k %= i
if k == 0:
valid = True
else:
dfs(node.next)
if valid:
return
if k > 0:
k -= 1
else:
ls = ListNode(node.val, node.next)
node.next = None
end.next = start
start = ls.next
valid = True
dfs(head)
return start
62. 不同路径
难度中等1207收藏分享切换为英文接收动态反馈
一个机器人位于一个 m x n
网格的左上角 (起始点在下图中标记为 “Start” )。
机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。
问总共有多少条不同的路径?
示例 1:
输入:m = 3, n = 7
输出:28
示例 2:
输入:m = 3, n = 2
输出:3
解释:
从左上角开始,总共有 3 条路径可以到达右下角。
1. 向右 -> 向下 -> 向下
2. 向下 -> 向下 -> 向右
3. 向下 -> 向右 -> 向下
示例 3:
输入:m = 7, n = 3
输出:28
示例 4:
输入:m = 3, n = 3
输出:6
提示:
1 <= m, n <= 100
- 题目数据保证答案小于等于
2 * 109
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# @Author : mtl
# @Desc : ***
# @File : 62.py
# @Software: PyCharm
class Solution:
def uniquePaths(self, m: int, n: int) -> int:
directions = [[0,1], [1,0]]
ans = 0
def dfs(pos_x, pos_y):
nonlocal ans
if pos_y == m - 1 and pos_x == n - 1:
ans += 1
if pos_y >= m or pos_x >= n:
return
for i in range(2):
direction_x, direction_y = directions[i]
dfs(pos_x + direction_x, pos_y + direction_y)
dfs(0, 0)
return ans
def uniquePaths(self, m: int, n: int) -> int:
pos = [[1 for _ in range(n)] for _ in range(m)]
for i in range(1, n):
for j in range(1, m):
pos[j][i] = pos[j - 1][i] + pos[j][i - 1]
return pos[m - 1][n - 1]
def uniquePaths(self, m: int, n: int) -> int:
pos = [1] * n
for i in range(1, m):
for j in range(1, n):
pos[j] += pos[j - 1]
return pos[-1]
if __name__ == '__main__':
m,n = 3, 7
print(Solution().uniquePaths(m, n))