力扣算法题简单(四)

    1. 删除排序链表中的重复元素
    public ListNode DeleteDuplicates(ListNode head)
    {
        if (head == null) return head;

        ListNode current = head;
        while (current.next != null)
        {
            if (current.val == current.next.val)
            {
                current.next = current.next.next;
            }
            else
            {
                current = current.next;
            }
        }

        return head;
    }
    1. 移除链表元素
    public ListNode RemoveElements(ListNode head, int val)
    {
        ListNode header = new ListNode(-1);
        header.next = head;
        ListNode cur = header;

        while (cur.next != null)
        {
            if (cur.next.val == val)
            {
                cur.next = cur.next.next;
            }
            else
            {
                cur = cur.next;
            }
        }
        return header.next;
    }
    1. 比较含退格的字符串
public bool BackspaceCompare(string S, string T)
    {
        Stack<char> ss = new Stack<char>();
        Stack<char> ts = new Stack<char>();
        for (int i = 0; i < S.Length; i++)
        {
            if (S[i] != '#')
            {
                ss.Push(S[i]);
            }
            else if (ss.Any())
            {
                ss.Pop();
            }
        }

        for (int i = 0; i < T.Length; i++)
        {
            if (T[i] != '#')
            {
                ts.Push(T[i]);
            }
            else if (ts.Any())
            {
                ts.Pop();
            }
        }
		//LINQ队列的比较一行代码解我忧愁
        return ss.SequenceEqual(ts);
    }
    1. 二叉树的层次遍历 II
    public IList<IList<int>> LevelOrderBottom(TreeNode root)
    {
        List<IList<int>> Res = new List<IList<int>>();

        Queue<TreeNode> queue = new Queue<TreeNode>();
        if (root == null) return Res;

        TreeNode left = null;
        TreeNode right = null;
        TreeNode temp = null;
        queue.Enqueue(root);
        int size = 0;
        while (true)
        {
            size = queue.Count;
            List<int> inner = new List<int>();
            while (size != 0)
            {
                temp = queue.Dequeue();
                inner.Add(temp.val);
                size--;
                left = temp.left;
                right = temp.right;
                if (left != null) queue.Enqueue(left);
                if (right != null) queue.Enqueue(right);
            }
            Res.Add(inner);
            if (queue.Count == 0)
            {
                break;
            }
        }

        Res.Reverse();
        return Res;
    }
    1. N叉树的层序遍历
 public IList<IList<int>> LevelOrder(Node root)
    {
        List<IList<int>> Res = new List<IList<int>>();
        if (root == null) return Res;

        Queue<Node> q = new Queue<Node>();
        q.Enqueue(root);
        int size = 0;
        Node temp = null;

        while (true)
        {
            IList<int> templist = new List<int>();
            size = q.Count;
            while (size != 0)
            {
                temp = q.Dequeue();
                templist.Add(temp.val);

                size--;
                if (temp.children != null)
                {
                    for (int i = 0; i < temp.children.Count; i++)
                    {
                        q.Enqueue(temp.children[i]);
                    }
                }
            }
            Res.Add(templist);
            if (q.Count == 0)
            {
                break;
            }
        }
        return Res;
    }
    1. 二叉树的层平均值
        List<IList<int>> Res = new List<IList<int>>();
        List<double> Res2 = new List<double>();
        Queue<TreeNode> queue = new Queue<TreeNode>();
        if (root == null) return Res2;

        TreeNode left = null;
        TreeNode right = null;
        TreeNode temp = null;
        queue.Enqueue(root);
        int size = 0;
        while (true)
        {
            size = queue.Count;
            List<int> inner = new List<int>();
            while (size != 0)
            {
                temp = queue.Dequeue();
                inner.Add(temp.val);
                size--;
                left = temp.left;
                right = temp.right;
                if (left != null) queue.Enqueue(left);
                if (right != null) queue.Enqueue(right);
            }
            Res.Add(inner);
            if (queue.Count == 0)
            {
                break;
            }
        }

        foreach (var item in Res)
        {
            double sum = 0;
            double tempDouble = 0;
            foreach (var item2 in item)
            {
                sum += (double)item2;
            }
            if (item.Count == 0)
            {
                tempDouble = 0;
            }
            else
            {
                tempDouble = sum / item.Count;
            }
            Res2.Add(tempDouble);
        }

        return Res2;
    1. 二叉搜索树的最小绝对差 , 783. 二叉搜索树结点最小距离
    public List<int> Go(TreeNode root)
    {
        List<int> list = new List<int>();
        if (root == null) return list;

        list.Add(root.val);
        list.AddRange(Go(root?.left));
        list.AddRange(Go(root?.right));

        return list;
    }

    public int GetMinimumDifference(TreeNode root)
    {
        if (root == null) return -1;
        var list = Go(root);
        int res = int.MaxValue;
        for (int i = 0; i < list.Count; i++)
        {
            for (int j = i + 1; j < list.Count; j++)
            {
                int temp = Math.Abs(list[i] - list[j]);
                res = Math.Min(res, temp);
            }
        }
        return res;
    }
    1. 相同的树
    public List<object> Go(TreeNode root)
    {
        List<object> list = new List<object>();
        if (root == null)
        {
            list.Add(root);
            return list;
        }

        list.Add(root.val);
        list.AddRange(Go(root?.left));
        list.AddRange(Go(root?.right));

        return list;
    }

    public bool IsSameTree(TreeNode p, TreeNode q)
    {
        var a = Go(p);
        var b = Go(q);
        return a.SequenceEqual(b);
    }
    1. 移除元素
    public int RemoveElement(int[] nums, int val)
    {
        int index = 0;

        for (int i = 0; i < nums.Length; i++)
        {
            if (nums[i] != val)
            {
                nums[index++] = nums[i];
            }
        }

        return index;
    }
    1. 缺失数字
    public int MissingNumber(int[] nums)
    {
        int sum = 0;
        for (int i = 1; i <= nums.Length; i++)
        {
            sum += i;
        }
        for (int i = 0; i < nums.Length; i++)
        {
            sum -= nums[i];
        }
        return sum;
    }
    1. 移动零
    public void MoveZeroes(int[] nums)
    {
        int j = 0;
        int k = 0;
        for (int i = 0; i < nums.Length; i++)
        {
            if (nums[i] == 0)
            {
                k++;
            }
            else
            {
                nums[j++] = nums[i];
            }
        }

        for (int i = 0; i < nums.Length; i++)
        {
            if (i >= (nums.Length - k))
            {
                nums[i] = 0;
            }
        }
    }

    1. 车的可用捕获量
