JAVA有赞2021校招数据笔试(B卷)——都是牛客原题

Posted 爱做梦的鱼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA有赞2021校招数据笔试(B卷)——都是牛客原题相关的知识,希望对你有一定的参考价值。

目录

前言

这次的笔试题都是牛客原题哦,祝兄弟们都有心仪的offer

一、LRU(全A)

代码

package youzan;
package youzan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/11
 * @Time: 15:34
 * @Version: 1.0
 * @Description: Description
 */
public class First 
    public static void main(String[] args) 
        int[][] matrix = new int[][]1, 1, 1, 1, 2, 2, 1, 3, 2, 2, 1, 1, 4, 4, 2, 2;
        System.out.println(Arrays.toString(LRU(matrix, 3)));
    

    public static int[] LRU(int[][] operators, int k) 
        // write code here
        LinkedHashMap<Integer, Integer> lruMap = new LinkedHashMap<>();
        ArrayList<Integer> res = new ArrayList<>();

        for (int[] oper : operators) 
            int key = oper[1];
            switch (oper[0]) 
                case 1:
                    int value = oper[2];
                    if (lruMap.size() < k) 
                        lruMap.put(key, value);
                     else 
                        Iterator it = lruMap.keySet().iterator();
                        lruMap.remove(it.next());
                        lruMap.put(key, value);
                    
                    break;
                case 2:
                    if (lruMap.containsKey(key)) 
                        int val = lruMap.get(key);
                        res.add(val);
                        lruMap.remove(key);

                        lruMap.put(key, val);
                     else 
                        res.add(-1);
                    
                    break;
            
        
        int[] resArr = new int[res.size()];
        for (int i = 0; i < res.size(); i++) 
            resArr[i] = res.get(i);
        
        return resArr;
    



二、合法括号(全A)

代码

package youzan;

import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/11
 * @Time: 15:19
 * @Version: 1.0
 * @Description: Description
 */
public class Second 
    public static void main(String[] args) 
//        System.out.println(isVaild("123"));
        System.out.println(isVaild("(123)"));
    

    public static boolean isVaild(String statement) 
        int len = statement.length();
        /*if ((len & 1) == 1) 
            return false;
        */
        Map<Character, Character> pairs = new HashMap<Character, Character>();
        pairs.put(')', '(');
        pairs.put(']', '[');
        pairs.put('', '');

        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < len; i++) 
            char ch = statement.charAt(i);
            if (pairs.containsKey(ch)) 
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) 
                    return false;
                
                stack.pop();
             else if (ch == '(' || ch == '[' || ch == '') 
                stack.push(ch);
            
        
        return stack.isEmpty();
    


以上是关于JAVA有赞2021校招数据笔试(B卷)——都是牛客原题的主要内容,如果未能解决你的问题,请参考以下文章

JAVA360-2021校招笔试-技术综合A卷-0911

题解百度2021校招Web前端工程师笔试卷(第一批):单选题多选题

题解百度2020校招Web前端工程师笔试卷(第一批):单选题多选题

JAVA滴滴-2021校招在线笔试-DE数据开发试卷-0913

JAVA滴滴-2021校招在线笔试-DE数据开发试卷-0913

百度 | 2021校招C++研发工程师笔试详解