算法进修Day-31

算法进修Day-31

31. 旋转链表

难度:中等
题目要求:
给你一个链表的头节点 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]

题解

最简单的算法,直接用指针遍历,将链表分成两段之后重新拼接

想法代码

public class ListNode
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val; 
        this.next = next;
    }
}

public class Solution
{
    public static void Main(string[] args)
    {
        ListNode head = new ListNode(1)
        {
            next = new ListNode(2)
            {
                next = new ListNode(3)
                {
                    next = new ListNode(4)
                    {
                        next = new ListNode(5)
                    }
                }
            }
        };
        int k = 2;
        Solution solution = new Solution();
        ListNode res = solution.RotateRight(head, k);
        while (res != null)
        {
            Console.Write(res.val + ",");
            res = res.next;
        }
    }

    public ListNode RotateRight(ListNode head, int k)
    {
        if (head == null || k == 0)
        {
            return head;
        }
        ListNode counter = head;
        ListNode node = new ListNode();
        ListNode node1 = new ListNode();
        ListNode s = node;
        ListNode temp = node1;
        int count = 0;
        while (counter != null)
        {
            count++;
            counter = counter.next;
        }

        if (k % count==0)
        {
            return head;
        }
        for (int i = 0; i < (count - (k + count) % count); i++)
        {
            node1.next = new ListNode(head.val);
            node1 = node1.next;
            head = head.next;
        }
        while (head != null)
        {
            node.next = new ListNode(head.val);
            node = node.next;
            head = head.next;
        }

        temp = temp.next;
        node.next = temp;
        return s.next;
    }
}

62. 不同路径

难度:中等
题目要求
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为 “Start” )。

机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为 “Finish” )。

问总共有多少条不同的路径?

示例1

输入:m = 3, n = 7
输出:28

示例2

输入:m = 3, n = 2
输出:3

示例3

输入:m = 7, n = 3
输出:28

示例4

输入:m = 3, n = 3
输出:6

题解

直接使用数学方法,总共 m − 1 m-1 m1 次向下和 n − 1 n-1 n1 次向右,所以,总次数为 m + n − 1 m+n-1 m+n1 次,从 m + n − 2 m+n-2 m+n2 次中挑取向下和向右的次数中较少的次数,进行数学公式 C m + n − 2 n − 1 C^{n-1}_{m+n-2} Cm+n2n1 C m + n − 2 m − 1 C^{m-1}_{m+n-2} Cm+n2m1

想法代码

class Solution
{
    public static void Main(String[] args)
    {
        int m = 3;
        int n = 7;
        Solution solution = new Solution();
        int res = solution.UniquePaths(m,n);
        Console.WriteLine(res);
    }

    public int UniquePaths(int m, int n)
    {
        long res = 1;

        if (m < n)
        {
            int temp = m; 
            m = n; 
            n = temp;
        }

        for (int i = m + n - 2, j = 1; j < n; j++, i--)
        {
            res *= i;
            res /= j;
        }

        return (int)res;
    }
}

你可能感兴趣的:(算法进修,算法,leetcode,c#)