leetcode
lightmare625
这个作者很懒,什么都没留下…
展开
-
第35题:LeetCode138. Copy List with Random Pointer
题目 给定一个链表,每个节点包含一个额外增加的随机指针,该指针可以指向链表中的任何节点或空节点。要求返回这个链表的深度拷贝。 考点1.hash 空间换时间 On^2->On2.双指针,野指针的鲁棒判断思路1.复制节点到原链表后面。copy->link->next 。只复制节点的值和next,不复制随机指针。cloneNode->rando...原创 2019-02-14 23:50:21 · 172 阅读 · 0 评论 -
LeetCode705. Design HashSet
题目 不使用任何内建的哈希表库设计一个哈希集合具体地说,你的设计应该包含以下的功能add(value):向哈希集合中插入一个值。 contains(value) :返回哈希集合中是否存在这个值。 remove(value):将给定值从哈希集合中删除。如果哈希集合中没有这个值,什么也不做。示例:MyHashSet hashSet = new MyHashSet();hash...原创 2019-02-19 00:31:07 · 195 阅读 · 0 评论 -
LeetCode706. Design HashMap
题目 不使用任何内建的哈希表库设计一个哈希映射具体地说,你的设计应该包含以下的功能put(key, value):向哈希映射中插入(键,值)的数值对。如果键对应的值已经存在,更新这个值。 get(key):返回给定的键所对应的值,如果映射中不包含这个键,返回-1。 remove(key):如果映射中存在这个键,删除这个数值对。示例:MyHashMap hashMap = n...原创 2019-02-19 01:07:07 · 93 阅读 · 0 评论 -
LeetCode426.Convert Binary Search Tree to Sorted Doubly Linked List
题目Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list.Let's take the foll...原创 2019-02-28 14:26:51 · 261 阅读 · 0 评论 -
LeetCode997. Find the Town Judge
题目 在一个小镇里,按从 1 到 N 标记了 N 个人。传言称,这些人中有一个是小镇上的秘密法官。如果小镇的法官真的存在,那么:小镇的法官不相信任何人。 每个人(除了小镇法官外)都信任小镇的法官。 只有一个人同时满足属性 1 和属性 2 。给定数组 trust,该数组由信任对 trust[i] = [a, b] 组成,表示标记为 a 的人信任标记为 b 的人。如果小镇存在秘...原创 2019-02-24 18:04:30 · 272 阅读 · 0 评论 -
LeetCode297. Serialize and Deserialize Binary Tree
题目 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。示例: 你可以将以下二...原创 2019-02-24 20:06:28 · 111 阅读 · 0 评论 -
第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。示例:root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8 10 / \ 5 -3 / \ \ 3 2 11 / \ \3 -2 1返回 3。和等于 8 的路径...原创 2019-02-13 23:51:06 · 118 阅读 · 0 评论 -
LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal
题目 根据一棵树的前序遍历与中序遍历构造二叉树。注意:你可以假设树中没有重复的元素。例如,给出前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7]返回如下的二叉树: 3 / \ 9 20 / \ 15 7Tag二分查找。dfs.递归。 代码0x00...原创 2019-02-25 01:15:16 · 102 阅读 · 0 评论 -
Leetcode463. Island Perimeter
题目 给定一个包含 0 和 1 的二维网格地图,其中 1 表示陆地 0 表示水域。网格中的格子水平和垂直方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长...原创 2019-02-19 23:10:18 · 88 阅读 · 0 评论 -
LeetCode567. Permutation in String
Given two stringss1ands2, write a function to return true ifs2contains the permutation ofs1. In other words, one of the first string's permutations is thesubstringof the second string.Exampl...原创 2019-03-01 23:04:04 · 86 阅读 · 0 评论 -
LeetCode46. Permutations
Given a collection ofdistinctintegers, return all possible permutations.Example:Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]法1.回溯法。递归。每次交换num中的两...原创 2019-03-01 23:04:14 · 128 阅读 · 0 评论 -
LeetCode47.Permutations II(剑指offer38-1)
Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[ [1,1,2], [1,2,1], [2,1,1]]法1.递归。swap.回溯。唯一要多做的就是去重。用...原创 2019-03-02 18:00:21 · 661 阅读 · 0 评论 -
LeetCode77. Combinations(剑指offer38-2)
Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.Example:Input:n = 4, k = 2Output:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]回溯法。细节。循环...原创 2019-03-02 20:21:25 · 123 阅读 · 0 评论 -
LeetCode51. N-Queens(剑指offer38-3)
Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Eac...原创 2019-03-03 13:30:44 · 153 阅读 · 0 评论 -
LeetCode295. Find Median from Data Stream(剑指offer41题)
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.For example,[2,3,4], the median is...原创 2019-03-15 00:36:55 · 173 阅读 · 0 评论 -
LeetCode53. Maximum Subarray(剑指offer42题)
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation:...原创 2019-03-15 11:51:18 · 317 阅读 · 0 评论 -
第39题:数组中超过一半的数字Leetcode:169. Majority Element
题目描述数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。法一。快排std::sort.从小到大的数组。O(nlogn).找到有序数组的中位数。计算他的个数。如果大于数组长度的一半就是要找的数字,否则返回0#inclu...原创 2019-03-07 23:52:56 · 666 阅读 · 0 评论 -
LeetCode172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Example 1:Input: 3Output: 0Explanation: 3! = 6, no trailing zero.Example 2:Input: 5Output: 1Explanation: 5! = 120, one trailing ...原创 2019-03-15 19:53:14 · 324 阅读 · 0 评论 -
LeetCode804. Unique Morse Code Words
题目 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 "-...", "c" 对应 "-.-.", 等等。为了方便,所有26个英文字母对应摩尔斯密码表如下:[".-","-...","-.-.","-..",&原创 2019-02-18 22:15:17 · 114 阅读 · 0 评论 -
LeetCode701.Insert into a Binary Search Tree
题目给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回任意有效的结果。例如,给定二叉搜索树: 4 / \ 2 7 / \ 1 3和 ...原创 2019-02-28 10:28:40 · 103 阅读 · 0 评论 -
LeetCode700. Search in a Binary Search Tree
题目给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。例如,给定二叉搜索树: 4 / \ 2 7 / \ 1 3和值: 2你应该返回如下子树: 2 / \ ...原创 2019-02-28 08:55:02 · 83 阅读 · 0 评论 -
第26题:LeetCode572:Subtree of Another Tree另一个树的子树
题目描述给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。示例 1:给定的树 s: 3 / \ 4 5 / \ 1 2给定的树 t: 4 / \ 1 2返回 true,因为 t 与 s...原创 2019-02-07 15:52:52 · 173 阅读 · 0 评论 -
LeetCode111. Minimum Depth of Binary Tree
题目 给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最小深度 2.考点dfs.递归思路代码1.递归/*...原创 2019-02-21 13:27:22 · 86 阅读 · 0 评论 -
LeetCode104.Maximum Depth of Binary Tree
题目 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回它的最大深度 3 。考点dfs 递归思路代码/** * Defi...原创 2019-02-21 13:32:31 · 104 阅读 · 0 评论 -
第27题:Leetcode226: Invert Binary Tree反转二叉树
翻转一棵二叉树。示例:输入: 4 / \ 2 7 / \ / \1 3 6 9输出: 4 / \ 7 2 / \ / \9 6 3 1 思路如果根节点存在,就交换两个子树的根节点,用递归,从下至上。、解一:auto tmp = invertTree(root->...原创 2019-02-07 19:25:47 · 133 阅读 · 0 评论 -
第28题:leetcode101:Symmetric Tree对称的二叉树
给定一个二叉树,检查它是否是镜像对称的。例如,二叉树 [1,2,2,3,4,4,3] 是对称的。 1 / \ 2 2 / \ / \3 4 4 3但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的: 1 / \ 2 2 \ \ 3 3说明:如果你可以运用递归和迭代两种方法解...原创 2019-02-07 20:32:29 · 106 阅读 · 0 评论 -
第29题:LeetCode54:Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。示例 1:输入:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]输出: [1,2,3,6,9,8,7,4,5]示例 2:输入:[ [1, 2, 3, 4], [5, 6, 7, 8], [9,10,11,12]...原创 2019-02-08 12:03:21 · 263 阅读 · 0 评论 -
第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈。push(x) -- 将元素 x 推入栈中。 pop() -- 删除栈顶的元素。 top() -- 获取栈顶元素。 getMin() -- 检索栈中的最小元素。示例:MinStack minStack = new MinStack();minStack.push(-2);minStack.p...原创 2019-02-08 13:21:33 · 209 阅读 · 0 评论 -
第31题:LeetCode946. Validate Stack Sequences验证栈的序列
题目给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true;否则,返回 false 。 示例 1:输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]输出:true解释:我们可以按以下顺序执行:push(1), push(2), push(3)...原创 2019-02-08 21:37:13 · 238 阅读 · 0 评论 -
32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树
题目 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回其层次遍历结果:[ [3], [9,20], [15,7]]考点思路代码newcoder/*s...原创 2019-02-09 10:17:34 · 222 阅读 · 0 评论 -
32-3题:LeetCode103. Binary Tree Zigzag Level Order Traversal锯齿形层次遍历/之字形打印二叉树
题目 给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。例如:给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7返回锯齿形层次遍历如下:[ [3], [20,9], [15,7]]考点1....原创 2019-02-09 10:18:26 · 104 阅读 · 0 评论 -
LeetCode94. Binary Tree Inorder Traversal
题目 给定一个二叉树,返回它的中序 遍历。示例:输入: [1,null,2,3] 1 \ 2 / 3输出: [1,3,2]进阶: 递归算法很简单,你可以通过迭代算法完成吗?考点stack递归中序遍历:左-根-右思路ref:二叉树的前中后和层序遍历详细图解(递归和非递归写法)1.递归若根节点为空,返回。左...原创 2019-02-22 00:08:08 · 121 阅读 · 0 评论 -
LeetCode144. Binary Tree Preorder Traversal
题目 给定一个二叉树,返回它的 前序 遍历。 示例:输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3]进阶: 递归算法很简单,你可以通过迭代算法完成吗?考点思路见LeetCode94. Binary Tree Inorder Traversal代码1.递归class Solut...原创 2019-02-22 00:40:05 · 79 阅读 · 0 评论 -
LeetCode145. Binary Tree Postorder Traversal
题目 给定一个二叉树,返回它的 后序 遍历。示例:输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1]进阶: 递归算法很简单,你可以通过迭代算法完成吗?考点思路代码1.递归//递归:左-右-根class Solution {public: vector<int&...原创 2019-02-22 01:15:35 · 102 阅读 · 0 评论 -
LeetCode1. Two Sum
题目 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。示例:给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1]...原创 2019-02-22 23:34:43 · 86 阅读 · 0 评论 -
LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal
题目 根据一棵树的中序遍历与后序遍历构造二叉树。注意:你可以假设树中没有重复的元素。例如,给出中序遍历 inorder = [9,3,15,20,7]后序遍历 postorder = [9,15,7,20,3]返回如下的二叉树: 3 / \ 9 20 / \ 15 7Tagdfs . post+inorder .递归...原创 2019-02-27 15:55:52 · 84 阅读 · 0 评论 -
第34-1题:LeetCode112. Path Sum I
题目 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。说明: 叶子节点是指没有子节点的节点。示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 / / \ 11 13 ...原创 2019-02-13 00:09:09 · 91 阅读 · 0 评论 -
第34-2题:LeetCode113. Path Sum II
题目给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。说明: 叶子节点是指没有子节点的节点。示例:给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 / / \ 11 13 4 /...原创 2019-02-12 23:58:41 · 121 阅读 · 0 评论 -
LeetCode215. Kth Largest Element in an Array
Kth Largest Element in an ArrayFind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:Input: [3,2,1,5...原创 2019-03-12 16:20:03 · 447 阅读 · 0 评论