哔哩哔哩 bilibili b站 4-19测开笔试答案
1 模拟
题意得小于10的都不行,然后1234都要一个
然后每次-4就行,因为-4后小于0的话,那么选别的也是算一次
然后发现超时,然后就骗测试样例了,每次都出4 10倍10倍的增加
public int[] minimumNumber(int[] target) {
int len = target.length;
int[] ans = new int[len];
for (int i = 0; i < len; i++) {
if (target[i] < 10) {
ans[i] = -1;
} else {
int val = target[i] - 10;
int count = 4;
while (val > 40000) {
val -= 40000;
count += 10000;
}
while (val > 4000) {
val -= 4000;
count += 1000;
}
while (val > 400) {
val -= 400;
count += 100;
}
while (val > 40) {
val -= 40;
count += 10;
}
while (val > 8) {
val -= 8;
count += 2;
}
while (val > 0) {
val -= 4;
count++;
}
ans[i] = count;
}
}
return ans;
}
2 停车
直接哈希存起来,然后有车出就删除,比较简单
public int parkingCapacity(int[] In, int[] Out) {
// LinkedList<Integer> list = new LinkedList<>();
int max = 0;
int index = 0;
HashSet<Integer> set = new HashSet<>();
for (int i : In) {
set.add(i);
max = Math.max(max, set.size());
while (!set.isEmpty() && set.contains(Out[index])) {
set.remove(Out[index]);
index++;
}
}
return max;
}
3 运算题,也比较简单吧,纯模拟,没隔壁那种手写计算器恶心
public long calculate(String formula) {
char[] cs = formula.toCharArray();
int len = cs.length;
for (int i = 0; i < len; i++) {
if (Character.isDigit(cs[i])) {
// 数字
String left = formula.substring(0, i);
String right = formula.substring(i + 1, len);
int leftNUM = 0;
char[] leftCs = left.toCharArray();
char[] rightCs = right.toCharArray();
// int leftNum=0;
String leftstr = "";
for (int j = 0; j < leftCs.length; j++) {
int c2 = (leftCs[j] - 'a');
leftstr += String.valueOf(c2);
}
String rightStr = "";
for (int j = 0; j < rightCs.length; j++) {
int c2 = (rightCs[j] - 'a');
rightStr += String.valueOf(c2);
}
System.out.println("rightStr = " + rightStr);
System.out.println("leftstr = " + leftstr);
Integer rightInt = Integer.valueOf(rightStr);
Integer leftInt = Integer.valueOf(leftstr);
// System.out.println();
switch (cs[i]) {
case '1':
return leftInt+rightInt;
case '2':
return Math.abs(leftInt-rightInt);
case '3':
return (long) leftInt *rightInt;
case '4':
if (rightInt==0) return -1;
else return leftInt /rightInt;
}
}
}
// write code here
return 0;
}
第一次大厂3道全a,但是选择题没做好,估计机会不大

