2023秋招笔试

柠檬微趣

  1. 将java的链表升序排序,链表用Class Node{int val,Node next}实现
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * 输入一串数字,放入list中,实现sortList,返回升序的list
 */
public class Test1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Node head = new Node();
        head.value = in.nextInt();
        head.next = null;
        Node temp = head;
        for(int i=0;i<8;i++){
            Node node = new Node();
            node.value = in.nextInt();
            node.next = null;
            temp.next = node;
            temp = temp.next;
        }
        head = sortList(head);
        System.out.print(head.value);
        head = head.next;
        while(head!=null){
            System.out.print(" "+head.value);
            head = head.next;
        }
    }

    public static Node sortList(Node head){
        PriorityQueue<Node> queue = new PriorityQueue<>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o1.value-o2.value;
            }
        });
        while(head!=null){
            queue.offer(head);
            head = head.next;
        }
        head = queue.poll();
        Node temp = head;
        while(!queue.isEmpty()){
            temp.next = queue.poll();
            temp = temp.next;
        }
        temp.next = null;
        return head;
    }
}

class Node{
    int value;
    Node next;
}
  1. 实现简单的模式匹配
    • .匹配任意字符
    • *和前面一个字符组合,表示匹配0个或多个该字符
    • ?和前面一个字符组合,表示匹配1个或多个该字符
import java.util.Scanner;

/**
 * 实现简单的模式匹配
 * . 匹配任意字符
 * * 和前面一个字符组合,表示匹配0个或多个该字符
 * ? 和前面一个字符组合,表示匹配1个或多个该字符
 *
 * 示例:
 *  输入:
 *      3
 *      aa aa
 *      aa aaa
 *      aaa aa
 *  输出:
 *      true
 *      false
 *      false
 *
 * 示例:
 *  输入:
 *      4
 *      a a.*
 *      aa aa.?
 *      aa a.?
 *      ab a.?
 *  输出:
 *      true
 *      false
 *      true
 *      true
 */
public class Test2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();

        for (int i = 0; i < n; i++) {
            char[] aChars = in.next().toCharArray();
            char[] bChars = in.next().toCharArray();
            int len = bChars.length;
            //k用于标记aChars的下标
            int k = 0;
            //j用于标记bChars的下标
            int j = 0;
            boolean end = false;
            boolean finish = false;
            int count = 0;
            while(true){
                if(j+1<len){
                    switch (bChars[j+1]){
                        case '*':{
                            count = 0;
                            while(bChars[j]=='.'||aChars[k]==bChars[j]){
                                k++;
                                count++;
                                if(k>=aChars.length){
                                    end = true;
                                    break;
                                }
                            }
                            k++;
                            j+=2;
                            break;
                        }
                        case '?':{
                            count = 0;
                            while(bChars[j]=='.'||aChars[k]==bChars[j]){
                                k++;
                                count++;
                                if(k>=aChars.length){
                                    end = true;
                                    break;
                                }
                            }
                            if(count>0){
                                k++;
                                j+=2;
                            }
                            else{
                                finish = true;
                            }
                            break;
                        }
                        default:{
                            if(bChars[j]=='.'){
                                j++;
                                k++;
                            }
                            else{
                                if(aChars[k]==bChars[j]){
                                    j++;
                                    k++;
                                }
                                else{
                                    finish = true;
                                }
                            }
                            break;
                        }
                    }

                }
                else{
                    if(bChars[j]=='.'||bChars[j]==aChars[k]){
                        j++;
                        k++;
                    }
                    else{
                        finish = true;
                    }
                }
                if(k>=aChars.length||j>=bChars.length){
                    end = true;
                }
                if(finish||end){
                    break;
                }
            }
            if(finish){
                System.out.println(false);
            }
            else{
                if(k<aChars.length){
                    System.out.println(false);
                }
                else if(j<bChars.length){
                    if((bChars.length-j)%2==0){
                        boolean flag = false;
                        while(bChars.length-j>0){
                            if(!(bChars[j]=='.'&&bChars[j+1]=='*')){
                                flag = true;
                                System.out.println(false);
                                break;
                            }
                            else{
                                j+=2;
                            }
                        }
                        if(!flag){
                            System.out.println(true);
                        }
                    }
                    else{
                        System.out.println(false);
                    }
                }
                else{
                    System.out.println(true);
                }
            }
        }
    }
}


  1. 野猪骑士?翻译过来就是:给一个数组,数组中的第i个元素,需要输出:数组下标比i大的集合中,值也比array[i]大的集合中的最小值。
import java.util.*;

/**
 * 野猪骑士?
 * 给一个数组,数组中的第i个元素,需要输出:数组下标比i大的集合中,值也比array[i]大的集合中的最小值。
 *
 * 示例:
 *  输入:
 *      4 2 1 3
 *  输出:
 *      -1 3 3 -1
 *
 * 示例:
 *  输入:
 *      8 4 9 3 5 1 7 2 6
 *  输出:
 *      9 5 -1 5 6 2 -1 6 -1
 */
public class Test3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        List<Integer> list = new ArrayList<>();
        while(in.hasNextInt()){
            list.add(in.nextInt());
        }
        int[] array = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        int[] result = new int[list.size()];
        for (int i = list.size()-1; i >= 0; i--) {
            Stack<Integer> stack = new Stack<>();
            while(!queue.isEmpty()){
                if(queue.peek()<array[i]){
                    stack.add(queue.poll());
                }
                else {
                    result[i] = queue.peek();
                    break;
                }
            }
            while(!stack.isEmpty()){
                queue.add(stack.pop());
            }
            if(result[i]==0){
                result[i]=-1;
            }
            queue.add(array[i]);
        }
        StringBuilder sb = new StringBuilder();
        sb.append(result[0]);
        for (int i = 1; i < list.size(); i++) {
            sb.append(" "+result[i]);
        }
        System.out.println(sb.toString());
    }
}

  1. 将一个整数转换为一个7bit的数据格式(没细看)

你可能感兴趣的:(秋招,笔试)