leetcode

Leetcode solution 173: Binary Search Tree Iterator

十年热恋 提交于 2019-11-28 09:06:49
Problem Statement Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Example: BSTIterator iterator = new BSTIterator(root); iterator.next(); // return 3 iterator.next(); // return 7 iterator.hasNext(); // return true iterator.next(); // return 9 iterator.hasNext(); // return true iterator.next(); // return 15 iterator.hasNext(); // return true iterator.next(); // return 20 iterator.hasNext(); // return false Note: next() and hasNext() should run in average O(1)

LeetCode 181. Employees Earning More Than Their Managers (超过经理收入的员工)

旧街凉风 提交于 2019-11-28 06:45:25
题目标签:   题目给了我们一个 员工表,包括经理。员工会有经理的id。   这里可以重复 利用两次 表格,表格a, 表格b,当a 员工的经理id 等于 b员工时候,在从中找到员工工资大于经理的。具体看code。 Java Solution: Runtime: 312 ms, faster than 65 % Memory Usage: N/A 完成日期:05/22/2019 关键点:From 员工表a,b # Write your MySQL query statement below SELECT a.Name AS Employee FROM Employee a, Employee b WHERE a.ManagerId = b.Id AND a.Salary > b.Salary 参考资料:LeetCode Solution LeetCode 题目列表 - LeetCode Questions List 题目来源:https://leetcode.com/ 来源: https://www.cnblogs.com/jimmycheng/p/11397741.html

[LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点

非 Y 不嫁゛ 提交于 2019-11-28 06:27:17
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: The node of a binary tree is a leaf if and only if it has no children The depth of the root of the tree is 0, and if the depth of a node is d , the depth of each of its children is d+1 . The lowest common ancestor of a set S of nodes is the node A with the largest depth such that every node in S is in the subtree with root A . Example 1: Input: root = [1,2,3] Output: [1,2,3] Explanation: The deepest leaves are the nodes with values 2 and 3. The lowest common ancestor of these leaves is the node

LeetCode第四题-寻找两个有序数组的中位数

本秂侑毒 提交于 2019-11-28 06:14:06
题目 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 示例 示例 1: nums1 = [1, 3] nums2 = [2] 则中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 则中位数是 (2 + 3)/2 = 2.5 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 思路 本菜鸡拿到这道题第一想法仍然是暴力破解,没办法菜鸡就是菜鸡,但是忽然看到题目限定了时间复杂度 O(log(m + n)),然而暴力破解需要将两个数组进行排序,然后找到中位数,这样一来时间复杂度就成了O(m+n),这与要求是不符的,接下来看一下官方是在限定的时间复杂度内怎么解决这道题的方法的思路: 方法

DFS小结

廉价感情. 提交于 2019-11-28 06:10:25
DFS 通过一些leetcode上面的题目, 总结出以下类型的题目; 树先序遍历 DFS + 回溯 DFS + 记忆化 DFS + 减枝 DFS 求最短路 DFS 求联通块儿 先序遍历 过于简单, 看看数据结构都能懂的。 题目 leetcode 144. 二叉树的前序遍历 leetcode 257. 二叉树的所有路径 DFS + 回溯 比较典型的问题是 迷宫 , 八皇后 等问题, 也比较简单。 当此题如果是判断某种情况存不存在, 则判断到存在的时候立马停止掉后续的所有DFS。 题目 leetcode 79. 单词搜索 DFS + 记忆化 DFS 过程当中同一个 节点的 状态可以多次进入, 而多次进入的需要求得结果均可使用第一次求得的结果, 这样能节省大量的时间。 模板 T dfs(...) { if 子节点已经记载: 求得当前顶点所对应的值 else dfs(子节点) 再求得当前顶点所对应的值 //此处记载当前顶点对应的值。 } 题目 leetcode 329. 矩阵中的最长递增路径 给定一个整数矩阵,找出最长递增路径的长度。 对于每个单元格,你可以往上,下,左,右四个方向移动。 你不能在对角线方向上移动或移动到边界外(即不允许环绕)。 输入: nums = [ [9,9,4], [6,6,8], [2,1,1] ] 输出: 4 解释: 最长递增路径为 [1, 2, 6, 9]。

(Easy) Detect Capital -LeetCode

泪湿孤枕 提交于 2019-11-28 05:39:22
Description: Given a word, you need to judge whether the usage of capitals in it is right or not. We define the usage of capitals in a word to be right when one of the following cases holds: All letters in this word are capitals, like "USA". All letters in this word are not capitals, like "leetcode". Only the first letter in this word is capital, like "Google". Otherwise, we define that this word doesn't use capitals in a right way. Example 1: Input: "USA" Output: True Example 2: Input: "FlaG" Output: False Note: The input will be a non-empty word consisting of uppercase and lowercase latin

求解最大子数组的和

纵然是瞬间 提交于 2019-11-28 01:48:02
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: 6 Explanation: [4,-1,2,1] has the largest sum = 6. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 初看这道题,好像很简单(照LeetCode的难度系数来看好像的确是挺简单的哈哈),然而在牛客网提交的代码通过了,在LeetCode这里却没有通过,好严格。。。 一般来说,这种题目其实都可以无脑用暴力解法的,虽然效率极低,有时候甚至会超时,但是确实也是一种思路。 class Solution { public int maxSubArray(int[] nums) { int length = nums.length; int result = nums[0]; int sum; for(int i = 0; i

leetcode刷题13

孤街浪徒 提交于 2019-11-28 01:28:00
j今天刷的题是LeetCode第235题。题目要求是:给定一个二叉搜索树,找到该树中两个指定节点的最近公共祖先 即,对于有根树T的两个节点pq,最近公共祖先表示为一个节点x,满足pq是x的子节点且x的深度尽可能大 其中给定的二叉树,所有节点的值均不相同,并且pq为不同节点且在二叉树中 开始的时候,想通过暴力法求解:即找到pq的节点的所有祖先节点,然后找出公共的节点即可。但是这在遍历搜索的时候出了问题,暂时还没解决 然后是参考LeetCode的官方解答思路:因为二叉搜索树有一个特点,那就是左子树的节点值都比右子树节点值小 pq的存在一共有三种情况:①pq在节点的左右子树中,此时pq的值将根节点的值夹在中间;②pq在左子树中,此时,pq的值均小于根节点的值;③pq的值在右子树中,此时pq的值均大于根节点的值 具体地代码如下: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p,

leetcode-解题记录 557. 反转字符串中的单词 III

匆匆过客 提交于 2019-11-28 00:04:47
题目: 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。 来源: 力扣(LeetCode) 解题python class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ return ' '.join(map(lambda x: x[::-1], s.split(' '))) 来源: https://www.cnblogs.com/xiaomingtx/p/11381793.html

Leetcode 75. Sort Colors

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 21:42:15
https://leetcode.com/problems/sort-colors/ Medium Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use the library's sort function for this problem. Example: Input: [2,0,2,1,1,0] Output: [0,0,1,1,2,2] Follow up: A rather straight forward solution is a two-pass algorithm using counting sort. First, iterate the array counting number of 0's, 1's