拼多多笔试

This commit is contained in:
xiongraorao 2018-08-05 21:05:37 +08:00
parent f2599feb9d
commit 9cf4ad1ced
7 changed files with 352 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.raorao.interview.pdd;
/**
* 去卡片.
*
* 题目描述
*
* @author Xiong Raorao
* @since 2018-08-05-20:40
*/
public class Carts {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,75 @@
package com.raorao.interview.pdd;
import java.util.ArrayList;
import java.util.Scanner;
/**
* .
*
* @author Xiong Raorao
* @since 2018-08-05-20:03
*/
public class Friends {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String[] input = scanner.nextLine().split(" ");
int userNum = Integer.parseInt(input[0]);
int targetUser = Integer.parseInt(input[1]);
int[][] relation = new int[userNum][userNum];
for (int i = 0; i < userNum && scanner.hasNextLine(); i++) {
String ss = scanner.nextLine();
String[] users = ss.split(" ");
for (String s : users) {
int user = Integer.parseInt(s);
relation[i][user] = 1;
relation[i][i] = 1;
}
//System.out.println(" line " + i );
}
//System.out.println(" out: ");
System.out.println(process(relation, targetUser, userNum));
}
}
private static int process(int[][] relation, int targetUser, int userNum) {
// 判断最可能的好友
ArrayList<Integer> stranger = new ArrayList<>();
for (int i = 0; i < userNum; i++) {
if (relation[targetUser][i] != 1) {
stranger.add(i);
}
}
if (userNum == 1) {
return -1;
}
// 判断哪个陌生人和自己的共同好友多
if (stranger.size() == 1) {
return stranger.get(0);
}
int resultUser = 0;
int maxFriends = getSameFriend(relation, stranger.get(0), targetUser, userNum);
for (int i = 1; i < stranger.size(); i++) {
int temp = getSameFriend(relation, stranger.get(i), targetUser, userNum);
if (temp > maxFriends) {
maxFriends = temp;
resultUser = stranger.get(i);
}
}
return resultUser;
}
private static int getSameFriend(int[][] relation, int user1, int user2, int userNum) {
int count = 0;
for (int i = 0; i < userNum; i++) {
if (relation[user1][i] == 1 && relation[user2][i] == 1) {
count++;
}
}
return count;
}
}

View File

@ -0,0 +1,90 @@
package com.raorao.interview.pdd;
import java.util.Scanner;
/**
* 列表补全问题.
*
* 描述 在商城的某个位置有一个商品列表该列表是由L1L2两个子列表拼接而成当用户浏览并翻页时需要从列表L1L2中获取商品进行展示展示规则如下
*
* 1. 用户可以进行多次翻页用offset表示用户在之前页面已经浏览的商品数量比如offset为4表示用户已经看了4个商品
*
* 2. n表示当前页面需要展示的商品数量
*
* 3. 展示商品时首先使用列表L1如果列表L1长度不够再从列表L2中选取商品
*
* 4. 从列表L2中补全商品时也可能存在数量不足的情况
*
* 请根据上述规则计算列表L1和L2中哪些商品在当前页面被展示了
*
* 输入描述
*
* 每个测试输入包含1个测试用例包含四个整数分别表示偏移量offset元素数量n列表L1的长度l1列表L2的长度l2
*
* 输出描述
*
* 一行内输出四个整数分别表示L1和L2的区间start1end1start2end2每个数字之间有一个空格 注意区间段使用半开半闭区间表示即包含起点不包含终点如果某个列表的区间为空使用[0,
* 0)表示如果某个列表被跳过使用[len, len)表示len表示列表的长度
*
* @author Xiong Raorao
* @since 2018-08-05-16:02
*/
public class ListComplete {
public static String process(String list) {
String[] arr = list.split(" ");
int offset = Integer.parseInt(arr[0]);
int n = Integer.parseInt(arr[1]);
int L1 = Integer.parseInt(arr[2]);
int L2 = Integer.parseInt(arr[3]);
int[] out = new int[4];
if (offset + n < L1) {
out[0] = offset;
out[1] = offset + n;
out[2] = 0;
out[3] = 0;
} else if (offset < L1 && offset + n >= L1) {
out[0] = offset;
out[1] = L1;
out[2] = 0;
if (offset + n < L1 + L2) {
out[3] = offset + n - L1;
} else {
out[3] = L2;
}
} else if (offset >= L1 && offset <= L1 + L2) {
out[0] = L1;
out[1] = L1;
out[2] = offset - L1;
if (offset + n < L1 + L2) {
out[3] = offset + n - L1;
} else {
out[3] = L2;
}
} else if (offset > L1 + L2){
out[0] = L1;
out[1] = L1;
out[2] = L2;
out[3] = L2;
}
StringBuilder sb = new StringBuilder();
sb.append(out[0]).append(" ")
.append(out[1]).append(" ")
.append(out[2]).append(" ")
.append(out[3]);
return sb.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(process(line));
}
}
}