public int NumRookCaptures(char[][] board)
    {
        int curX = 0, curY = 0;
        int Res = 0;
        for (int i = 0; i < board.Length; i++)
        {
            for (int j = 0; j < board[0].Length; j++)
            {
                if (board[i][j] == 'R')
                {
                    curX = i;
                    curY = j;
                }
            }
        }
        //上方向遍历

        for (int i = curX - 1; i >= 0; i--)
        {
            char c = board[i][curY];
            if (c == 'B')
            {
                break;
            }
            else if (c == '.')
            {
                continue;
            }
            else if (c == 'p')
            {
                Res++;
                break;
            }
        }

        for (int i = curX + 1; i < 8; i++)
        {
            char c = board[i][curY];
            if (c == 'B')
            {
                break;
            }
            else if (c == '.')
            {
                continue;
            }
            else if (c == 'p')
            {
                Res++;
                break;
            }
        }

        for (int i = curY + 1; i < 8; i++)
        {
            char c = board[curX][i];
            if (c == 'B')
            {
                break;
            }
            else if (c == '.')
            {
                continue;
            }
            else if (c == 'p')
            {
                Res++;
                break;
            }
        }

        for (int i = curY - 1; i >= 0; i--)
        {
            char c = board[curX][i];
            if (c == 'B')
            {
                break;
            }
            else if (c == '.')
            {
                continue;
            }
            else if (c == 'p')
            {
                Res++;
                break;
            }
        }

        return Res;
    }
    1. 岛屿的周长
  public int IslandPerimeter(int[,] grid)
    {
        int count = 0;
        for (int i = 0; i <= grid.GetUpperBound(0); i++)
        {

            for (int j = 0; j <= grid.GetUpperBound(1); j++)
            {
                if (grid[i, j] == 1)
                {
                    count++;
                }
            }
        }

        int count2 = 0;

        for (int i = 0; i <= grid.GetUpperBound(0); i++)
        {

            for (int j = 0; j <= grid.GetUpperBound(1); j++)
            {
                if (grid[i, j] == 1)
                {
                    count2 += Jiansuo(i, j, grid);
                }
            }
        }

        return count * 4 - count2;
    }

    public int Jiansuo(int i, int j, int[,] grid)
    {
        int count = 0;
        //上是什么呢
        if (i != 0 && (grid[i - 1, j] == 1))
        {
            count++;
        }
        //下是什么呢
        if (i != grid.GetUpperBound(0) && (grid[i + 1, j] == 1))
        {
            count++;
        }
        //左
        if (j != 0 && (grid[i, j - 1] == 1))
        {
            count++;
        }
        //右边
        if (j != grid.GetUpperBound(1) && (grid[i, j + 1] == 1))
        {
            count++;
        }
        return count;
    }
    1. 修剪二叉搜索树
    public TreeNode TrimBST(TreeNode root, int L, int R)
    {

        if (root == null) return null;

        root.left = TrimBST(root.left, L, R);
        root.right = TrimBST(root.right, L, R);

        if (root.val < L)
        {
            return root.right;
        }

        if (root.val > R)
        {
            return root.left;
        }

        return root;
    }
    1. 重塑矩阵
    private static int[,] MatrixReshape(int[,] nums, int r, int c)
    {
        if (r * c != nums.Length) return nums;
        int k = -1;
        int[,] res = new int[r, c];

        foreach (var item in nums)
        {
            res[++k / c, k % c] = item;
        }

        return res;
    }

    1. 三维形体投影面积
    public int ProjectionArea(int[][] grid)
    {
        int N = grid.Length;
        int res = 0;
        for (int i = 0; i < N; i++)
        {
            int maxRow = 0;
            int maxColum = 0;
            for (int j = 0; j < N; j++)
            {
                if (grid[i][j] > 0)
                {
                    res++;
                }
                maxRow = Math.Max(maxRow, grid[i][j]);
                maxColum = Math.Max(maxColum, grid[j][i]);
            }
            res += maxRow + maxColum;
        }

        return res;
    }
    1. 二进制表示中质数个计算置位
  public int CountPrimeSetBits(int L, int R)
    {
        int[] num = { 2, 3, 5, 7, 11, 13, 17, 19 };
        int Res = 0;
        for (int i = L; i <= R; i++)
        {
            string k = Convert.ToString(i, 2);
            int kwo = GetStirngInt(k);
            if (num.Contains(kwo))
            {
                Res++;
            }
        }
        return Res;
    }

    public int GetStirngInt(string k)
    {
        int res = 0;
        foreach (var item in k)
        {
            if (item == '1')
            {
                res++;
            }
        }
        return res;
    }
    1. 查找常用字符
    public IList<string> CommonChars(string[] A)
    {
        var k = from p in A
                from cha in p
                where A.All(b => b.Contains(cha))
                select cha;

        var kk = k.ToArray();
        List<string> list = new List<string>();
        Dictionary<char, int> dic = new Dictionary<char, int>();
        foreach (var item in kk)
        {
            if (!dic.ContainsKey(item))
            {
                dic.Add(item, 1);
            }
            else
            {
                dic[item] += 1;
            }
        }
        Dictionary<char, int> dic22 = new Dictionary<char, int>();

        foreach (var key in dic.Keys)
        {
            int MinNumber = int.MaxValue;
            foreach (var item in A)
            {
                int num = 0;

                foreach (var cha in item)
                {
                    if (cha == key)
                    {
                        num++;
                    }
                }
                MinNumber = Math.Min(num, MinNumber);
            }
            dic22.Add(key, MinNumber);
        }

        foreach (var Value in dic22)
        {
            for (int i = 0; i < Value.Value; i++)
            {
                list.Add(Value.Key.ToString());
            }
        }
        return list;
    }
  1. 下一个更大元素 I
    public int[] NextGreaterElement(int[] findNums, int[] nums)
    {
        int[] RES = new int[findNums.Length];
        for (int i = 0; i < RES.Length; i++)
        {
            RES[i] = -1;
        }

        for (int i = 0; i < findNums.Length; i++)
        {
            for (int j = nums.Length - 1; j >= 0; j--)
            {
                if (findNums[i] < nums[j])
                {
                    RES[i] = nums[j];
                }
                else if (findNums[i] == nums[j])
                {
                    break;
                }
            }
        }

        return RES;
    }

    1. 托普利茨矩阵
  public bool IsToeplitzMatrix(int[,] matrix)
    {
        for (int i = 0; i <= matrix.GetUpperBound(0) - 1; i++)
        {
            for (int j = 0; j <= matrix.GetUpperBound(1) - 1; j++)
            {
                if (matrix[i + 1, j + 1] != matrix[i, j])
                {
                    return false;
                }
            }
        }
        return true;
    }

猜你喜欢

转载自blog.csdn.net/qq_37811712/article/details/87976985