Skip to content

Commit 52f9958

Browse files
committed
daylily
1 parent b5fa97f commit 52f9958

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

Solution29.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
package com.usher.algorithm.offer;
22

3+
import java.util.ArrayList;
4+
35
/**
46
* @Author: Usher
57
* @Description:
8+
* 顺时针打印矩阵
9+
* col > startX * 2,row > startY * 2
610
*/
711
public class Solution29 {
12+
public ArrayList<Integer> printMatrix(int [][] matrix) {
13+
ArrayList<Integer> ret = new ArrayList<>();
14+
int r1 = 0, r2 = matrix.length - 1, c1 = 0, c2 = matrix[0].length - 1;
15+
while (r1 <= r2 && c1 <= c2) {
16+
for (int i = c1; i <= c2; i++)
17+
ret.add(matrix[r1][i]);
18+
for (int i = r1 + 1; i <= r2; i++)
19+
ret.add(matrix[i][c2]);
20+
if (r1 != r2)
21+
for (int i = c2 - 1; i >= c1; i--)
22+
ret.add(matrix[r2][i]);
23+
if (c1 != c2)
24+
for (int i = r2 - 1; i > r1; i--)
25+
ret.add(matrix[i][c1]);
26+
r1++; r2--; c1++; c2--;
27+
}
28+
return ret;
29+
}
830
}

Solution30.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
package com.usher.algorithm.offer;
22

3+
import java.util.Stack;
4+
35
/**
46
* @Author: Usher
57
* @Description:
8+
* 包含min的栈
69
*/
710
public class Solution30 {
11+
Stack<Integer> s1 = new Stack<>();
12+
Stack<Integer> s2 = new Stack<>();
13+
Stack<Integer> s3 = new Stack<>();
14+
15+
public void push(int node){
16+
s1.push(node);
17+
if (s2.isEmpty()){
18+
s2.push(node);
19+
}else if (node <= s2.peek()){
20+
s2.push(node);
21+
}
22+
if (s3.isEmpty()){
23+
s3.push(node);
24+
}else if (node >= s3.peek()){
25+
s3.push(node);
26+
}
27+
}
28+
public void pop(){
29+
int data = s1.pop();
30+
if (data == s2.peek()){
31+
s2.pop();
32+
}
33+
if (data == s3.peek()){
34+
s3.pop();
35+
}
36+
}
37+
public int top(){
38+
return s1.peek();
39+
}
40+
public int min(){
41+
return s2.peek();
42+
}
43+
public int max(){
44+
return s3.peek();
45+
}
846
}

Solution31.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.usher.algorithm.offer;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* @Author: Usher
7+
* @Description:
8+
* 栈的压入,弹出序列
9+
* 链接:https://www.nowcoder.com/questionTerminal/d77d11405cc7470d82554cb392585106
10+
来源:牛客网
11+
12+
借用一个辅助的栈,遍历压栈顺序,先讲第一个放入栈中,这里是1,然后判断栈顶元素是不是出栈顺序的第一个元素,这里是4,很显然1≠4,所以我们继续压栈,直到相等以后开始出栈,出栈一个元素,则将出栈顺序向后移动一位,直到不相等,这样循环等压栈顺序遍历完成,如果辅助栈还不为空,说明弹出序列不是该栈的弹出顺序。
13+
14+
举例:
15+
16+
入栈1,2,3,4,5
17+
18+
出栈4,5,3,2,1
19+
20+
首先1入辅助栈,此时栈顶1≠4,继续入栈2
21+
22+
此时栈顶2≠4,继续入栈3
23+
24+
此时栈顶3≠4,继续入栈4
25+
26+
此时栈顶4=4,出栈4,弹出序列向后一位,此时为5,,辅助栈里面是1,2,3
27+
28+
此时栈顶3≠5,继续入栈5
29+
30+
此时栈顶5=5,出栈5,弹出序列向后一位,此时为3,,辅助栈里面是1,2,3
31+
32+
….
33+
34+
依次执行,最后辅助栈为空。如果不为空说明弹出序列不是该栈的弹出顺序。
35+
*/
36+
public class Solution31 {
37+
public boolean IsPopOrder(int [] pushA,int [] popA) {
38+
Stack<Integer> stack = new Stack<>();
39+
//用于标识弹出序列的位置
40+
int popIndex = 0;
41+
for (int i =0;i < pushA.length;i++){
42+
stack.push(pushA[i]);
43+
//如果栈不为空,且栈顶元素等于弹出序列
44+
while (!stack.isEmpty() && stack.peek() == popA[popIndex]){
45+
stack.pop();
46+
//弹出序列向后一位
47+
popIndex++;
48+
}
49+
}
50+
return stack.isEmpty();
51+
}
52+
}

Solution32.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.usher.algorithm.offer;
2+
3+
import java.util.*;
4+
5+
/**
6+
* @Author: Usher
7+
* @Description:
8+
* 从上到下打印二叉树
9+
* 迭代(队列)层次遍历,
10+
* 先把每层父节点push到队列,遍历左右子节点是否为叶子节点,不是就push到队列,删除父节点,
11+
* 直到队列为空
12+
*/
13+
public class Solution32 {
14+
private List<Integer> list = new ArrayList<>();
15+
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
16+
if (root == null)
17+
return new ArrayList<>();
18+
Queue<TreeNode> queue = new LinkedList<>();
19+
queue.offer(root);
20+
while (!queue.isEmpty()){
21+
int size = queue.size();
22+
for(int i = 0;i < queue.size();i++){
23+
TreeNode node = queue.poll();
24+
list.add(node.val);
25+
if (node.left != null)
26+
queue.offer(node.left);
27+
if (node.right != null)
28+
queue.offer(node.right);
29+
}
30+
}
31+
return (ArrayList<Integer>) list;
32+
}
33+
}

0 commit comments

Comments
 (0)