leetcode 61. 旋转链表 62. 不同路径

leetcode 61. 旋转链表 62. 不同路径

61.旋转链表

难度中等681收藏分享切换为英文接收动态反馈

给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

示例 1:

img

输入:head = [1,2,3,4,5], k = 2
输出:[4,5,1,2,3]

示例 2:

img

输入: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:

img

输入: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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

matianlongg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值