文章目录
记一次某大厂笔试
摸牌:
拿道题目就是一通模拟,很快啊,超时了
package letcodeLearn;
import java.util.Deque;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Pro1 {
public static int eval(Deque<Integer> list){
PriorityQueue<Integer> res = new PriorityQueue<>(list);
int count = 0;
while(list.size() != 0){
if(list.peek() == res.peek()){
list.pollFirst();
res.poll();
}else{
int tmp = list.pollFirst();
list.addLast(tmp);
}
count++;
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Deque<Integer> list = new LinkedList<>();
while(n-- > 0){
list.addLast(scanner.nextInt());
}
System.out.println(eval(list));
}
}
通过16%,超时。
排序二叉树求闪电个数:
题目真长,差10秒提交。
package letcodeLearn;
import java.util.*;
public class Pro3 {
public static Node root;
public static int count;
public static int length;
public static void insertBST(int key){
Node p = root;
Node prev = null;
while(p != null){
prev = p;
if(key < p.value){
p = p.left;
}else if(key > p.value){
p = p.right;
}else{
return;
}
}
if(root == null){
root = new Node(key);
}else if (key < prev.value){
prev.left = new Node(key);
}else{
prev.right = new Node(key);
}
}
public static void eval(Node root,int lor,int len){
if(root == null){
return;
}
if(len > length){
length = len;
count=1;
}else if(len == length){
count += 1;
}
if (lor == 1) {
eval(root.right,0,len + 1);
}else{
eval(root.left,1,len + 1);
}
}
public static void t(Node root){
if(root == null) return;
eval(root,0,1);
eval(root,1,1);
t(root.left);
t(root.right);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Integer> list = new ArrayList<>();
while(n-- > 0){
list.add(scanner.nextInt());
}
for (int i = 0; i < list.size(); i++) {
insertBST(list.get(i));
}
t(root);
System.out.print(length+" "+count);
}
}
class Node{
public int value;
public Node left;
public Node right;
Node(){
}
Node(int key){
this.value = key;
this.left = null;
this.right = null;
}
Node(int key,Node left,Node right){
this.value = key;
this.left = left;
this.right = right;
}
}
本地测试通过,没提交上。