View File

@ -0,0 +1,35 @@
package com.raorao.interview.pdd;
import java.util.Scanner;
/**
* 最大乘积.
*
* @author Xiong Raorao
* @since 2018-08-05-17:03
*/
public class MaxMultiply {
private static int[] arr;
private static int size;
public static void main(String[] args) {
for (int i = 0; i < size; i++) {
}
}
public static void read() {
Scanner scanner = new Scanner(System.in);
if (scanner.hasNextInt()) {
size = scanner.nextInt();
arr = new int[size];
}
if (scanner.hasNextLine()) {
String[] ss = scanner.nextLine().split(" ");
for (int i = 0; i < ss.length; i++) {
arr[i] = Integer.parseInt(ss[i]);
}
}
}
}

View File

@ -0,0 +1,58 @@
package com.raorao.interview.pdd;
import java.util.Scanner;
/**
* 有趣的变换.
*
* @author Xiong Raorao
* @since 2018-08-05-19:52
*/
public class Numbers {
public static int intSum(String string) {
if (string.equals("0")) {
return 1;
}
if (string.startsWith("0")) {
return 0;
} else {
return 1;
}
}
public static int foatSum(String string) {
if (string.endsWith("0")) {
return 0;
}
if (string.startsWith("0")) {
return 1;
} else {
return string.length() - 1;
}
}
public static int process(String string) {
int count = 0;
for (int i = 1; i < string.length(); i++) {
count +=
intSum(string.substring(0, i)) * foatSum(string.substring(i, string.length())) +
intSum(string.substring(0, i)) * intSum(string.substring(i, string.length()))
+
foatSum(string.substring(0, i)) * intSum(
string.substring(i, string.length())) +
foatSum(string.substring(0, i)) * foatSum(
string.substring(i, string.length()));
}
return count;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.println(process(input));
}
}

View File

@ -0,0 +1,72 @@
package com.raorao.interview.pdd;
import java.util.Scanner;
/**
* 旋转的字符串.
*
* 题目要求 求给定的数字的字符串
*
* @author Xiong Raorao
* @since 2018-08-05-19:04
*/
public class Square {
private static void process(String ss) {
char[] arr = ss.toCharArray();
int a;
char[][] result;
if (ss.length() == 0) {
System.out.println();
}
if (ss.length() % 4 == 0) {
a = ss.length() / 4 + 1;
result = new char[a][a];
// 第一行
for (int i = 0; i < a; i++) {
result[0][i] = arr[i];
}
// 最后一行
for (int i = 0; i < a; i++) {
result[a - 1][a - i - 1] = arr[2 * a - 2 + i];
}
// 右边
for (int i = 0; i < a - 2; i++) {
result[i + 1][a - 1] = arr[a + i];
}
// 左边
for (int i = 0; i < a - 2; i++) {
result[a - 2 - i][0] = arr[3 * a - 2 + i];
}
// 打印
print(result);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
process(scanner.nextLine());
}
}
private static void print(char[][] arr) {
int a = arr.length;
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
if (i == 0 || j == 0 || i == a - 1 || j == a - 1) {
System.out.print(arr[i][j] + "");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}

View File

@ -66,6 +66,12 @@ df, disk free, 通过文件系统来快速获取空间大小的信息,当我
(6) TCP 四次挥手的过程time_out的缺点
(7) mysql 的 group by 和 partition by?
group by 用来做聚合操作partition by 用于对某个字段分区,然后做某些操作。
普通的聚合函数用group by分组每个分组返回一个统计值而分析函数采用partition by分组并且每组每行都可以返回一个统计值。
分析函数的形式分析函数带有一个开窗函数over(),包含三个分析子句:分组(partition by), 排序(order by), 窗口(rows) 。
parition by 针对Oracle数据库
[参考链接](https://blog.csdn.net/cyl937/article/details/19930349)
(8) 海量数据去重例如大量的IP地址如何去重
MapReduce