随笔分类 - LeetCode
摘要:设变量 sum: 数组的和,n:数组元素个数,tot: 子数组和,cnt:子数组元素个数 通过简单的公式变换可以得到:sum / n = tot / cnt sum / n 的值是确定的,所以也就是需要找到一个子数组的平均值为 sum / n 最简单的方法是通过二进制枚举进行查找,但 n <= 30
阅读全文
摘要:思路 从题目中可以得出,一个表达式是通过 n(n>=1) 个表达式并列、嵌套而成。其实很像前缀表达式。 这样我们很容易想到通过递归的方式来做,递归的边界条件就是 "t" 或者 "f"。 在递归的过程中实际上是不断地缩小问题的规模,直到能够得出问题最终的答案。 在缩小问题规模的时候有两种情况: 嵌套
阅读全文
摘要:规则: () 得一分 AB 得 A + B 分,其中 A 和 B 是平衡括号字符串 (A) 得 2 * A 分,其中 A 是平衡括号字符串 给定一个平衡字符串求最后的分数: 思路: 这种括号类的题目使用栈的一般解法:左括号进栈,遇到右括号出栈 问题的关键是:如何在出栈的时候计算分数! 假设我们在括号
阅读全文
摘要:题目 给定两个大小分别为 m 和 n 的正序(从小到大)数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。 算法的时间复杂度应该为 O(log (m+n)) 。 思路 思路一: 对两个数组进行归并排序,因为两个都是有序的,所以排序的时间复杂度为 O(m + n) 思路二:
阅读全文
摘要:Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linea
阅读全文
摘要:[LeetCode] 73. Set Matrix Zeroes 题目 Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's, and return the mat
阅读全文
摘要:[LeetCode] 42. Trapping Rain Water 题目 Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much w
阅读全文
摘要:[LeetCode] 41. First Missing Positive 题目 Given an unsorted integer array nums, return the smallest missing positive integer. You must implement an alg
阅读全文
摘要:[LeetCode] 71. Simplify Path 题目 Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file
阅读全文
摘要:[LeetCode] 148. Sort List 题目 Given the head of a linked list, return the list after sorting it in ascending order. Example 1: Input: head = [4,2,1,3]
阅读全文
摘要:[LeetCode] 239. Sliding Window Maximum 题目 You are given an array of integers nums, there is a sliding window of size k which is moving from the very l
阅读全文
摘要:[LeetCode] 55. 跳跃游戏 题目 给定一个非负整数数组 nums ,你最初位于数组的 第一个下标 。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 判断你是否能够到达最后一个下标。 示例 1: 输入:nums = [2,3,1,1,4] 输出:true 解释:可以先跳 1 步,从下
阅读全文
摘要:[LeetCode] 395. 至少有 K 个重复字符的最长子串 题目 给你一个字符串 s 和一个整数 k ,请你找出 s 中的最长子串, 要求该子串中的每一字符出现次数都不少于 k 。返回这一子串的长度。 示例 1: 输入:s = "aaabb", k = 3 输出:3 解释:最长子串为 "aaa
阅读全文
摘要:[LeetCode] 61. 旋转链表 题目 Given the head of a linked list, rotate the list to the right by k places. Example 1: Input: head = [1,2,3,4,5], k = 2 Output:
阅读全文
摘要:[LeetCode] 60. 排列序列 题目 The set [1, 2, 3, ..., n] contains a total of n! unique permutations. By listing and labeling all of the permutations in order,
阅读全文
摘要:[LeetCode] 59. 螺旋矩阵 II 题目 Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order. Example 1: Input: n
阅读全文
摘要:LeetCode 刷题 2021/10/15 48. 旋转图像 题目 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。 思路 题目要求不能用辅助数组。 易
阅读全文