9.7 途虎养车JAVA笔试
编程题一,求m号顾客排队等待时间,HashMap+队列模拟过了100%
public int timeRequiredToBuyCounpons (int[] coupons, int m) { // write code here LinkedList<Integer> list=new LinkedList<>(); for(int i=0;i<coupons.length;i++)list.add(i); Map<Integer,Integer> map=new HashMap<>(); for(int i=0;i<coupons.length;i++)map.put(i,coupons[i]); int time=0; while(map.get(m)!=0){ int index=list.poll(); map.put(index,map.get(index)-1); time++; if(map.get(index)!=0)list.add(index); } return 10*time; }编程题二,求所有祖父节点(父节点的父节点)为奇数的节点值的和,暴力过100%
int res=0; public int sum (TreeNode root) { if(root==null)return 0; if(root.val%2==1)getSum(root); if(root.left!=null)sum(root.left); if(root.right!=null)sum(root.right); // write code here return res; } public void getSum(TreeNode root){ TreeNode left=root.left; TreeNode right=root.right; if(left!=null){ if(left.left!=null)res+=left.left.val; if(left.right!=null)res+=left.right.val; } if(right!=null){ if(right.left!=null)res+=right.left.val; if(right.right!=null)res+=right.right.val; } }编程题三,求车队喷漆的最小开销,回溯会超时,改完只过了33%这里就不放代码了,希望有大佬能给个正解
算法题还是很容易的,选择题盲区比较多,希望能有机会进面试吧
#笔试#