1.包含min函数的栈
题目
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))
思路
既要实现复杂度为o(1)的min功能,也要完成stack特有的push,pop功能。所以一个栈不可以同时实现,需要借助一个辅助栈来完成。
- push:datastack正常压入node;minstack压入最小值
- pop:datastack正常弹出,minstack只在和datastack相等的情况下弹出
- top:datastack正常弹出,如果minstack的栈顶与datastack一致那么也弹出,返回弹出值。
- min:peek辅助栈栈顶元素。
代码
public class minstack {
Stack<Integer> minstack = new Stack<>();
Stack<Integer> datastack = new Stack<>();
public void push(int node) {
datastack.push(node);
if(minstack.isEmpty())
minstack.push(node);
else if(node<=minstack.peek())
minstack.push(node);
}
public void pop() {
if(datastack.isEmpty())
throw new RuntimeException("This stack is Empty");
if(datastack.peek()==minstack.peek())
minstack.pop();
datastack.pop();
}
public int top() {
if(datastack.isEmpty())
throw new RuntimeException("This stack is Empty");
int value = datastack.pop();
if(value == minstack.pop())
minstack.pop();
return value;
}
public int min() {
if(datastack.isEmpty())
throw new RuntimeException("This stack is Empty");
else
return minstack.peek();
}
}
2.栈的压入,弹出序列
题目:
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
思路:
使用stack按照pushA的顺序入栈,模拟popA的顺序出栈
当s的栈顶元素等于popA中指向的第一个元素时,s.pop,indexpop++关键点在于:不需要使用两个循环就能实现,判断popA顺序卸载入栈的push循环中。如果pushA中所有数据都已经入栈,而s中经过出栈后还有剩余说明pushA和popA不匹配,不存在popA
代码:
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA.length==0||popA.length==0) return false;
Stack<Integer> s = new Stack<>();
int popindex = 0; //用于标识弹出序列的位置
for(int i=0;i<pushA.length;++i) {
s.push(pushA[i]);
while(!s.empty()&&s.peek()==popA[popindex]) {//弹出序列的元素==栈顶元素时弹出
s.pop();
popindex++; //弹出序列向后一位,直到不满足s,peek=popA[index]时,再继续模拟入栈
}
}
return s.empty();
}
3.二叉树和为某一值的路径
题目:
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
思路:
递归调用。
ArrayList list2 = new ArrayList(list);
使得左右分支互不影响!
代码:
ArrayList<ArrayList<Integer>> res =new ArrayList<ArrayList<Integer>> ();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root==null)return res;
findpath(root,target,new ArrayList<Integer>());
return res;
}
private void findpath(TreeNode root,int target,ArrayList<Integer> list) {
list.add(root.val);
if(root.left==null&&root.right==null) {
if(target==root.val)
{
res.add(list);
}
return ;
}
ArrayList<Integer> list2 = new ArrayList<Integer>(list);//必须写在两个子递归之前
if(root.left!=null)
findpath(root.left,target-root.val,list);
if(root.right!=null)
findpath(root.right,target-root.val,list2);
}
大佬精简代码
public class Solution {
private ArrayList<ArrayList<Integer>> listAll = new ArrayList<ArrayList<Integer>>();
private ArrayList<Integer> list = new ArrayList<Integer>();
public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
if(root == null) return listAll;
list.add(root.val);
target -= root.val;
if(target == 0 && root.left == null && root.right == null) //只有当是叶子节点并且target==0时才为路径终点
listAll.add(new ArrayList<Integer>(list));
FindPath(root.left, target);
FindPath(root.right, target);
list.remove(list.size()-1);//回退!!!最后判空
return listAll;
}
}