58笔试 简单回忆版
作者:
福尔摩东
,
2022-03-12 20:46:11
,
所有人可见
,
阅读 204
58笔试 简单回忆版 (2022/03/12 20:30结束 三道easy题,第二题我傻逼了没交上去。。。)
1.最长不重复数组
int solve(int[] arr) {
Queue<Integer> queue = new LinkedList<>();
int ans = 0;
for (int i = 0; i < arr.length; i++) {
queue.offer(arr[i]);
while (queue.contains(arr[i])) {
queue.poll();
}
ans = Math.max(queue.size(), ans);
}
return ans;
}
在一堆里找不重复且平均值相同的数组
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
HashSet<Integer> set = new HashSet<>();
boolean[] st;
public ArrayList<ArrayList<Integer>> process (int[] allScoreArray, int examCount, int lastScore) {
// write code here
st = new boolean[allScoreArray.length];
for(int i = 1; i < allScoreArray.length; i++){
if(allScoreArray[i] == allScoreArray[i-1]){
st[i] = true;
}
}
Arrays.sort(allScoreArray);
dfs(allScoreArray,examCount,lastScore,0);
return list;
}
ArrayList<Integer> tmp = new ArrayList<>();
void dfs(int[] allScoreArray, int examCount, int la,int n){
if(tmp.size()!= 0 && n%tmp.size()!=0){
return;
}
if(tmp.size() == examCount){
if(n%examCount== 0 && tmp.get(examCount-1) == la ){
ArrayList<Integer> t = new ArrayList<>(tmp);
list.add(t);
return;
}
}
int cnt = 0;
for(Integer i:allScoreArray){
if(!st[cnt++] && !set.contains(i)){
set.add(i);
tmp.add(i);
dfs(allScoreArray,examCount,la,n+i);
set.remove(i);
tmp.remove(i);
}
}
}
跳箱子升级版
int solve(int k) {
int[] dp = new int[50];
dp[1] = 1;
for (int i = 1; i <= 40; i++) {
if (i % 2 == 0) {
dp[i + 1] = dp[i] + 1;
dp[i + 2] = dp[i] + 2;
} else {
dp[i + 1] = dp[i] + 1;
dp[i + 3] = dp[i] + 1;
}
}
return dp[k];
}