自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(61)
  • 收藏
  • 关注

原创 连续子数组的最大乘积

连续子数组最大乘积

2022-06-25 09:55:18 1527 1

原创 BM34 判断是不是二叉搜索树

题目描述判断是否为二叉搜索树解题思路中序遍历,看结果是否单调递增。代码/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */class Solution {public: /** * 代码中的类名、方法名、参数名

2022-05-15 09:24:20 376

原创 BM22 比较版本号

题目描述牛客项目发布项目版本时会有版本号,比如1.02.11,2.14.4等等现在给你2个版本号version1和version2,请你比较他们的大小版本号是由修订号组成,修订号与修订号之间由一个"."连接。1个修订号可能有多位数字组成,修订号可能包含前导0,且是合法的。例如,1.02.11,0.1,0.2都是合法的版本号每个版本号至少包含1个修订号。修订号从左到右编号,下标从0开始,最左边的修订号下标为0,下一个修订号下标为1,以此类推。解题思路模拟比较过程。将两个字符串按照 ‘.’ 分割开

2022-05-09 09:33:44 341

原创 BM19 寻找峰值

题目描述给定一个长度为n的数组nums,请你找到峰值并返回其索引。数组可能包含多个峰值,在这种情况下,返回任何一个所在位置即可。1.峰值元素是指其值严格大于左右相邻值的元素。严格大于即不能有等于2.假设 nums[-1] = nums[n] = -\infty−∞3.对于所有有效的 i 都有 nums[i] != nums[i + 1]4.你可以使用O(logN)的时间复杂度实现此问题吗?解题思路用二分法求解峰值问题。对于每一个位置,判断其两边的元素于他的大小。在比他大的那一侧一定存在峰值。

2022-05-06 15:01:53 271

原创 BM11 链表相加(二)

题目描述链表加法。解题思路模拟加法过程,先将两个链表进行 reverse,然后进行逐个相加。代码/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: /** *

2022-05-04 09:15:54 572

原创 BM12 单链表的排序

题目描述列表排序,时间复杂度 O(nlogn)。解题思路看到列表排序和时间复杂度,直接归并。代码/** * struct ListNode { * int val; * struct ListNode *next; * }; */class Solution {public: /** * * @param head ListNode类 the head node * @return ListNode类 */ ListNode

2022-05-03 09:06:16 482

原创 BM9 删除链表的倒数第n个节点

题目描述删除倒数第 n 个节点。解题思路双指针。代码/** * struct ListNode { * int val; * struct ListNode *next; * }; */class Solution {public: /** * * @param head ListNode类 * @param n int整型 * @return ListNode类 */ ListNode* removeNthF

2022-04-27 09:43:17 1624

原创 BM8 链表中倒数最后k个结点

题目描述输入一个长度为 n 的链表,设链表中的元素的值为 ai ,返回该链表中倒数第k个节点。如果该链表长度小于k,请返回一个长度为 0 的链表。解题思路双指针。slow 指针和 fast 指针保持k的间距,当 fast 为 null 时slow即为答案。代码/** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */c

2022-04-26 09:16:36 1527

原创 BM3 链表中的节点每k个一组翻转

题目描述将给出的链表中的节点每 k 个一组翻转,返回翻转后的链表如果链表中的节点数不是 k 的倍数,将最后剩下的节点保持原样你不能更改节点中的值,只能更改节点本身。解题思路分段反转,边界处理。关键步骤写在注释了哈。代码/** * struct ListNode { * int val; * struct ListNode *next; * }; */class Solution {public: /** * * @param head ListNo

2022-04-24 15:17:40 461

原创 BM6 判断链表中是否有环

题目描述判断列表是否有环。结题思路计数法,根据输入规模设置最大值。代码/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head

2022-04-23 16:26:33 929

原创 BM2 链表内指定区间反转

题目描述将一个节点数为 size 链表 m 位置到 n 位置之间的区间反转,要求时间复杂度 O(n)O(n),空间复杂度 O(1)O(1)。解题思路找到开始反转的位置,然后调用反转列表。记住,因为调用反转列表没有考虑头和尾的问题,还需要将头尾单独处理一下,看代码。代码/** * struct ListNode { * int val; * struct ListNode *next; * }; */class Solution {public: /** *

2022-04-23 09:11:36 447

原创 牛客 BM1 反转链表

题目描述反转一个链表解题思路模板题,记住套路即可。代码/*struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }};*/#include<cstdlib>class Solution {public: ListNode* ReverseList(ListNode* pHead) { ListNode* pre =

2022-04-22 09:29:16 967

原创 LeetCode 503. Next Greater Element II

题目描述Given a circular integer array nums (i.e., the next element of nums[nums.length - 1] is nums[0]), return the next greater number for every element in nums.The next greater number of a number x is the first greater number to its traversing-order next

2022-04-17 15:07:20 168

原创 LeetCode 209. Minimum Size Subarray Sum

题目描述Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, …, numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0

2022-04-16 19:12:00 249

原创 LeetCode 496. Next Greater Element I

题目描述The next greater element of some element x in an array is the first greater element that is to the right of x in the same array.You are given two distinct 0-indexed integer arrays nums1 and nums2, where nums1 is a subset of nums2.For each 0 <= i

2022-04-15 18:49:36 236

原创 LeetCode 508. Most Frequent Subtree Sum

题目描述Given the root of a binary tree, return the most frequent subtree sum. If there is a tie, return all the values with the highest frequency in any order.The subtree sum of a node is defined as the sum of all the node values formed by the subtree roote

2022-04-15 09:06:04 356

原创 LeetCode 279. Perfect Squares

题目描述Given an integer n, return the least number of perfect square numbers that sum to n.A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfe

2022-04-09 08:57:14 133

原创 LeetCode 338. Counting Bits

题目描述Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1’s in the binary representation of i.解题思路看到时间复杂度限制,首先想到DP。难点在于如何找到递推式子。对于一个二进制的数字比如,1001,其实我们可以看出,如果把最高位的 1 去掉,那么其就变成 001 (也就是

2022-04-07 16:12:14 150

原创 LeetCode 198. House Robber

题目描述You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will autom

2022-04-06 17:01:14 149

原创 LeetCode 153. Find Minimum in Rotated Sorted Array

题目描述Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:[4,5,6,7,0,1,2] if it was rotated 4 times.[0,1,2,4,5,6,7] if it was rotated 7 times.Notice that rota

2022-04-04 09:24:46 228

原创 LeetCode 763. Partition Labels

题目描述You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.Note that the partition is done so that after concatenating all the parts in order, the resultant string should b

2022-04-02 15:09:19 103

原创 LeetCode 409. Longest Palindrome

题目描述Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters.Letters are case sensitive, for example, “Aa” is not considered a palindrome here.解题思路这道题的解题关键是不要把它想

2022-03-30 08:44:44 4776

原创 LeetCode 179. Largest Number

题目描述Given a list of non-negative integers nums, arrange them such that they form the largest number and return it.Since the result may be very large, so you need to return a string instead of an integer.解题思路贪心思想。两两比较选取最大的一个,我是采取的冒泡排序来解这一道题,抛砖引玉吧算是。代码

2022-03-29 09:06:00 237

原创 LeetCode 120. Triangle

题目描述Given a triangle array, return the minimum path sum from top to bottom.For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the n

2022-03-28 10:06:43 121

原创 LeetCode 236. Lowest Common Ancestor of a Binary Tree

题目描述Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q

2022-03-12 17:26:01 404

原创 76. Minimum Window Substring

题目描述Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string “”.The testcas

2022-03-06 09:36:07 209

原创 LeetCode 129. Sum Root to Leaf Numbers

题目描述You are given the root of a binary tree containing digits from 0 to 9 only.Each root-to-leaf path in the tree represents a number.For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123.Return the total sum of all root-to-lea

2022-02-24 09:10:40 8235

原创 LeetCode 404. Sum of Left Leaves

题目描述将所有的左叶子节点相加,返回其加和。代码# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def sumOfLeftLeav

2022-02-23 21:30:58 75

原创 LeetCode 114. Flatten Binary Tree to Linked List

题目描述将二叉树转换成列表解题思路递归拆分。对于一个根节点 root,其左子树的列表一定在右子树列表上方,故若知道 root 的左右子成分,那么最后的结果也就出来了。代码# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left#

2022-02-22 10:11:49 239

原创 LeetCode 108. Convert Sorted Array to Binary Search Tree

题目描述将有序列表转化为平衡二叉搜索树思路任务是构建树,第一想到就是递归创建。然后,平衡二叉搜索树,因为拿到的列表是有序的 (无序排一下序即可),列表的中间位置就是 root,左子树与右子树节点数近乎相同,故满足平衡性质。代码# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val#

2022-02-19 22:56:38 8174

原创 LeetCode 98. Validate Binary Search Tree

题目描述判断二叉搜索树是否有效。解题思路题目分析应该使用递归解法。然需要考虑一个问题,在判断子树的有效性时,不要忘记其父亲节点的性质,即应该给出一个区间约束。代码# Definition for a binary tree node.import sysclass TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left

2022-02-10 10:39:11 182

原创 96. Unique Binary Search Trees

题目描述Given an integer n, return the number of structurally unique BST’s (binary search trees) which has exactly n nodes of unique values from 1 to n.解题思路动态规划。找动态规划方程,我们知道当加入第 i 个节点时,他的位置只能为:(左上) -> i -> (左下)。那么这道题目就很简单了,左上和右下的数目都是已知的。代码class Solu

2022-01-31 16:54:47 832

原创 LeetCode 64. Minimum Path Sum

题目描述Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path.Note: You can only move either down or right at any point in time.结题思路动态规划。找到最优子结构,即因为当前的最优值取决于上一步

2022-01-23 14:17:50 100

原创 LeetCode 61. Rotate List

题目描述Given the head of a linked list, rotate the list to the right by k places.解题思路题目思路很简单。但是注意输入规模,输入规模将其变成了一道找规律的题目。规模为 n 的链表,其反转周期也为n。代码# Definition for singly-linked list.class ListNode: def __init__(self, val=0, next=None): self.val =

2022-01-11 10:49:34 58

原创 LeetCode 62. Unique Paths

题目描述There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in ti

2022-01-10 09:26:56 275

原创 LeetCode 56. Merge Intervals

题目描述Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.解题思路按照 start 将intervals 排序,从头开始,两两合并,遇到不可并,保存当前结果。代码cl

2022-01-09 10:16:12 3240

原创 LeetCode 55. Jump Game

题目描述You are given an integer array nums. You are initially positioned at the array’s first index, and each element in the array represents your maximum jump length at that position.Return true if you can reach the last index, or false otherwise.解题思路请移步

2022-01-08 15:01:24 2486

原创 LeetCode 54. Spiral Matrix

题目描述Given an m x n matrix, return all elements of the matrix in spiral order.结题思路解题思路如图,此题本质上是一个循环问题,找出循环关系即可解出。每次循环圈的维度减少 2,看代码。代码class Solution: def spiralOrder(self, matrix: List[List[int]]) -> List[int]: ans = [] cnt = 0

2022-01-07 11:01:00 177

原创 LeetCode 51. N-Queens

题目描述The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle. You may return the answer in any order.Each solution co

2022-01-06 10:11:48 2722

原创 LeetCode 46. Permutations

题目描述Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.1 <= nums.length <= 6-10 <= nums[i] <= 10All the integers of nums are unique.代码class Solution: def permute(self

2022-01-02 09:31:48 655

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除