Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d419858

Browse files
committed
code input method
1 parent e58db7d commit d419858

9 files changed

+658
-150
lines changed

‎src/main/java/grey/algorithm/Code_0012_LeetCode_0145_BinaryTreePostorderTraversal.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.util.*;
44

5-
65
// https://leetcode.com/problems/binary-tree-postorder-traversal/
76
// 二叉树的后序遍历
87
// 笔记:https://www.cnblogs.com/greyzeng/articles/15941957.html
@@ -56,30 +55,30 @@ public List<Integer> postorderTraversal2(TreeNode root) {
5655
// TODO
5756
// 【非递归】【单栈】后序遍历
5857
public static List<Integer> postorderTraversal1(TreeNode h) {
59-
List<Integer> ans = new ArrayList<>();
60-
if (h != null) {
61-
Stack<TreeNode> stack = new Stack<>();
62-
stack.push(h);
63-
// 如果始终没有打印过节点,h就一直是头节点
64-
// 一旦打印过节点,h就变成打印节点
65-
// 之后h的含义 : 上一次打印的节点
66-
while (!stack.isEmpty()) {
67-
TreeNode cur = stack.peek();
68-
if (cur.left != null && h != cur.left && h != cur.right) {
69-
// 有左树且左树没处理过
70-
stack.push(cur.left);
71-
} else if (cur.right != null && h != cur.right) {
72-
// 有右树且右树没处理过
73-
stack.push(cur.right);
74-
} else {
75-
// 左树、右树 没有 或者 都处理过了
76-
ans.add(cur.val);
77-
h = stack.pop();
78-
}
79-
}
80-
}
81-
return ans;
82-
}
58+
List<Integer> ans = new ArrayList<>();
59+
if (h != null) {
60+
Stack<TreeNode> stack = new Stack<>();
61+
stack.push(h);
62+
// 如果始终没有打印过节点,h就一直是头节点
63+
// 一旦打印过节点,h就变成打印节点
64+
// 之后h的含义 : 上一次打印的节点
65+
while (!stack.isEmpty()) {
66+
TreeNode cur = stack.peek();
67+
if (cur.left != null && h != cur.left && h != cur.right) {
68+
// 有左树且左树没处理过
69+
stack.push(cur.left);
70+
} else if (cur.right != null && h != cur.right) {
71+
// 有右树且右树没处理过
72+
stack.push(cur.right);
73+
} else {
74+
// 左树、右树 没有 或者 都处理过了
75+
ans.add(cur.val);
76+
h = stack.pop();
77+
}
78+
}
79+
}
80+
return ans;
81+
}
8382

8483
// morris遍历实现后序遍历
8584
// 处理时机放在能回到自己两次的点,且第二次回到自己的时刻,第二次回到他自己的时候,
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4+
*/
5+
package grey.algorithm;
6+
7+
// 本文件课上没有讲
8+
// java同学可以使用FastReader进行快读,可以使用FastWriter进行快写,速度是很快的
9+
// 如何使用可以参考main函数
10+
11+
import java.io.BufferedWriter;
12+
import java.io.ByteArrayOutputStream;
13+
import java.io.FileNotFoundException;
14+
import java.io.FileOutputStream;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.io.OutputStream;
18+
import java.io.Writer;
19+
import java.util.InputMismatchException;
20+
public class Code_0013_FastReaderWriter {
21+
22+
public static void main(String[] args) {
23+
FastReader reader = new FastReader(System.in);
24+
FastWriter writer = new FastWriter(System.out);
25+
System.out.println("输入一个字符:");
26+
int cha = reader.readByte(); // reader会读到字符的ASCII码
27+
System.out.println("输入一个int类型的数字:");
28+
int num1 = reader.readInt(); // reader会读到该数字
29+
System.out.println("输入一个long类型的数字:");
30+
long num2 = reader.readLong(); // reader会读到该数字
31+
System.out.println("打印结果:");
32+
writer.println(cha);
33+
writer.println(num1);
34+
writer.println(num2);
35+
writer.close();// close方法包含flush,会把结果刷出去
36+
}
37+
38+
// 快读
39+
public static class FastReader {
40+
InputStream is;
41+
private byte[] inbuf = new byte[1024];
42+
public int lenbuf = 0;
43+
public int ptrbuf = 0;
44+
45+
public FastReader(final InputStream is) {
46+
this.is = is;
47+
}
48+
49+
public int readByte() {
50+
if (lenbuf == -1) {
51+
throw new InputMismatchException();
52+
}
53+
if (ptrbuf >= lenbuf) {
54+
ptrbuf = 0;
55+
try {
56+
lenbuf = is.read(inbuf);
57+
} catch (IOException e) {
58+
throw new InputMismatchException();
59+
}
60+
if (lenbuf <= 0) {
61+
return -1;
62+
}
63+
}
64+
return inbuf[ptrbuf++];
65+
}
66+
67+
public int readInt() {
68+
return (int) readLong();
69+
}
70+
71+
public long readLong() {
72+
long num = 0;
73+
int b;
74+
boolean minus = false;
75+
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
76+
;
77+
if (b == '-') {
78+
minus = true;
79+
b = readByte();
80+
}
81+
while (true) {
82+
if (b >= '0' && b <= '9') {
83+
num = num * 10 + (b - '0');
84+
} else {
85+
return minus ? -num : num;
86+
}
87+
b = readByte();
88+
}
89+
}
90+
}
91+
92+
// 快写
93+
public static class FastWriter {
94+
private static final int BUF_SIZE = 1 << 13;
95+
private final byte[] buf = new byte[BUF_SIZE];
96+
private OutputStream out;
97+
private Writer writer;
98+
private int ptr = 0;
99+
100+
public FastWriter(Writer writer) {
101+
this.writer = new BufferedWriter(writer);
102+
out = new ByteArrayOutputStream();
103+
}
104+
105+
public FastWriter(OutputStream os) {
106+
this.out = os;
107+
}
108+
109+
public FastWriter(String path) {
110+
try {
111+
this.out = new FileOutputStream(path);
112+
} catch (FileNotFoundException e) {
113+
throw new RuntimeException("FastWriter");
114+
}
115+
}
116+
117+
public FastWriter write(byte b) {
118+
buf[ptr++] = b;
119+
if (ptr == BUF_SIZE) {
120+
innerflush();
121+
}
122+
return this;
123+
}
124+
125+
public FastWriter write(String s) {
126+
s.chars().forEach(c -> {
127+
buf[ptr++] = (byte) c;
128+
if (ptr == BUF_SIZE) {
129+
innerflush();
130+
}
131+
});
132+
return this;
133+
}
134+
135+
private static int countDigits(long l) {
136+
if (l >= 1000000000000000000L) {
137+
return 19;
138+
}
139+
if (l >= 100000000000000000L) {
140+
return 18;
141+
}
142+
if (l >= 10000000000000000L) {
143+
return 17;
144+
}
145+
if (l >= 1000000000000000L) {
146+
return 16;
147+
}
148+
if (l >= 100000000000000L) {
149+
return 15;
150+
}
151+
if (l >= 10000000000000L) {
152+
return 14;
153+
}
154+
if (l >= 1000000000000L) {
155+
return 13;
156+
}
157+
if (l >= 100000000000L) {
158+
return 12;
159+
}
160+
if (l >= 10000000000L) {
161+
return 11;
162+
}
163+
if (l >= 1000000000L) {
164+
return 10;
165+
}
166+
if (l >= 100000000L) {
167+
return 9;
168+
}
169+
if (l >= 10000000L) {
170+
return 8;
171+
}
172+
if (l >= 1000000L) {
173+
return 7;
174+
}
175+
if (l >= 100000L) {
176+
return 6;
177+
}
178+
if (l >= 10000L) {
179+
return 5;
180+
}
181+
if (l >= 1000L) {
182+
return 4;
183+
}
184+
if (l >= 100L) {
185+
return 3;
186+
}
187+
if (l >= 10L) {
188+
return 2;
189+
}
190+
return 1;
191+
}
192+
193+
public FastWriter write(long x) {
194+
if (x == Long.MIN_VALUE) {
195+
return write("" + x);
196+
}
197+
if (ptr + 21 >= BUF_SIZE) {
198+
innerflush();
199+
}
200+
if (x < 0) {
201+
write((byte) '-');
202+
x = -x;
203+
}
204+
int d = countDigits(x);
205+
for (int i = ptr + d - 1; i >= ptr; i--) {
206+
buf[i] = (byte) ('0' + x % 10);
207+
x /= 10;
208+
}
209+
ptr += d;
210+
return this;
211+
}
212+
213+
public FastWriter writeln(long x) {
214+
return write(x).writeln();
215+
}
216+
217+
public FastWriter writeln() {
218+
return write((byte) '\n');
219+
}
220+
221+
private void innerflush() {
222+
try {
223+
out.write(buf, 0, ptr);
224+
ptr = 0;
225+
} catch (IOException e) {
226+
throw new RuntimeException("innerflush");
227+
}
228+
}
229+
230+
public void flush() {
231+
innerflush();
232+
try {
233+
if (writer != null) {
234+
writer.write(((ByteArrayOutputStream) out).toString());
235+
out = new ByteArrayOutputStream();
236+
writer.flush();
237+
} else {
238+
out.flush();
239+
}
240+
} catch (IOException e) {
241+
throw new RuntimeException("flush");
242+
}
243+
}
244+
245+
public FastWriter println(long x) {
246+
return writeln(x);
247+
}
248+
249+
public void close() {
250+
flush();
251+
try {
252+
out.close();
253+
} catch (Exception e) {
254+
}
255+
}
256+
257+
}
258+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package grey.algorithm;
2+
3+
// 展示填函数风格的测试方式
4+
// 子矩阵的最大累加和问题,不要求会解题思路,后面的课会讲
5+
// 测试链接 : https://www.nowcoder.com/practice/840eee05dccd4ffd8f9433ce8085946b
6+
public class Code_0013_FillFunction {
7+
8+
public int sumOfSubMatrix(int[][] mat, int n) {
9+
return maxSumSubmatrix(mat, n, n);
10+
}
11+
12+
// 求子矩阵的最大累加和,后面的课会讲
13+
public static int maxSumSubmatrix(int[][] mat, int n, int m) {
14+
int max = Integer.MIN_VALUE;
15+
for (int i = 0; i < n; i++) {
16+
// 需要的辅助数组,临时动态生成就可以
17+
int[] arr = new int[m];
18+
for (int j = i; j < n; j++) {
19+
for (int k = 0; k < m; k++) {
20+
arr[k] += mat[j][k];
21+
}
22+
max = Math.max(max, maxSumSubarray(arr, m));
23+
}
24+
}
25+
return max;
26+
}
27+
28+
// 求子数组的最大累加和,后面的课会讲
29+
public static int maxSumSubarray(int[] arr, int m) {
30+
int max = Integer.MIN_VALUE;
31+
int cur = 0;
32+
for (int i = 0; i < m; i++) {
33+
cur += arr[i];
34+
max = Math.max(max, cur);
35+
cur = cur < 0 ? 0 : cur;
36+
}
37+
return max;
38+
}
39+
40+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /