题目:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
题目分析:需要定义栈数据结构,并提供找出栈内最小元素的min函数,并且时间复杂度O(1)。
思路:借助辅助栈来实现(入栈时:添加的元素小于辅助栈的最顶元素,则入到辅助栈中。出战时:辅助栈和元栈栈顶元素相等则辅助栈出栈。)
对原来栈的封装,使之实现O(1)复杂度的min方法。
1 import java.util.Stack;
2
3 public class Solution {
4 //O(1)时间复杂度,借助辅助栈来实现(入栈时:添加的元素小于辅助栈的最顶元素,则入到辅助栈中。出战时:辅助栈和元栈栈顶元素相等则辅助栈出栈。)。
5 //对原来栈的封装,使之实现O(1)复杂度的min方法。
6 Stack<Integer> stack1 = new Stack<>(),stack2 = new Stack<>();
7 public void push(int node) {
8 stack1.push(node);
9 if(stack2.empty()){
10 stack2.push(node);
11 }else if(node<stack2.peek()){
12 stack2.push(node);
13 }
14 }
15
16 public void pop() {
17 if(stack1.peek()==stack2.peek()){
18 stack2.pop();
19 }
20 stack1.pop();
21
22 }
23
24 public int top() {
25 return stack1.peek();
26 }
27
28 public int min() {
29 return stack2.peek();
30 }
31 }
题目:输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
思路:利用辅助栈,对入栈序列压栈,判断栈顶元素与出栈元素是否相同,相同则出栈,最后判断栈是否为空。
1 import java.util.ArrayList;
2 import java.util.Stack;
3 public class Solution {
4 //利用辅助栈,对入栈序列压栈,判断栈顶元素与出栈元素是否相同,相同则出栈,最后判断栈是否为空。
5 public boolean IsPopOrder(int [] pushA,int [] popA) {
6 if(pushA.length == 0 || popA.length == 0)
7 return false;
8 Stack<Integer> tmp = new Stack<>();
9 int j=0;
10 for(int i=0;i<pushA.length;i++){
11 tmp.push(pushA[i]);
12 while(!tmp.empty()&&tmp.peek()==popA[j]){
13 tmp.pop();
14 j++;
15 }
16 }
17 return tmp.empty();
18 }
19 }
题目:从上往下打印出二叉树的每个节点,同层节点从左至右打印。
思路:使用ArrayList模拟队列,先进先出,实现逐层遍历。
1 import java.util.ArrayList;
2 /**
3 public class TreeNode {
4 int val = 0;
5 TreeNode left = null;
6 TreeNode right = null;
7
8 public TreeNode(int val) {
9 this.val = val;
10
11 }
12
13 }
14 */
15 public class Solution {
16 public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
17 /**
18 //逐层遍历
19 if(root == null){
20 return new ArrayList<Integer>();
21 }
22 ArrayList<TreeNode> list = new ArrayList<>();
23 list.add(root);
24 return method(list);
25 **/
26 //使用ArrayList模拟队列,先进先出
27 ArrayList<Integer> list = new ArrayList<>();
28 ArrayList<TreeNode> queue = new ArrayList<>();
29 if(root == null){
30 return list;
31 }
32 queue.add(root);
33 while(!queue.isEmpty()){
34 TreeNode tmp = queue.remove(0);
35 if(tmp.left!=null){
36 queue.add(tmp.left);
37 }
38 if(tmp.right!=null){
39 queue.add(tmp.right);
40 }
41 list.add(tmp.val);
42 }
43 return list;
44 }
45 //逐层将TreeNode保存到ArrayList中
46 private ArrayList<Integer> method(ArrayList<TreeNode> floor){
47 ArrayList<Integer> nodelist = new ArrayList<>();
48 if(floor==null||floor.size()==0){
49 return null;
50 }
51 ArrayList<TreeNode> list = new ArrayList<TreeNode>();
52 for(TreeNode node:floor){
53 nodelist.add(node.val);
54 if(node.left!=null){
55 list.add(node.left);
56 }
57 if(node.right!=null){
58 list.add(node.right);
59 }
60 }
61 ArrayList<Integer> tmpList = method(list);
62 if(tmpList!=null)
63 nodelist.addAll(tmpList);
64 return nodelist;
65 }
66 }
题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
1 public class Solution {
2 public boolean VerifySquenceOfBST(int [] sequence) {
3 if(sequence==null||sequence.length==0){
4 return false;
5 }
6 if(sequence.length==1){
7 return true;
8 }else{
9 int root = sequence[sequence.length-1];
10 int cut = 0;
11 while(sequence[cut]<root){
12 cut++;
13 }
14 for(int i=cut;i<sequence.length;i++){
15 if(sequence[i]<root){
16 return false;
17 }
18 }
19 int[] left = new int[cut];
20 int[] right = new int[sequence.length-cut-1];
21 for(int i=0;i<sequence.length-1;i++){
22 if(i<cut){
23 left[i] = sequence[i];
24 }else{
25 right[i-cut] = sequence[i];
26 }
27 }
28 if(left.length!=0){
29 if(right.length!=0){
30 return VerifySquenceOfBST(left)&&VerifySquenceOfBST(right);
31 }else{
32 return VerifySquenceOfBST(left);
33 }
34 }else{
35 if(right.length!=0){
36 return VerifySquenceOfBST(right);
37 }else{
38 return true;
39 }
40 }
41
42 }
43
44 }
45 }
来源:https://www.cnblogs.com/w-honey/p/12261909.html