华为笔试9.9 AC3 Java
1. 完美排列 第一题靠运气过的,我反复提交了将近一个小时,出了一次AC,别的一直各种10%,20%,30%,50%,60%,90%…
#笔经##华为#
import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int [][] prefect = new int [n][2]; for(int i =0;i<n*2;i++) { if (i < n ) { prefect[i][0] = sc.nextInt(); } else { prefect[i-n][1] = sc.nextInt(); } } int m = sc.nextInt(); int [][] temp = new int [m][2]; Queue<Integer> queue = new LinkedList<>(); for(int i =0;i<m*2;i++){ if(i < m){ temp[i][0] = sc.nextInt(); }else{ temp[i-m][1] = sc.nextInt(); } } for(int i=0;i<m-prefect.length;i++){ if(temp[i][0] == prefect[0][0]&&temp[i][1] == prefect[0][1]){ queue.add(i); } } int result = 0; while(!queue.isEmpty()){ int position = queue.poll(); result = position+1; boolean flag = false; int index = 0; for(int i = position;i<m;i++){ if(index==prefect.length){ flag = true; break; } else if(temp[i][0] == prefect[index][0]&&temp[i][1] == prefect[index][1]){ index++; }else{ result = 0; break; } } if(flag){ break; } } System.out.println(result); } }
2.最长水沟
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int [][] map = new int [n][m]; for(int i = 0;i<n;i++){ for(int j =0;j<m;j++){ map[i][j] = sc.nextInt(); } } int result = 0; for(int i = 0;i<n;i++){ for(int j =0;j<m;j++){ int temp = track(map,i,j,Integer.MAX_VALUE,0); if(temp>result){ result = temp; } } } System.out.println(result); } public static int track(int [][] map,int x,int y,int ori,int result){ if(x<0||x>=map.length||y<0||y>=map[0].length){ return result; } if(map[x][y]>=ori){ return result; } result++; return Math.max(Math.max(track(map,x-1,y,map[x][y],result),track(map,x+1,y,map[x][y],result)),Math.max(track(map,x,y-1,map[x][y],result),track(map,x,y+1,map[x][y],result))); } }3.树最大异或路径
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); } }