当前位置: 首页 > 面试经验 >

水滴笔试Java

优质
小牛编辑
157浏览
2023-03-28

水滴笔试Java

3.23笔试 各位大佬都a了几道啊 只a了第一道后面两道只过了一半

Java-B 20到单选 5到多选 3到编程


public static int countSubStr (String s) {
// write code here
//给定一个字符串s(只包含0-9)任意子串的不同数字的个数大于单个数字出现的频率就结果加一
int res = 0;
int[] arr = new int[10];
for (int i = 0; i < s.length(); i++) {
for (int j = i; j < s.length(); j++) {
int dif_times = 0;
int same_times = 0;
String sub = s.substring(i,j+1);
for (int k = 0; k < sub.length(); k++) {
arr[sub.charAt(k)-48]++;
}
for (int k = 0; k < arr.length; k++) {
if (arr[k] != 0)dif_times++;
same_times = Math.max(same_times,arr[k]);
}
if (same_times < dif_times){res++;
System.out.println(sub + "dif" + dif_times+"sam "+same_times);}
Arrays.fill(arr,0);
}
}
return res;
}

public static boolean canDefeatAllMonsters (int initialAttack, int[] monsterHealths, int[] monsterPowers) {
// write code here
//初始攻击力为initialAttack 每次可以攻击monsterHealths扣除数组每一个怪兽的血量,存活下来的怪兽取攻击最小的monsterPowers中对应的power,会扣除到initialAttack玩家攻击力上,判断能否打败所有怪兽
boolean res = false;
while (initialAttack > 0){
int index = 0;
for (int i = 0; i < monsterHealths.length; i++) {
if (monsterHealths[i] > 0){
index++;
}
}
if (index != 0){
for (int i = 0; i < monsterHealths.length; i++) {
monsterHealths[i]-=initialAttack;
}
int power = 1000;
for (int i = 0; i < monsterPowers.length; i++) {
if (monsterHealths[i] > 0){
power = Math.min(power,monsterPowers[i]);
}
}
initialAttack-=power;
}else {
res = true;
break;
}
}
return res;
}

public static char[][] simulatingGravity (char[][] mesh) {
// write code here
//模拟重力 o就是苹果然后 #就是阻挡物 .是空白的 只要一列数组o的下面是.苹果就会下落,求最后的结果
int row = mesh.length;
int col = mesh[0].length;
for (int i = 0; i < row-1; i++) {
for (int j = 0; j < col; j++) {
if (mesh[i][j] == 'o'){
int temp = i;
mesh[i][j] = '.';
while (++temp < row){
if (mesh[temp][j] == '#'){
mesh[temp-1][j] = 'o';
break;
}else if (temp == row-1){
mesh[row-1][j] = 'o';
break;
}
}
}
}
}
return mesh;
}
#软件开发2023笔面经##笔试##水滴##投票#
 类似资料: