leetcode

LeetCode All in One 题目讲解汇总(持续更新中...)

时间秒杀一切 提交于 2020-02-26 16:32:20
Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them. For example, Given the tree: 4 / \ 2 7 / \ 1 3 And the value to insert: 5 You can return this binary search tree: 4 / \ 2 7 / \ / 1 3 5 This tree is also valid: 5 / \ 2 7 / \ 1 3 \ 4 这道题让我们在二叉搜索树中插入结点

LeetCode All in One 题目讲解汇总(持续更新中...)

◇◆丶佛笑我妖孽 提交于 2020-02-26 16:29:57
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL. For example, Given the tree: 4 / \ 2 7 / \ 1 3 And the value to search: 2 You should return this subtree: 2 / \ 1 3 In the example above, if we want to search the value 5 , since there is no node with value 5 , we should return NULL . Note that an empty tree is represented by NULL , therefore you would see the expected output (serialized tree format) as [] ,

lc 0226

不打扰是莪最后的温柔 提交于 2020-02-26 11:47:45
目录 ✅ 232. 用栈实现队列 描述 解答 cpp py ✅ 496. 下一个更大元素 I 描述 解答 java py ✅ 232. 用栈实现队列 https://leetcode-cn.com/problems/implement-queue-using-stacks 描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。 示例: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty(); // 返回 false 说明: 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。 来源:力扣(LeetCode) 链接:https://leetcode-cn

五分钟自学编程:怎样才能学好笔试面试最爱考察的算法

醉酒当歌 提交于 2020-02-26 07:34:42
原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者。 本文思维导图 什么是算法 上回我们有一篇文章,讲述了作为一个新人程序员,如何学习数据结构这门课程,其实呢,数据结构和算法是息息相关的,为什么这么说呢,因为数据结构本身只是一个载体,而在数据结构之上产生作用和输出价值的东西其实是算法。 比如数据结构里的数组,看似非常简单的数据结构,却可以支持很多复杂的算法,比如动态规划,比如DFS和BFS,再比如字符串算法、二叉树算法等等。那么算法到底是什么东西呢,不妨让我们来看看官方的介绍。 根据百度百科的介绍,算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制。也就是说,能够对一定规范的输入,在有限时间内获得所要求的输出。一个算法的优劣可以用空间复杂度与时间复杂度来衡量。 其实,算法的本质就是给你一组输入,运算之后给你一组输出,因此,算法帮人们解决很多问题把抽象的问题具体化,把一个问题转化成另外一个问题。 认识算法的N个阶段 我第一次遇到算法题,还是在我考研复习数据结构的时候,那个时候我看到的算法题其实都是很基础的题目,比如把数组中的两个元素置换,把两个链表合并成一个,但对于我来说已经是很有难度的事情了,那时候我连伪代码是什么都还不懂。 第二次认识算法,还是在研究生期间找实习工作的时候

五分钟学编程:怎样才能学好笔试面试最爱考察的算法

删除回忆录丶 提交于 2020-02-26 01:23:13
原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者。 本文思维导图 什么是算法 上回我们有一篇文章,讲述了作为一个新人程序员,如何学习数据结构这门课程,其实呢,数据结构和算法是息息相关的,为什么这么说呢,因为数据结构本身只是一个载体,而在数据结构之上产生作用和输出价值的东西其实是算法。 比如数据结构里的数组,看似非常简单的数据结构,却可以支持很多复杂的算法,比如动态规划,比如DFS和BFS,再比如字符串算法、二叉树算法等等。那么算法到底是什么东西呢,不妨让我们来看看官方的介绍。 根据百度百科的介绍,算法(Algorithm)是指解题方案的准确而完整的描述,是一系列解决问题的清晰指令,算法代表着用系统的方法描述解决问题的策略机制。也就是说,能够对一定规范的输入,在有限时间内获得所要求的输出。一个算法的优劣可以用空间复杂度与时间复杂度来衡量。 其实,算法的本质就是给你一组输入,运算之后给你一组输出,因此,算法帮人们解决很多问题把抽象的问题具体化,把一个问题转化成另外一个问题。 认识算法的N个阶段 我第一次遇到算法题,还是在我考研复习数据结构的时候,那个时候我看到的算法题其实都是很基础的题目,比如把数组中的两个元素置换,把两个链表合并成一个,但对于我来说已经是很有难度的事情了,那时候我连伪代码是什么都还不懂。 第二次认识算法,还是在研究生期间找实习工作的时候

