前言
华为三道题,100+200+300,100及格,大家做对第一题就好了,祝大家全都有心仪的offer,不要慌,不要焦虑。
一、完美排列
package huawei0909;
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
*
* @Author:
* @Email:
* @Date: 2020/9/9
* @Time: 19:04
* @Version: 1.0
* @Description: Description
*/
public class First {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int K = sc.nextInt();
int[] perArr = new int[K];
int[] perArr1 = new int[K];
for (int i = 0; i < K; i++)
perArr[i] = sc.nextInt();
for (int i = 0; i < K; i++)
perArr1[i] = sc.nextInt();
int n = sc.nextInt();
int[] arr = new int[n];
int[] arr1 = new int[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextInt();
for (int i = 0; i < n; i++)
arr1[i] = sc.nextInt();
sc.close();
/*if (n<K){ //必须判断,不然A0.6或0.7,因为题目中说:如果不是完美排列,则输出0,详情看下面的System.out.println(i + 1);
System.out.println(0);
return;
}*/
for (int i = 0; i < n; i++) {
if (arr[i] == perArr[0] && arr1[i] == perArr1[0] && i + K - 1 < n && arr[i + K - 1] == perArr[K - 1] && arr1[i + K - 1] == perArr1[K - 1]) {
boolean flag = true;
int index = i;
for (int j = 1; j < K - 1; j++) {
index++;
if (!(arr[index] == perArr[j] && arr1[index] == perArr1[j])) {
flag = false;
break;
}
}
if (flag) { //输出可能为0,如果没考虑到,则A0.6或0.7,因为题目中说:如果不是完美排列,则输出0
System.out.println(i + 1);
return;
}
}
}
System.out.println(0); //必须有,不然A0.6或0.7,因为题目中说:如果不是完美排列,则输出0,详情看下面的System.out.println(i + 1);
}
}
二、最长的水沟
package huawei0909;
import java.util.Scanner;
/**
* Created by IntelliJ IDEA.
*
* @Author:
* @Email:
* @Date: 2020/9/9
* @Time: 19:36
* @Version: 1.0
* @Description: Description
*/
public class Second {
public static int[][] matrix;
public static int[][] dp;
public static int[][] k = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
public static int n, m, ans;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
matrix = new int[n + 1][m + 1];
dp = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
matrix[i][j] = sc.nextInt();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
ans = Math.max(ans, dfs(i, j));
System.out.println(ans + 1);
}
public static int dfs(int x, int y) {
if (dp[x][y] != 0)
return dp[x][y];
for (int i = 0; i <= 3; i++) {
int tx = x + k[i][0];
int ty = y + k[i][1];
if (!(tx < 1 || ty < 1 || tx > n || ty > m || matrix[tx][ty] >= matrix[x][y]))
dp[x][y] = Math.max(dp[x][y], 1 + dfs(tx, ty));
}
return dp[x][y];
}
}
三、树最大异或路径
import java.util.HashMap;
import java.util.Scanner;
public class Main {
static int result = 0;
public static class TreeNode{
int id;
int value;
int left_id;
int right_id;
TreeNode left;
TreeNode right;
public TreeNode() {
}
public TreeNode(int id, int value) {
this.id = id;
this.value = value;
}
public TreeNode(int id, int value, int left_id, int right_id) {
this.id = id;
this.value = value;
this.left_id = left_id;
this.right_id = right_id;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
HashMap<Integer,TreeNode> map = new HashMap<>();
for(int i =0;i<n;i++){
int node_id = sc.nextInt();
int node_value = sc.nextInt();
int left_id = sc.nextInt();
int right_id = sc.nextInt();
TreeNode treeNode = new TreeNode(node_id,node_value,left_id,right_id);
map.put(node_id,treeNode);
}
for(Integer key:map.keySet()){
TreeNode treeNode = map.get(key);
if(treeNode.left_id!=-1){
treeNode.left = map.get(treeNode.left_id);
}
if(treeNode.right_id!=-1){
treeNode.right = map.get(treeNode.right_id);
}
}
for(Integer key:map.keySet()){
TreeNode treeNode = map.get(key);
dfs(treeNode,0);
}
System.out.println(result);
}
public static void dfs(TreeNode node,int value){
if(node==null){
return;
}
value = value^node.value;
if(value>result){
result = value;
}
dfs(node.left,value);
dfs(node.right,value);
}
}
感谢大家看到这里,文章有不足,欢迎大家指出;如果你觉得写得不错,那就给我一个赞吧。
我这边也整理了很多面试的文档笔记。 也还整理了一些最新2023收集的一些大厂的面试真题(都整理成文档,小部分截图)。
上述的面试题答案都整理成文档笔记。有需要的伙伴可以点赞支持我一下。