字节跳动笔试-过前三道,最后一道题目都没看明白。。。
第一道,刚开始也是只过了80%,最后要提交的时候灵机一动,题目说了肯定有答案,那么第一个肯定是答案。。。然后就全A了。。。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] time = new int[n][2];
int[] times = new int[n];
for (int i = 0; i < n; i++) {
time[i][0] = sc.nextInt();
time[i][1] = sc.nextInt();
times[i] = 60 * time[i][0] + time[i][1];
}
int m = sc.nextInt();
int hour = sc.nextInt();
int minute = sc.nextInt();
int startTime = 60 * hour + minute;
for (int i = n - 1; i >= 0; i--) {
if (times[i] + m <= startTime) {
System.out.println(time[i][0] + " " + time[i][1]);
return;
}
}
System.out.println(time[0][0] + " " + time[0][1]);
} 第二题, public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int X = sc.nextInt();
String string = sc.next();
if (X == 0) {
System.out.println(string);
return;
}
int[] num = new int[string.length()];
for (int i = 0; i < string.length(); i++) {
num[i] = string.charAt(i) - '0';
}
int[] ret = new int[N];
ret[0] = num[0];
for (int i = 1; i < N; i++) {
// 这个时候不是由X个数进行异或生成的
if (i < X) {
ret[i] = num[i] ^ num[i - 1];
} else {
int t = X - 1;
int temp = num[i];
while (t > 0) {
temp = temp ^ ret[i - t];
t--;
}
ret[i] = temp;
}
}
for (int i = 0; i < N; i++) {
System.out.print(ret[i]);
}
System.out.println("");
} 第三道题目,原型应该是LeetCode上分糖果的题目,
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
if (n == 0) {
System.out.println(0);
}
if (n == 1) {
System.out.println(100);
}
int result = f(array, n);
System.out.println(result);
}
public static int f(int[] array, int n) {
int nums[] = new int[n];
nums[0] = 1;
for (int i = 1; i < n; i++) {
if (array[i] > array[i - 1]) {
nums[i] = nums[i - 1] + 1;
} else {
nums[i] = 1;
}
}
for (int i = n - 2; i >= 0; i--) {
if (array[i] > array[i + 1] && nums[i] <= nums[i + 1]) {
nums[i] = nums[i + 1] + 1;
}
}
int count = 0;
for (int i = 0; i < n; i++) {
count += nums[i];
}
return count * 100;
} 最后一道,题目都没看明白,感觉题目也说的不清楚,求大佬说说第四题。