Leetcode 35.搜索插入位置(Search Insert Position)

放肆的年华 提交于 2020-02-25 22:00:21
Leetcode 35.搜索插入位置 1 题目描述( Leetcode题目链接 )   给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。 输入 : [ 1 , 3 , 5 , 6 ] , 5 输出 : 2 输入 : [ 1 , 3 , 5 , 6 ] , 2 输出 : 1 输入 : [ 1 , 3 , 5 , 6 ] , 7 输出 : 4 输入 : [ 1 , 3 , 5 , 6 ] , 0 输出 : 0 2 题解   二分法查找正确位置,关键在于边界的处理,循环限制条件最好为 l e f t < r i g h t left<right l e f t < r i g h t ,这样会确保在退出循环的时候 l e f t = r i g h t left = right l e f t = r i g h t ,避免了要思考到底是返回哪个值。于此题,由于要插入正确位置,那么应该将数组的长度加一来看待,规定右边界的值为数组长度,左边界值为0,接下来要判断二分过程中 m i d mid m i d 与左右边界值的关系:如果 t a r g e t > n u m s [ m i d ] target>nums[mid] t a r g e t > n u m s [ m i d ]

Leetcode:110. 平衡二叉树

别来无恙 提交于 2020-02-24 09:30:53
Leetcode: 110. 平衡二叉树 Leetcode: 110. 平衡二叉树 点链接就能看到原题啦~ 关于AVL的判断函数写法,请跳转: 平衡二叉树的判断 废话不说直接上代码吧~主要的解析的都在上面的链接里了 自顶向下写法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int getHeight(TreeNode* root){ if(root==NULL) return 0; return max(getHeight(root->right),getHeight(root->left))+1; } bool isBalanced(TreeNode* root) { if(root==NULL) return true; if(isBalanced(root->left)&&isBalanced(root->right)) if(abs(getHeight(root->left)-getHeight(root->right))<2)

LeetCode \"Maximal Rectangle\"

倖福魔咒の 提交于 2020-02-24 04:58:13
My intuition is flood-fill the BFS solution, which is O(n^4); and then I figured out a DP solution which is O(n^4).. So I googled some hints: it can be built upon another LeetCode problem: Largest Rectangle in Histogram. With proper pre-calculation, we can make a O(n^3) solution. What's more, we can start from bottom-right to upper-left, and then make sure each '1' cell is visited only once - that's O(n^2). http://yucoding.blogspot.com/2013/01/incomplete-leetcode-question-47-maximal.html And as always, there's such a great solution: https://leetcode.com/discuss/20240/share-my-dp-solution 来源:

LeetCode 70 爬楼梯

泄露秘密 提交于 2020-02-22 22:40:38
题目: 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数。 示例 1: 输入: 2 输出: 2 解释: 有两种方法可以爬到楼顶。 1. 1 阶 + 1 阶 2. 2 阶 示例 2: 输入: 3 输出: 3 解释: 有三种方法可以爬到楼顶。 1. 1 阶 + 1 阶 + 1 阶 2. 1 阶 + 2 阶 3. 2 阶 + 1 阶 思路: 第二种思路 标签:动态规划 本问题其实常规解法可以分成多个子问题,爬第n阶楼梯的方法数量,等于 2 部分之和 爬上 n−1 阶楼梯的方法数量。因为再爬1阶就能到第n阶 爬上 n−2 阶楼梯的方法数量,因为再爬2阶就能到第n阶 所以我们得到公式 dp[n] = dp[n-1] + dp[n-2]dp[n]=dp[n−1]+dp[n−2] 同时需要初始化 dp[0]=1dp[0]=1 和 dp[1]=1dp[1]=1 时间复杂度:O(n)O(n) 作者:guanpengchn 链接:https://leetcode-cn.com/problems/climbing-stairs/solution/hua-jie-suan-fa-70-pa-lou-ti-by-guanpengchn/ 来源:力扣(LeetCode) 著作权归作者所有

[leetcode]Swap Nodes in Pairs @ Python

妖精的绣舞 提交于 2020-02-22 06:20:35
原题地址:http://oj.leetcode.com/problems/swap-nodes-in-pairs/ Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. 题意:将链表中的节点两两交换。Given 1->2->3->4 , you should return the list as 2->1->4->3 . 解题思路: 这题主要涉及到链表的基本操作。加一个头结点,操作起来会很方便。另外配了一个示意图 [本图是我asrman原创] 代码: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: