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

小红书3.26Java后端笔试全AC分享

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

小红书3.26Java后端笔试全AC分享

第一题:解密字符串,模拟一下即可,每个字符往前数三个


import java.util.*;

public class Main {
private static String getRes(String s) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
sb.append((char) ((ch - 'a' - 3 + 26) % 26 + 'a'));
}
return sb.toString();
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String s = in.next();
System.out.println(getRes(s));
}
}

第二题:K排序,其实就是模拟一下链表的操作。判断每相邻两个整数(n-1和n,如1和2)的相对顺序,如果顺序不对则进行一次操作。最后把这些操作按至多k个一组看能分多少组。


import java.util.*;

public class Main {
private static int getRes(int n, int k, int[] nums) {
Map<Integer, Integer> rec = new HashMap<>();
for (int i = 0; i < n; ++i) rec.put(nums[i], i);
int res = 0;
for (int i = 1; i < n; ++i) if (rec.get(i + 1) < rec.get(i)) rec.put(i + 1, n + (res++));
return res / k + (res % k == 0 ? 0 : 1);
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt(), k = in.nextInt();
int[] nums = new int[n];
for (int i = 0; i < n; ++i) nums[i] = in.nextInt();
System.out.println(getRes(n, k, nums));
}
}
}

第三题:染色。这里本来以为是差分数组或者类似线段树之类的题,结果暴力直接AC??后来就没细想,可以在评论区讨论讨论。


import java.util.*;

public class Main {
private static long[] getRes(int n, long[] nums, int m, int[][] opidxs, String ops, long[] params) {
for (int i = 0; i < m; ++i) {
char op = ops.charAt(i);
int left = opidxs[i][0] - 1, right = opidxs[i][1] - 1;
long param = params[i];
if (op == '|') for (int j = left; j < n && j <= right; ++j) nums[j] = nums[j] | param;
else if (op == '&') for (int j = left; j < n && j <= right; ++j) nums[j] = nums[j] & param;
else for (int j = left; j < n && j <= right; ++j) nums[j] = param;
}
return nums;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[] nums = new long[n];
for (int i = 0; i < n; ++i) nums[i] = in.nextLong();
int m = in.nextInt();
int[][] opidxs = new int[m][2];
for (int i = 0; i < m; ++i) opidxs[i][0] = in.nextInt();
for (int i = 0; i < m; ++i) opidxs[i][1] = in.nextInt();
String ops = in.next();
long[] params = new long[m];
for (int i = 0; i < m; ++i) params[i] = in.nextLong();
long[] res = getRes(n, nums, m, opidxs, ops, params);
for (int i = 0; i < n; ++i) {
System.out.print(res[i]);
if (i != n - 1) System.out.print(' ');
else System.out.print('\n');
}
}
}

#牛客解忧铺##笔试复盘##小红书##小红书24届实习招聘##题解#
 类似资料: