随笔分类 - Leetcode
Leetcode: Sliding Window Maximum
摘要:方法一:Heap时间 O(NlogK) 空间 O(K) maintain a maximum heap, notice PriorityQueue.remove(object o) can remove certain object from our heap. When writting Maxi
阅读全文
Leetcode: Product of Array Except Self
摘要:Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except ...
阅读全文
Leetcode: Delete Node in a Linked List
摘要:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -
阅读全文
Leetcode: 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...
阅读全文
Leetcode: Lowest Common Ancestor of a Binary Search Tree
摘要:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia
阅读全文
Leetcode: Palindrome Linked List
摘要:Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?先分成大小相同(可能长度差1) 两部分, reverse一个list. ...
阅读全文
Leetcode: Number of Digit One
摘要:Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6...
阅读全文
Leetcode: Power of Two
摘要:Best Solution 还是有一个地方不小心:Integer.MIN_VALUE, 应该是false,但是因为只有一个1,我曾经判断为true。事实上,所有negative value都应该是false 一旦符号位为1,就return false, 检查其他位只有1个1 做的时候遇到很多语法错误
阅读全文
Leetcode: Implement Queue using Stacks
摘要:Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front o...
阅读全文
Leetcode: Majority Element II
摘要:Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.Hin...
阅读全文
Leetcode: Kth Smallest Element in a BST
摘要:Java Solution 1 - Inorder Traversal We can inorder traverse the tree and get the kth smallest element. Time is O(n). Recursion method: Java Solution 2
阅读全文
Leetcode: Basic Calculator II
摘要:用Stack来做:时间 O(N) 空间 O(N) 因为乘法和除法不仅要知道下一个数,也要知道上一个数。所以我们用一个栈把上次的数存起来,遇到加减法就直接将数字压入栈中,遇到乘除法就把栈顶拿出来乘或除一下新数,再压回去。最后我们把栈里所有数加起来就行了。 上面这段code可以先用String.repl
阅读全文
Leetcode: Basic Calculator
摘要:https://discuss.leetcode.com/topic/15816/iterative-java-solution-with-stack 第二遍做法:遇到digit直接走到末尾,并且直接利用sign进行计算,这样比较方便 第一遍做法:
阅读全文
Leetcode: Count Complete Tree Nodes
摘要:递归树高法 复杂度 时间 O(N) 空间 O(1) 思路 完全二叉树的一个性质是,如果左子树最左边的深度,等于右子树最右边的深度,说明这个二叉树是满的,即最后一层也是满的,则以该节点为根的树其节点一共有2^h-1个。如果不等于,则是左子树的节点数,加上右子树的节点数,加上自身这一个。 注意 这里在左
阅读全文
Leetcode: Implement Stack using Queues
摘要:Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. to
阅读全文
Leetcode: Maximal Square
摘要:dp问题,用一个dp[i][j]保存matrix[i][j]作为右下节点的时候的最大矩形的边长 if (matrix[i][j] == '0') dp[i][j] = 0; else dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i][j-1]) + 1;
阅读全文
Leetcode: Contains Duplicate III
摘要:暴力枚举会LTE,解题思路是用sortedSet来解决,sortedSet可以返回一个在某个范围的subSet,同时判断这个subSet是否为空即可解决,主要,本题需要使用long类型! 时间 O(NlogK) 空间 O(K) 思路 要求判断之前是否存在差值小于t的数字,我们需要知道在当前数字x两边
阅读全文
Leetcode: Invert Binary Tree
摘要:nvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:This problem was inspired by this...
阅读全文
Leetcode: The Skyline Problem
摘要:参考了http://www.cnblogs.com/tonyluis/p/4564617.html 复杂度 Time Complexity is O(n^2) because removal in a PQ is O(n), 空间 O(N) I think if replace PQ with Tr
阅读全文
浙公网安备 33010602011771号