腾讯笔试-后台/综合-【100/100/0/100/40】
电话号码合法性
这题签到题不解释。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for (int i = 0; i < n; i++) {
String line = sc.nextLine();
line = sc.nextLine();
boolean f = isOk(line);
if (f) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
private static boolean isOk(String input) {
if (input.length() < 11) {
return false;
}
if (input.length() == 11 && input.charAt(0) == '8') {
return input.charAt(0) == '8';
}
int i;
for (i = 0; i < input.length() - 1; i++) {
char c = input.charAt(i);
if (c == '8') {
break;
}
}
return input.length() - i >= 11;
}
}
工作的两两匹配时间最少
这题暴力解法,运行了5次,第一次100,第二次50,第三次40,第四次30,第五次100。
哈哈哈,看来我的代码时间复杂度在边缘。只要服务器负载小就能过吧??
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[][] temp = new int[n][2];
int total = 0;
for (int i = 0; i < n; i++) {
String[] line = sc.nextLine().split(" ");
int n0 = Integer.parseInt(line[0]);
total += n0;
int n1 = Integer.parseInt(line[1]);
temp[i][0] = n0;
temp[i][1] = n1;
}
Arrays.sort(temp, Comparator.comparingInt(o -> o[1]));
int[][] a = temp;//
int[][] b = new int[n][2];
int c = 0;
for (int i = n - 1; i >= 0; i--) {
b[c++] = a[i];
}
int ca = 0, cb = 0;
int max = 0;
for (int i = 0; i < total; i++) {
int[] aa = a[ca];
int[] bb = b[cb];
max = Math.max(max, (aa[1] + bb[1]));
if (--aa[0] == 0) {
ca++;
}
if (--bb[0] == 0) {
cb++;
}
}
/*int[] a = new int[total];
int c = 0;
for (int[] m : temp) {
for (int i = 0; i < m[0]; i++) {
a[c++] = m[1];
}
}
Arrays.sort(a);
System.out.println(Arrays.toString(a));
int max = 0;
for (int i = 0; i < a.length / 2; i++) {
max = Math.max(max, (a[i] + a[a.length - i - 1]));
}*/
System.out.println(max);
}
} 战斗力分组
不会
最小非0元素
用ArrayList存,遇到0就删掉,也是边缘时间复杂度,有时候不能100。
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] line = sc.nextLine().split(" ");
int n = Integer.parseInt(line[0]);
int k = Integer.parseInt(line[1]);
line = sc.nextLine().split(" ");
int[] a = new int[n];
ArrayList<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(line[i]);
}
Arrays.sort(a);
for (int m : a) {
list.add(m);
}
print(list, k);
}
private static void print(ArrayList<Integer> list, int k) {
int subSum = 0;
while (k-- > 0) {
if (list.size() <= 0) {
System.out.println(0);
} else {
int m;
while (((m = list.remove(0)) - subSum) <= 0) {
if (list.size() == 0) {
break;
}
}
int sub = m - subSum;
subSum += sub;
System.out.println(sub);
}
}
}
}
异或
过了40,优化不下去了。
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[] a = new int[n];
int[] b = new int[n];
String[] line;
line = sc.nextLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(line[i]);
}
line = sc.nextLine().split(" ");
for (int i = 0; i < n; i++) {
b[i] = Integer.parseInt(line[i]);
}
Arrays.sort(a);
Arrays.sort(b);
int[] na = replace(a);
int[] nb = replace(b);
// System.out.println(Arrays.toString(na));
// System.out.println(Arrays.toString(nb));
int res = 0;
for (int x : na) {
for (int y : nb) {
res ^= (x + y);
}
}
System.out.println(res);
}
private static int[] replace(int[] a) {
int n = a.length;
int zero = 0;
for (int i = 0; i < n - 1; i++) {
int current = a[i];
int next = a[i + 1];
if (current == next) {
a[i] = 0;
a[i + 1] = 0;
i++;
zero += 2;
}
}
int[] na = new int[n - zero];
int c = 0;
for (int m : a) {
if (m != 0) {
na[c++] = m;
}
}
return na;
}
}
OPPO公司福利 1147人发布