- 博客(14)
- 收藏
- 关注
原创 leetcode232. 用栈实现队列
思路一: 用两个栈实现队列 class MyQueue { Stack<Integer>stk = new Stack<Integer>(); /** Initialize your data structure here. */ public MyQueue() { } /** Push eleme...
2019-04-11 22:55:28
133
原创 leetcode94. 二叉树的中序遍历
思路一: 经典的递归算法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Sol...
2019-04-11 20:22:20
107
原创 leetcode224. 基本计算器
思路一: 1.运算符和数字两个栈 2.数字遇到栈顶是数字则直接出栈计算 结果入栈 3.左括号则入栈 4.右括号则出栈(表达式始终有效,则运算符的栈顶此时必定为左括号),并且直接计算此时数字栈残留的数字,这样就有效避免了栈中多于两个数字的情况(这样出栈计算会导致运算式子变为右结合) class Solution { public int Cal(int a,int b,char ...
2019-04-11 19:54:09
253
原创 leetcode173. 二叉搜索树迭代器
思路一: 二叉搜索树是中序遍历由小到大排列的,根据这个特性可以找到中序排列的序列来解,顺便复习了中序遍历的非递归算法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * T...
2019-04-10 14:37:33
141
原创 leetcode150. 逆波兰表达式求值
思路一: 逆波兰表达式其实就是后序遍历二叉树的序列 根据左右根的顺序入栈出栈即可 class Solution { public int evalRPN(String[] tokens) { Stack<Integer>stk1 = new Stack<Integer>(); Stack<Character>stk...
2019-04-09 19:55:55
92
原创 leetcode145. 二叉树的后序遍历
思路一: 递归写法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Soluti...
2019-04-09 13:18:13
97
原创 leetcode103. 二叉树的锯齿形层次遍历
思路一: 只需要奇偶层分开处理 本来卡在不知道咋确定层数,后来发现在上一层出队之前其实就可以确定这一层有多少节点,再执行入队操作 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; ...
2019-04-08 12:57:13
85
原创 leetcode144. 二叉树的前序遍历
思路一: 迭代 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution...
2019-04-07 17:22:18
80
原创 leetcode85. 最大矩形
思路一: 利用单调栈,和前一题计算最大矩形相似,但这里需要多一步转化,矩阵转化为一个个的一维数组,每个一维数组的其中一个数字是每一列从0开始连续1的个数,如果中间遇到有0,则直接计算下一列的连续1个数(因为悬空的1对于此一维数组来说没有用,可以直接认为不存在) class Solution { public int maximalRectangle(char[][] matrix) ...
2019-04-07 16:43:22
110
原创 leetcode84. 柱状图中最大的矩形
思路一: 前一個柱子若大於後一個柱子,則它大於的部分其實對於後一個柱子後面的柱子與他形成的矩形大小沒有關係 基於此,每次遍歷回去更新,比較暴力,最後要優化一下全是同一高度的情況,即整個圖形爲一個矩形。 class Solution { public int largestRectangleArea(int[] heights) { int flag=0; ...
2019-03-29 16:53:12
187
1
原创 leetcode 687
思路一:每一個根節點都有左右子樹,那麼遍歷根節點找到每個根節點的最長同值路徑即可。 對於每一個根節點,左子樹最大值,右子樹最大值,或者是左右子樹相加的最大值。 返回上一層,返回的值是左右子樹相對較大的那個值 /** * Definition for a binary tree node. * public class TreeNode { * int val; * ...
2019-03-28 21:20:26
224
原创 leetcode 71 簡化路徑
思路一: 這題有點坑,不過也是自己不夠熟悉文件路徑導致的,“.”超過三個及以上就是普通文件夾或文件的名了 做法是先用棧去掉多餘的“/”,然後在將字符串分爲一個個的小段,這樣判斷路徑是什麼類型就比較容易了 class Solution { public String simplifyPath(String path) { Stack<Character>s...
2019-03-27 22:51:12
73
原创 leetcode 331. 验证二叉树的前序序列化
思路一: 利用棧模擬前序遍歷的相反過程,且每個前序序列最後必定爲“#” class Solution { public boolean isValidSerialization(String preorder) { String[] str = preorder.split(","); Stack<String>stk = new Sta...
2019-03-25 19:59:59
364
原创 leetcode 接雨水
思路一: 一層層的遍歷,時間容易超限 思路二: 利用棧,將其從大到小排列進棧中,遇到比棧頂大的數則找到了右邊界,此時陸續出棧並計算各自與右邊界的相差,直到棧頂大於右邊界,將右邊界入棧。 class Solution { public int trap(int[] height) { Stack<Integer>stk = new Stack<In...
2019-03-23 22:24:29
238
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人