ARTS-第46周

ARTS (第46周)

要有目标,才会有动力。

Algorithm 算法

树的子结构

1
2
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
https://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88?tpId=13&tqId=11170&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

遍历递归判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 树的子结构
// 输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
public boolean HasSubtree(TreeNode root1, TreeNode root2) {
if (root1 == null || root2 == null) {
return false;
}
if (HasSubtreeHelp(root1, root2)) {
return true;
}
return HasSubtree(root1.left, root2) || HasSubtree(root1.right, root2);
}

// 辅助方法
private boolean HasSubtreeHelp(TreeNode root1, TreeNode root2) {
if (root2 == null) {
return true;
}
if (root1 == null) {
return false;
}
if (root1.val != root2.val) {
return false;
}
return HasSubtreeHelp(root1.left, root2.left) && HasSubtreeHelp(root1.right, root2.right);
}

二叉树的镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5
https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

递归交换子节点即可

1
2
3
4
5
6
7
8
9
10
11
12
13
public void Mirror(TreeNode root) {
if(root==null)
return;
swapChild(root);
Mirror(root.left);
Mirror(root.right);
}

private void swapChild(TreeNode root) {
TreeNode temp = root.left;
root.left=root.right;
root.right=temp;
}

顺时针打印矩阵

1
2
3
题目描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
https://www.nowcoder.com/practice/9b4c81a02cd34f76be2659fa0d54342a?tpId=13&tqId=11172&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.ArrayList;
public class Solution {
// 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11
// 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
public ArrayList<Integer> printMatrix(int[][] matrix) {
if (matrix == null || matrix.length == 0)
return null;
if (matrix[0] == null || matrix[0].length == 0)
return null;

ArrayList<Integer> r = new ArrayList<Integer>();
// 上限
int w = matrix[0].length - 1;
int h = matrix.length - 1;
// 下限
int x = 0;
int y = 0;
while (x <= w &&y <= h) {
// 从左到右
for (int i = x; i <= w &&y <= h; i++) {
r.add(matrix[y][i]);
}
y++;
// 从上到下
for (int i = y; i <= h&&x <= w; i++) {
r.add(matrix[i][w]);
}
w--;
// 从右到左
for (int i = w; i >= x &&y <= h; i--) {
r.add(matrix[h][i]);
}
h--;
// 从下到上
for (int i = h; i >= y&&x <= w; i--) {
r.add(matrix[i][x]);
}
x++;
}

return r;
}

}

包含min函数的栈

1
2
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
注意:保证测试中不会当栈为空的时候,对栈调用pop()或者min()或者top()方法。

双栈法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


import java.util.Stack;

public class SolutionStack1 {



Stack<Integer> stack=new Stack<Integer>();
Stack<Integer> stackMin=new Stack<Integer>();

public void push(int node) {
stack.push(node);
if(stackMin.isEmpty() ||stackMin.peek()==null||Integer.valueOf(stackMin.peek())>=node ){
stackMin.push(node);
}
}

public void pop() {

Integer val = stack.pop();
if(val!=null && Integer.valueOf(val)==Integer.valueOf(stackMin.peek())){
stackMin.pop();
}
}

public int top() {
return stack.peek();
}

public int min() {
return stackMin.peek();
}

@Override
public String toString() {
return "{\"stack\":" + stack + ", \"stackMin\":" + stackMin + "}";
}

// 简易链表
// class LinkNode{
// int val;
// LinkNode next;
// }
}

节省空间版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Stack;

public class Solution {


Stack<Integer> stack = new Stack<Integer>();
int min = 0;

public void push(int node) {
if (stack.isEmpty()) {
min = node;
stack.push(0);
} else {
// 后续 通过min的差值还原真正的值
int v = min - node;
stack.push(v);
min = v > 0 ? node : min;

}

}

public void pop() {
Integer val = stack.pop();
if(val>0){
//需要替换最小值的时候
min = val + min;
}
}

public int top() {
Integer v=stack.peek();
//还原出上一个min
int min = this.min;
if(v>0){
min = v + min;
}
//通过差值
return min-v;
}

public int min() {
return min;
}
}

栈的压入、弹出序列

1
2
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&tqId=11174&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

解法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class Solution {
//栈的压入、弹出序列
public boolean IsPopOrder(int [] pushA,int [] popA) {
if(pushA==null ||popA==null||pushA.length == 0 || popA.length == 0) {
return false;
}
//pop的索引
int p=0;
//模拟一个栈
ArrayList<Integer> stack = new ArrayList<Integer>();
for (int i = 0; i < pushA.length; i++) {
stack.add(pushA[i]);
while((!stack.isEmpty()) && stack.get(stack.size()-1) == popA[p] ) {
//如果和pop序列的当前位置相同就弹出
stack.remove(stack.size()-1);
p++;
}
}
//空了 就代表全部弹出
return stack.isEmpty();
}
}

Review 英文文章

http://kafka.apache.org/quickstart

kafka 的helloword

Tip 技巧

以下针对不同版本的Linux系统检查防火墙的状态,及关闭防火墙:


Ubuntu(ubuntu-12.04-desktop-amd64)

查看防火墙状态:ufw status

关闭防火墙:ufw disable


centos6.0

查看防火墙状态:service iptables status

关闭防火墙:chkconfig iptables off #开机不启动防火墙服务


centos7.0(默认是使用firewall作为防火墙,如若未改为iptables防火墙,使用以下命令查看和关闭防火墙)

查看防火墙状态:firewall-cmd –state

关闭防火墙:systemctl stop firewalld.service
————————————————
版权声明:本文为CSDN博主「光于前裕于后」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dr_guo/article/details/51658559

Share 分享

https://blog.csdn.net/dr_guo/article/details/51658559 该文章是介绍zk启动的端口问题的,我在配置的时候,是集群直接无法通信,后来经这个文章发现是防火墙的问题。