算法 —— 2020年Bilibili秋招后端开发笔试

一、考察内容

30道单选题,3道编程题。单选题相关内容有计网、操作系统(Linux)、数据结构和排序算法、C相关知识等。

二、编程题一

题目内容:给一组含4个数(1-10)的数组,要求输出这四个数经过 加减乘除后 是否能组成二十四点,不能重复使用同一位置的数。

   public static boolean Game24Points (int[] arr) {
        // write code here
        if(arr == null || arr.length == 0)
            return false;
        return dfs(arr,0,0);
    }

    public static boolean dfs(int[] arr,int cur,int target){
        if (target == 24){
            return true;
        }
        if (cur >= 4){
            return  false;
        }
        int curNum = arr[cur];
        return dfs(arr,cur+1,target + curNum)
                || dfs(arr,cur+1,target - curNum)
                    || dfs(arr,cur+1,target * curNum)
                        || dfs(arr,cur+1,target / curNum);
    }
    public static void main(String[] args) {
        int[] in = new int[]{7,2,1,10};
        System.out.println(Game24Points(in));
    }

三、编程题二

题目内容:有效的括号
https://leetcode-cn.com/problems/valid-parentheses/

    public static boolean IsValidExp (String s) {
        // write code here
        if(s == null || s.length() == 0)
            return true;
        int length = s.length();
        Stack<Character> stack = new Stack<>();
        int i = 0;
        while (i < length){
            if (s.charAt(i) == '{' || s.charAt(i) == '['  || s.charAt(i) == '(' ){
                stack.push(s.charAt(i));
            }else{
                if (!stack.isEmpty() && (s.charAt(i) == ')' && stack.peek() == '('
                        || s.charAt(i) == '}' && stack.peek() == '{'
                            || s.charAt(i) == ']' && stack.peek() == '[' )){
                    stack.pop();
                }else {
                    return false;
                }
            }
            i++;
        }
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        System.out.println(IsValidExp("{{{ "));
    }

四、编程题三

题目内容:硬币有4种面值的,分别为1块、4块、16块、64块,纸币面值只有1024块面值的。现在小Y拿着1024块的纸币,买了一件商品花了N (1 <= N <= 1024) 元,请问最少能拿到多少枚硬币?
输入:N为200,输出最少能拿到17枚硬币(12枚64块,3枚16块,2枚4块)。

   public static int GetCoinCount (int N) {
        // write code here
        int rest = 1024 - N;
        int count = 0;
        int[] mon = {1,4,16,64};
        for (int i = mon.length - 1 ; i >= 0 ; i--){
            int cur = rest / mon[i];
            rest -= cur * mon[i];
            count += cur;
        }
        return count;
    }
    public static void main(String[] args) {
        System.out.println(GetCoinCount(200));
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值