Stack
文章平均质量分 53
iteye_17352
这个作者很懒,什么都没留下…
展开
-
Leetcode - Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0...原创 2015-06-04 08:25:16 · 98 阅读 · 0 评论 -
Leetcode - Largest Rectangle in Histogram
Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where width of ea...原创 2015-05-20 07:53:41 · 83 阅读 · 0 评论 -
Leetcode - Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()", whic...原创 2015-04-21 09:13:13 · 66 阅读 · 0 评论 -
Leetcode - Closest Binary Search Tree Value II
[分析]思路1:遍历所有节点找到和 target最接近的 k 个元素,能否有个数据结构在遍历过程中维护已遍历元素离target最近的呢?PriorityQueue具备这种能力。我们需要个最小堆,堆元素需要保存两个信息,一个是树节点元素值,一个是这个元素和target的差的绝对值。但PriorityQueue是没有“堆底”概念的,当堆的size 增长到 k 后,如何删除堆中最大元素呢? 为实现删...原创 2015-09-13 14:16:30 · 82 阅读 · 0 评论 -
Leetcode - Closest Binary Search Tree Value II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.Note:Given target value is a floating point.You may assume k is always valid, t...原创 2015-09-01 09:50:30 · 213 阅读 · 0 评论 -
Leetcode - Binary Tree Postorder
[分析]迭代实现后序遍历比迭代实现先序和中序都要稍微复杂,对其一直有恐惧心理,总觉得自己不看答案做不出来……这次竟然做出来了,很开心,练习还是有效果的~思路1:遍历到curr节点,不管三七二十一,入栈,继续访问左节点,因为后序遍历中root最后访问。问题是栈中的节点何时出栈?答案是其左右节点均被访问之后。怎么判断其左右节点均被访问过了呢?或者换个问题,怎么判断右节点没有被访问过(一路向左,...原创 2015-08-29 21:07:42 · 82 阅读 · 0 评论 -
Leetcode - Verify Preorder Sequence in Binary Search Tree
[分析]思路1:暴力法,遍历当前待检查数组,找到第一个大于数组起始位置的位置i,则 i 为右子树根节点,然后递归判断左右子树。对于根节点,最坏情况下要遍历整个数组,时间为O(N),因为要递归检查每个节点,因此总的时间复杂度是O(N^2)。思路2:参考StefanPochmann大神的作品[url]https://leetcode.com/discuss/51543/java-o-n-and...原创 2015-08-29 16:46:16 · 83 阅读 · 0 评论 -
Leetcode - Min Stack
[分析]这题属于简单题,之所以要记录下是因为遇到了个不理解的Java语法问题。在pop()时如果判断条件写成if (minStack.peek() == stack.peek())是要吃WA的,原因是在表达式中比较的是引用本身,而不是值,奇怪的是在main中我认为一样的情况(e\f这对)判断结果却不同。在pop()中执行如下两句: System.out.print...原创 2015-08-29 11:20:36 · 88 阅读 · 0 评论 -
Leetcode - Basic Calculator II
mplement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should tr...原创 2015-08-27 09:16:27 · 80 阅读 · 0 评论 -
Leetcode - Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"[分析] 思路很简单,遇到“.”跳过,遇到“..”删除其前面一级目录,使用堆栈来删除前一级目录的想法很妙(要是我...原创 2015-06-17 21:58:55 · 62 阅读 · 0 评论 -
Leetcode - Calculator
[分析]思路1:逆序遍历字符串,数字和右括号保存在一个堆栈stack1中,运算符保存在另一个堆栈stack2中,跳过空格,遇到左括号时计算stack1中首个右括号之上的所有数据,也即当前括号对中的内容。 在Accept之前有两个出错点:1)采用顺序遍历。出错case:2-1+1,因为加减的运算顺序是从左至右;2)逆序遍历时拼接完一个位数大于1的完整int 后要逆序已得到实际数值。出错case:...原创 2015-06-10 09:31:43 · 75 阅读 · 0 评论 -
Leetcode - Maximum Rectangle
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.[balabala] 这题想到借用Largest Rectangle in Histogram的思路就比较简单了。计算行 i 时,以这行为底,计算每一列对应的...原创 2015-05-20 08:58:59 · 143 阅读 · 0 评论