牛客网-Leetcode
分类记录牛客网-Leetcode148道的思路和题解,供个人复习算法使用。
标题名:来源 + Tag + 难度 + 题名
_Charison
这个作者很懒,什么都没留下…
展开
-
[牛客网-Leetcode] #哈希 较难 two-sum
两数之和 two-sum题目描述给出一个整数数组,请在数组中找出两个加起来等于目标值的数,你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index2.。注意:下标是从1开始的假设给出的数组中只存在唯一解例如:给出的数组为 {2, 7, 11, 15},目标值为9输出 ndex1=1, index2=2Given an array of integers, find two numbers such that they add u原创 2020-06-29 21:16:12 · 153 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 binary-tree-level-order-traversal-ii
二叉树的层次遍历ii binary-tree-level-order-traversal-ii题目描述给定一个二叉树,返回该二叉树由底层到顶层的层序遍历,(从左向右,从叶子节点到根节点,一层一层的遍历)例如:给定的二叉树是{3,9,20,#,#,15,7},该二叉树由底层到顶层层序遍历的结果是[[15,7],[9,20],[3]]Given a binary tree, return the bottom-up level order traversal of its nodes’原创 2020-06-28 21:03:36 · 95 阅读 · 0 评论 -
[牛客网-Leetcode] #树 较难 binary-tree-zigzag-level-order-traversal
二叉树的Z字形层次遍历 binary-tree-zigzag-level-order-traversal题目描述Given a binary tree, return the zigzag level ordertraversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tre原创 2020-06-27 10:28:26 · 169 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 symmetric-tree
对称树 symmetric-tree题目描述给定一棵二叉树,判断琪是否是自身的镜像(即:是否对称)例如:下面这棵二叉树是对称的下面这棵二叉树不对称。备注:如果你用递归和迭代两种方法解决这个问题的话,可以得到附加分。Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric:原创 2020-06-26 11:31:36 · 101 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 sum-root-to-leaf-numbers
根节点到叶节点路径和 sum-root-to-leaf-numbers题目描述给定一个仅包含数字0-9的二叉树,每一条从根节点到叶子节点的路径都可以用一个数字表示。例如根节点到叶子节点的一条路径是1->2->3,那么这条路径就用123来代替。找出根节点到叶子节点的所有路径表示的数字之和例如:1↵ / ↵ 2 3根节点到叶子节点的路径1->2用数字12代替根节点到叶子节点的路径1->3用数字13代替所以答案为12+13=25Given a binary t原创 2020-06-22 10:55:29 · 121 阅读 · 0 评论 -
[牛客网-Leetcode] #树 较难 minimum-depth-of-binary-tree
二叉树的最小深度 minimum-depth-of-binary-tree题目描述求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.示例输入{1,2原创 2020-06-21 10:54:58 · 120 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 binary-tree-postorder-traversal
二叉树的后序遍历 binary-tree-postorder-traversal题目描述求给定的二叉树的后序遍历。例如:给定的二叉树为{1,#,2,3},1↵ ↵ 2↵ /↵ 3↵返回[3,2,1].备注;用递归来解这道题太没有新意了,可以给出迭代的解法么?Given a binary tree, return the postorder traversal of its nodes’ values.For example:Given binary tree{1,原创 2020-06-21 10:33:41 · 81 阅读 · 0 评论 -
[牛客网-Leetcode] #树 简单 binary-tree-preorder-traversal
二叉树的先序遍历 binary-tree-preorder-traversal题目描述求给定的二叉树的前序遍历。例如:给定的二叉树为{1,#,2,3},1↵ ↵ 2↵ /↵ 3↵返回:[1,2,3].备注;用递归来解这道题太没有新意了,可以给出迭代的解法么?Given a binary tree, return the preorder traversal of its nodes’ values.For example:Given binary tree{1,#原创 2020-06-21 10:12:01 · 89 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 populating-next-right-pointers-in-each-node-ii
填充节点的右兄弟指针ii populating-next-right-pointers-in-each-node-ii题目描述继续思考"Populating Next Right Pointers in Each Node".这道题如果给定的树可以是任意的二叉树呢?你之前的给出的算法还有效吗?注意:你只能使用常量的额外内存空间例如:给出的二叉树如下:1↵ / ↵ 2 3↵ / ↵ 4 5 7调用完你给出的函数之后,这棵树应该变成原创 2020-06-20 11:45:24 · 120 阅读 · 0 评论 -
[牛客网-Leetcode] #树 简单 populating-next-right-pointers-in-each-node
填充节点的右兄弟指针 populating-next-right-pointers-in-each-node题目描述给定一个二叉树struct TreeLinkNode {↵ TreeLinkNode *left;↵ TreeLinkNode *right;↵ TreeLinkNode *next;↵ }填充所有节点的next指针,指向它右兄弟节点。如果没有右兄弟节点,则应该将next指针设置为NULL。初始时,所有的next指针都为NULL注意:你只能使用原创 2020-06-20 11:43:25 · 105 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 construct-binary-tree-from-preorder-and-inorder-traversal
利用前序遍历和中序遍历构建二叉树 construct-binary-tree-from-preorder-and-inorder-traversal题目描述给出一棵树的前序遍历和中序遍历,请构造这颗二叉树注意:可以假设树中不存在重复的节点Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the原创 2020-06-20 10:13:30 · 110 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 path-sum-ii
路径和ii path-sum-ii题目描述给定一个二叉树和一个值sum,请找出所有的根节点到叶子节点的节点值之和等于sum的路径,例如:给出如下的二叉树,sum=22,返回[↵ [5,4,11,2],↵ [5,8,4,5]↵]↵Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.For example:Given the belo原创 2020-06-18 15:50:17 · 130 阅读 · 0 评论 -
[牛客网-Leetcode] #树 中等 path-sum
路径和 path-sum题目描述给定一个二叉树和一个值sum,判断是否有从根节点到叶子节点的节点值之和等于sum的路径,例如:给出如下的二叉树,sum=22,返回true,因为存在一条路径5->4->11->2的节点值之和为22Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path原创 2020-06-18 10:12:01 · 122 阅读 · 0 评论 -
[牛客网-Leetcode] #树 简单 balanced-binary-tree
二叉平衡树 balanced-binary-tree题目描述判断给定的二叉树是否是平衡的在这个问题中,定义平衡二叉树为每个节点的左右两个子树高度差的绝对值不超过1的二叉树Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subt原创 2020-06-17 20:32:35 · 73 阅读 · 0 评论 -
[牛客网-Leetcode] #树 简单 binary-tree-level-order-traversal
二叉树层次遍历 binary-tree-level-order-traversal题目描述给定一个二叉树,返回该二叉树层序遍历的结果,(从左到右,一层一层地遍历)例如:给定的二叉树是{3,9,20,#,#,15,7},3↵ / ↵ 9 20↵ / ↵ 15 7该二叉树层序遍历的结果是[↵ [3],↵ [9,20],↵ [15,7]↵]如果你不清楚“{1,#,2,3}“的含义的话,请继续阅读我们用如下方法将二叉树序列化:二叉树的序列化遵循层序遍历的原则,”#“原创 2020-06-17 20:08:03 · 82 阅读 · 0 评论 -
[牛客网-Leetcode] #树 简单 binary-tree-inorder-traversal
二叉树中序遍历 binary-tree-inorder-traversal题目描述给出一棵二叉树,返回这棵树的中序遍历例如:给出的二叉树为{1,#,2,3},1↵ ↵ 2↵ /↵ 3↵返回[1,3,2].备注:递归的解法太没有新意了,你能用迭代的方法来解这道题吗?如果你不清楚“{1,#,2,3}“的含义的话,请继续阅读我们用如下方法将二叉树序列化:二叉树的序列化遵循层序遍历的原则,”#“代表该位置是一条路径的终结,下面不再存在结点。例如:1↵ / ↵ 2原创 2020-06-12 09:23:21 · 108 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #DFS #回溯 较难 surrounded-regions
被包围的区域 surrounded-regions题目描述现在有一个仅包含‘X’和‘O’的二维板,请捕获所有的被‘X’包围的区域捕获一个被包围区域的方法是将被包围区域中的所有‘O’变成‘X’例如X X X XX O O XX X O XX O X X执行完你给出的函数以后,这个二维板应该变成:X X X XX X X XX X X XX O X XGiven a 2D board containing’X’and’O’, capture all regions surrounde原创 2020-06-11 10:09:25 · 146 阅读 · 2 评论 -
[牛客网-Leetcode] #查找 #数组 中等 search-a-2d-matrix
已排序的二维矩阵中寻找目标值 search-a-2d-matrix题目描述请写出一个高效的在m*n矩阵中判断目标值是否存在的算法,矩阵具有如下特征:每一行的数字都从左到右排序每一行的第一个数字都比上一行最后一个数字大例如:对于下面的矩阵:[↵ [1, 3, 5, 7],↵ [10, 11, 16, 20],↵ [23, 30, 34, 50]↵]要搜索的目标值为3,返回true;Write an efficient algorithm that searches for a原创 2020-06-11 09:10:08 · 111 阅读 · 0 评论 -
[牛客网-Leetcode] #查找 #数组 中等 longest-consecutive-sequence
最长连续序列 longest-consecutive-sequence题目描述给定一个无序的整数类型数组,求最长的连续元素序列的长度。例如:给出的数组为[100, 4, 200, 1, 3, 2],最长的连续元素序列为[1, 2, 3, 4]. 返回这个序列的长度:4你需要给出时间复杂度在O(n)之内的算法Given an unsorted array of integers, find the length of the longest consecutive elements sequen原创 2020-06-10 16:51:45 · 137 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 中等 merge-sorted-array
合并排序数组 merge-sorted-array题目描述给出两个有序的整数数组A和B,请将数组B合并到数组A中,变成一个有序的数组注意:可以假设A数组有足够的空间存放B数组的元素,A和B中初始的元素数目分别为m和nGiven two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional el原创 2020-06-10 15:20:36 · 104 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 中等 remove-duplicates-from-sorted-array-ii
移除重复数字ii remove-duplicates-from-sorted-array-ii题目描述继续思考题目"Remove Duplicates":给定一个已排序的数组,去除重复元素如果数组中元素最多允许重复两次呢?例如:给出有序数组 A =[1,1,1,2,2,3],你给出的函数应该返回length =5, A 变为[1,1,2,2,3].Follow up for “Remove Duplicates”:What if duplicates are allowed at mos原创 2020-06-10 10:46:51 · 107 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 中等 plus-one
数字加一 plus-one题目描述给出用数字数组表示的一个非负整数,请对该整数加1。Given a non-negative number represented as an array of digits, plus one to the number.示例示例1输入[1]输出[2]示例2输入[1,2,3]输出[1,2,4]解题思路实质上是大整数相加,设置进位标志carry,从低位开始遍历,每次如果出现进位,就修改当前位和进位标志carry。如果遍历完后仍原创 2020-06-08 10:48:25 · 121 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 较难 insert-interval
插入区间 insert-interval题目描述给定一组不重叠的时间区间,在时间区间中插入一个新的时间区间(如果有重叠的话就合并区间)。这些时间区间初始是根据它们的开始时间排序的。示例1:给定时间区间[1,3],[6,9],在这两个时间区间中插入时间区间[2,5],并将它与原有的时间区间合并,变成[1,5],[6,9].示例2:给定时间区间[1,2],[3,5],[6,7],[8,10],[12,16],在这些时间区间中插入时间区间[4,9],并将它与原有的时间区间合并,变成[1,2],[3,原创 2020-06-08 09:37:41 · 113 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 较难 merge-intervals
合并区间 merge-intervals题目描述给出一组区间,请合并所有重叠的区间。例如,给出[1,3],[2,6],[8,10],[15,18],返回[1,6],[8,10],[15,18].Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18], return[1,6],[8,10],[15,18].解题思路首先对inte原创 2020-06-07 11:02:43 · 184 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 较难 spiral-matrix
螺旋矩阵 spiral-matrix题目描述给定一个m x n大小的矩阵(m行,n列),按螺旋的顺序返回矩阵中的所有元素。例如,给出以下矩阵:[↵ [ 1, 2, 3 ],↵ [ 4, 5, 6 ],↵ [ 7, 8, 9 ]↵]你应该返回[1,2,3,6,9,8,7,4,5]。Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For原创 2020-06-07 10:00:17 · 158 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 较难 first-missing-positive
第一个缺失的正整数 first-missing-positive题目描述给出一个无序的整数型数组,求不在给定数组里的最小的正整数例如:给出的数组为[1,2,0] 返回3,给出的数组为[3,4,-1,1] 返回2.你需要给出时间复杂度在O(n)之内并且空间复杂度为常数级的算法Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[原创 2020-06-06 10:47:23 · 197 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 中等 next-permutation
下一个全排列 next-permutation题目描述实现函数next permutation(下一个排列):将排列中的数字重新排列成字典序中的下一个更大的排列。将排列中的数字重新排列成字典序中的下一个更大的排列。如果不存在这样的排列,则将其排列为字典序最小的排列(升序排列)需要使用原地算法来解决这个问题,不能申请额外的内存空间下面有机组样例,左边是输入的数据,右边是输出的答案1,2,3→1,3,23,2,1→1,2,31,1,5→1,5,1Implement next permutati原创 2020-06-06 09:47:01 · 131 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 中等 remove-duplicates-from-sorted-array
移除重复数字 remove-duplicates-from-sorted-array题目描述给定一个已排序的数组,使用就地算法将重复的数字移除,使数组中的每个元素只出现一次,返回新数组的长度。不能为数组分配额外的空间,你必须使用常数级空间复杂度的就地算法。例如,给定输入数组 A=[1,1,2],你给出的函数应该返回length=2,A数组现在是[1,2]。Given a sorted array, remove the duplicates in place such that each el原创 2020-06-05 10:03:40 · 111 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 简单 rotate-image
数组旋转 rotate-image题目描述给出一个用二维矩阵表示的图像返回该图像顺时针旋转90度的结果扩展:你能使用原地算法解决这个问题么?You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?解题思路先沿着右上-左下的对角线翻转,再沿着水平中线翻转。class S原创 2020-06-04 21:06:43 · 86 阅读 · 0 评论 -
[牛客网-Leetcode] #数学 简单 palindrome-number
判断回文数 palindrome-number题目描述在不使用额外的内存空间的条件下判断一个整数是否是回文提示:负整数可以是回文吗?(比如-1)如果你在考虑将数字转化为字符串的话,请注意一下不能使用额外空间的限制你可以将整数翻转。但是,如果你做过题目“Reverse Integer”,你会知道将整数翻转可能会出现溢出的情况,你怎么处理这个问题?这道题有更具普遍性的解法。Determine whether an integer is a palindrome. Do this without原创 2020-06-04 20:40:36 · 108 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #复杂度 简单 reverse-integer
翻转整数 reverse-integer题目描述将给出的整数x翻转。例1:x=123,返回321例2:x=-123,返回-321你有思考过下面的这些问题么?如果整数的最后一位是0,那么输出应该是什么?比如10,100你注意到翻转后的整数可能溢出吗?假设输入是32位整数,则将翻转10000000003就会溢出,你该怎么处理这样的样例?抛出异常?这样做很好,但是如果不允许抛出异常呢?这样的话你必须重新设计函数(比如添加一个额外的参数)。Reverse digits of an integer.原创 2020-06-04 09:41:21 · 137 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #复杂度 入门 single-number
单独的数 single-number题目描述现在有一个整数类型的数组,数组中素只有一个元素只出现一次,其余的元素都出现两次,找出这个只出现一次的数。注意:你需要给出一个线性时间复杂度的算法,你能在不使用额外内存空间的情况下解决这个问题么?Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a lin原创 2020-06-03 22:09:49 · 87 阅读 · 0 评论 -
[牛客网-Leetcode] #树 入门 maximum-depth-of-binary-tree
二叉树的最大深度 maximum-depth-of-binary-tree题目描述求给定二叉树的最大深度,最大深度是指树的根结点到最远叶子结点的最长路径上结点的数量。Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.示例示例1输入原创 2020-06-03 21:49:10 · 80 阅读 · 0 评论 -
[牛客网-Leetcode] #树 入门 same-tree
判断两棵树是否相同 same-tree题目描述给出两个二叉树,请写出一个判断两个二叉树是否相等的函数。判断两个二叉树相等的条件是:两个二叉树的结构相同,并且相同的节点上具有相同的值。Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the node原创 2020-06-03 21:33:30 · 108 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #哈希 #排序 中等 3sum-closet
最接近的三数之和 3sum-closet题目描述给出含有n个整数的数组s,找出s中和加起来的和最接近给定的目标值的三个整数。返回这三个整数的和。你可以假设每个输入都只有唯一解。例如,给定的整数 S = {-1 2 1 -4}, 目标值 = 1.↵↵ 最接近目标值的和为 2. (-1 + 2 + 1 = 2).Given an array S of n integers, find three integers in S such that the sum is closest to a give原创 2020-06-02 21:27:36 · 177 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 简单 spiral-matrix-ii
螺旋矩阵ii spiral-matrix-ii题目描述给定一个整数n,将数字1到n^2n2按螺旋的顺序填入n×n的矩阵例如:给出的n=3,你应该返回如下矩阵:[↵ [ 1, 2, 3 ],↵ [ 8, 9, 4 ],↵ [ 7, 6, 5 ]↵]Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n =3,You原创 2020-06-02 20:45:40 · 135 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #dp 较难 best-time-to-buy-and-sell-stock-iii
最佳买卖股票时机iii best-time-to-buy-and-sell-stock-iii题目描述假设你有一个数组,其中第i个元素是某只股票在第i天的价格。设计一个算法来求最大的利润。你最多可以进行两次交易。注意:你不能同时进行多个交易(即,你必须在再次购买之前出售之前买的股票)。Say you have an array for which the i th element is the price of a given stock on day i.Design an algorith原创 2020-06-01 20:35:16 · 92 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #dp 简单 best-time-to-buy-and-sell-stock-i
最佳买卖股票时机i best-time-to-buy-and-sell-stock-i题目描述假设你有一个数组,其中第i个元素是某只股票在第i天的价格。如果你最多只能完成一笔交易(即买一股和卖一股股票),设计一个算法来求最大利润。Say you have an array for which the i th element is the price of a given stock on day i.If you were only permitted to complete at most o原创 2020-06-01 18:50:54 · 75 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #dp 简单 best-time-to-buy-and-sell-stock-ii
最佳买卖股票时机ii best-time-to-buy-and-sell-stock-ii题目描述假设你有一个数组,其中第i个元素表示某只股票在第i天的价格。设计一个算法来寻找最大的利润。你可以完成任意数量的交易(例如,多次购买和出售股票的一股)。但是,你不能同时进行多个交易(即,你必须在再次购买之前卖出之前买的股票)。Say you have an array for which the ith element is the price of a given stock on day i.Des原创 2020-06-01 17:56:07 · 144 阅读 · 0 评论 -
[牛客网-Leetcode] #数组 #查找 简单 container-with-most-water
盛最多的水 container-with-most-water题目描述给定n个非负整数a1,a2,…,an,其中每个数字表示坐标(i, ai)处的一个点。以(i,ai)和(i,0)(i=1,2,3…n)为端点画出n条直线。你可以从中选择两条线与x轴一起构成一个容器,最大的容器能装多少水?注意:你不能倾斜容器例如:输入 [1,8,6,2,5,4,8,3,7]输出: 49Given n non-negative integers a1 , a2 , …, an , where each repr原创 2020-06-01 15:44:21 · 110 阅读 · 0 评论