拼多多笔试
出去玩回来晚了,也忘了笔试了。。 好像是七点半开始做的吧。 这次很简单,当练手了
1.方形输出
```
public static String answer(String str) {
int k = str.length() / 4;
String res = "";
for(int i = 0; i < k + 1; i++) {
res += str.charAt(i);
}
res += "\r\n";
for(int i = 0; i < k - 1; i++) {
res += str.charAt(str.length() - 1 - i);
for(int j = 0; j < k - 1; j++) {
res += " ";
}
res += str.charAt(k + i + 1);
res += "\r\n";
}
for(int i = 3 * k; i >= 2 * k; i--) {
res += str.charAt(i);
}
return res;
}
```
int k = str.length() / 4;
String res = "";
for(int i = 0; i < k + 1; i++) {
res += str.charAt(i);
}
res += "\r\n";
for(int i = 0; i < k - 1; i++) {
res += str.charAt(str.length() - 1 - i);
for(int j = 0; j < k - 1; j++) {
res += " ";
}
res += str.charAt(k + i + 1);
res += "\r\n";
}
for(int i = 3 * k; i >= 2 * k; i--) {
res += str.charAt(i);
}
return res;
}
```
2.数字分组
```
public static int answer2(String str) {
int res = 0;
for(int i = 1; i < str.length(); i++) {
int a = helper(str.substring(0, i));
int b = helper(str.substring(i));
res += a * b;
}
return res;
}
private static int helper(String s) {
if(s.length() == 1) return 1;
if(s.charAt(0) == '0' && s.charAt(s.length() - 1) == '0') return 0;
if(s.charAt(0) == '0' || s.charAt(s.length() - 1) == '0') return 1;
return s.length();
}
}
int res = 0;
for(int i = 1; i < str.length(); i++) {
int a = helper(str.substring(0, i));
int b = helper(str.substring(i));
res += a * b;
}
return res;
}
private static int helper(String s) {
if(s.length() == 1) return 1;
if(s.charAt(0) == '0' && s.charAt(s.length() - 1) == '0') return 0;
if(s.charAt(0) == '0' || s.charAt(s.length() - 1) == '0') return 1;
return s.length();
}
}
```
3.寻找最熟悉的陌生人
```
public static int answer3(int N, int m, List<List<Integer>> list) {
List<Integer> parentList = list.get(m);
int[] arr = new int[N];
for(int i = 0; i < N; i++) {
if(i != m && !parentList.contains(i)) {
arr[i] = help(list.get(i), parentList);
}
}
int result = -1;
int max = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] > max && i != m) {
result = i;
max = arr[i];
}
}
if(max == 0) {
return -1;
}
return result;
}
public static int help(List<Integer> list1, List<Integer> list2) {
Set<Integer> set = new HashSet<>();
for(int i = 0; i < list1.size(); i++) {
set.add(list1.get(i));
}
int res = 0;
for(Integer x : list2){
if(set.contains(x)) res++;
}
return res;
}
List<Integer> parentList = list.get(m);
int[] arr = new int[N];
for(int i = 0; i < N; i++) {
if(i != m && !parentList.contains(i)) {
arr[i] = help(list.get(i), parentList);
}
}
int result = -1;
int max = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] > max && i != m) {
result = i;
max = arr[i];
}
}
if(max == 0) {
return -1;
}
return result;
}
public static int help(List<Integer> list1, List<Integer> list2) {
Set<Integer> set = new HashSet<>();
for(int i = 0; i < list1.size(); i++) {
set.add(list1.get(i));
}
int res = 0;
for(Integer x : list2){
if(set.contains(x)) res++;
}
return res;
}
```
4.没时间做了,还二十来分钟,看了十分钟题都没看懂,放弃了
#拼多多##笔试题目##秋招##内推#