腾讯4.26笔试AC代码分享(1,4题)

/**
* 1.模拟队列
* @param args
*/
public static void main(String args[]){
    Scanner in = new Scanner(System.in);
    LinkedList<String> queue = new LinkedList<>();
    int T = in.nextInt();
    for(int i = 0; i < T; i++){
        int Q = in.nextInt();
        for(int j = 0; j < Q; j++){
            String opt = in.next();
            switch(opt){
                case "PUSH":
                    String x = in.next();
                    queue.add("" + x);
                    break;
                case "POP":
                    if(queue.size() != 0){
                        queue.pop();
                    } else {
                        System.out.println("-1");
                    }
                    break;
                case "TOP":
                    if(queue.size() != 0){
                        System.out.println(queue.peek());
                    } else {
                        System.out.println("-1");
                    }
                    break;
                case "SIZE":
                    System.out.println(queue.size());
                    break;
                case "CLEAR":
                    queue.clear();
                    break;
                default:
                    break;
            }
        }
        queue.clear();
    }
}

/**
* 两个栈实现队列
* @param args
*/
public static void main(String args[]){
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    LinkedList<String> queue = new LinkedList<>();
    for(int i = 0; i < N; i++){
        String opt = in.next();
        switch(opt){
            case "add":
                String x = in.next();
                queue.add("" + x);
                break;
            case "poll":
                queue.pop();
                break;
            case "peek":
                System.out.println(queue.peek());
                break;
            default:
                break;
        }
    }
}
诶,太菜了。只AC了两道。而且第2道实现方法还很猥琐2333。#腾讯暑期实习##腾讯##笔试题目#
全部评论

相关推荐

评论
点赞
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务