网易笔试

This commit is contained in:
xiongraorao 2018-08-11 17:02:40 +08:00
parent 1d42ecffe6
commit 1907ff2e3b
8 changed files with 305 additions and 4 deletions

View File

@ -0,0 +1,89 @@
package com.raorao.interview.alibaba;
import java.util.Scanner;
/**
* 从坐标原点出发依次经过几个目标点直到回到原点问最短路径多少.
*
* @author Xiong Raorao
* @since 2018-08-11-8:28
*/
public class Main {
private static int times = 0;
private static int minCost = Integer.MAX_VALUE;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 初始化
int N = Integer.parseInt(scanner.nextLine());
boolean[] mark = new boolean[N];
Node[] nodes = new Node[N];
for (int i = 0; i < N; i++) {
String[] coordinate = scanner.nextLine().split(",");
int x = Integer.parseInt(coordinate[0]);
int y = Integer.parseInt(coordinate[1]);
nodes[i] = new Node(x, y);
}
route(mark, nodes, -1, N, 0, 0);
System.out.println(minCost);
}
public static int distance(Node from, Node to) {
return Math.abs(from.x - to.x) + Math.abs(from.y - to.y);
}
/**
* 求解路径
* @param mark 标记该点是否走过
* @param nodes 所有的坐标点
* @param now 第几个目标点
* @param N 总目标点数
* @param step 已经走过的目标点数
* @param cost 花费路程
*/
private static void route(boolean[] mark, Node[] nodes, int now, int N, int step, int cost) {
// 如果step == N 说明所有点一定都走过了
if (step > N) {
minCost = Math.min(minCost, cost + Math.abs(nodes[now].x) + Math.abs(nodes[now].y));
return;
}
if (now >= 0 && mark[now]) {
return;
}
if (now >= 0) {
mark[now] = true;
}
// System.out.println(step + ": " + cost);
for (int i = 0; i < N; i++) {
route(mark, nodes, i, N, step + 1,
cost + (now < 0 ? Math.abs(nodes[i].x) + Math.abs(nodes[i].y)
: distance(nodes[i], nodes[now])));
}
if (now >= 0) {
mark[now] = false;
}
}
private static class Node {
public int x;
public int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
}

View File

@ -0,0 +1,14 @@
package com.raorao.interview.netease.q1;
/**
* .
*
* @author Xiong Raorao
* @since 2018-08-11-16:03
*/
public class Main {
public static void main(String[] args) {
}
}

View File

@ -0,0 +1,69 @@
package com.raorao.interview.netease.q2;
import java.util.Scanner;
/**
* 分苹果问题 N堆苹果从左往右数第x个求第x个在第几堆里面堆从1开始算 .
*
* @author Xiong Raorao
* @since 2018-08-11-16:09
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
int[] apples = new int[n];
String[] temp = scanner.nextLine().split(" ");
for (int i = 0; i < n; i++) {
apples[i] = Integer.parseInt(temp[i]);
}
int m = Integer.parseInt(scanner.nextLine());
int[] queries = new int[m];
temp = scanner.nextLine().split(" ");
for (int i = 0; i < m; i++) {
queries[i] = Integer.parseInt(temp[i]);
}
process(apples, n, queries, m);
}
private static void process(int[] apples, int n, int[] queries, int m) {
int[] acc = new int[n];
acc[0] = apples[0];
for (int i = 1; i < n; i++) {
acc[i] = acc[i - 1] + apples[i];
}
for (int i = 0; i < m; i++) {
// for (int j = 0; j < n; j++) {
// if (queries[i] <= acc[j]) {
// System.out.println(j + 1);
// break;
// }
// }
System.out.println(part(0, n - 1, acc, queries[i]) + 1);
}
}
private static int part(int left, int right, int[] acc, int query) {
int mid = (left + right) / 2;
if (mid == 0) {
return 0;
}
if(left == right ){
return left;
}
if ( mid < acc.length && query <= acc[mid] && query <= acc[mid + 1] && query >= acc[mid - 1]) {
return mid;
}
if (query > acc[mid]) {
left = mid + 1;
return part(left, right, acc, query);
}
if (query < acc[mid]) {
right = mid - 1;
return part(left, right, acc, query);
}
return 0;
}
}

View File

@ -0,0 +1,18 @@
package com.raorao.interview.netease.q3;
/**
* .
*
* @author Xiong Raorao
* @since 2018-08-11-16:27
*/
public class Main {
public static void main(String[] args) {
}
private static void process() {
}
}

View File

@ -0,0 +1,76 @@
package com.raorao.java.althorithm.bintree;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* 二叉树的层次遍历.
*
* 描述输入一个二叉树的根节点输出
*
* @author Xiong Raorao
* @since 2018-08-11-10:50
*/
public class LevelOrder {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
TreeNode left = new TreeNode(2);
left.left = new TreeNode(4);
root.left = left;
TreeNode right = new TreeNode(3);
right.right = new TreeNode(5);
root.right = right;
List<List<Integer>> ret = levelOrder(root);
ret.stream().forEach(e -> {
e.stream().forEach(f -> System.out.print(f + "\t"));
System.out.println();
});
}
public static List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if (root == null) {
return ret;
}
LinkedList<TreeNode> queue = new LinkedList<>();
queue.offer(root);
List<Integer> level = new ArrayList<>();
int flag = 0;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
System.out.print(node.val + " ");
flag--;
if(flag == 0){
System.out.println();
}
if (level.size() > 0) {
ret.add(level);
}
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
flag = queue.size();
}
return ret;
}
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}

View File

@ -62,10 +62,10 @@ public class StreamTest {
* 和前面 Stream 的第一个第二个 n 个元素组合从这个意义上说字符串拼接数值的 summinmaxaverage 都是特殊的 reduce * 和前面 Stream 的第一个第二个 n 个元素组合从这个意义上说字符串拼接数值的 summinmaxaverage 都是特殊的 reduce
*/ */
// 1. 字符串拼接 // 1. 字符串拼接
Optional<String> ss = stream.reduce((a, b) -> a + b); //Optional<String> ss = stream.reduce((a, b) -> a + b);
System.out.println(ss.orElse(null)); //System.out.println(ss.orElse(null));
//String s2 = stream.reduce("", (a, b) -> a + b + "-"); String s2 = stream.reduce("", (a, b) -> a + "-" + b );
//System.out.println(s2); System.out.println(s2);
// 2. 数值相加 // 2. 数值相加
Optional<Integer> sum = intList.stream().reduce((a, b) -> a + b); Optional<Integer> sum = intList.stream().reduce((a, b) -> a + b);

View File

@ -0,0 +1,30 @@
package com.raorao.leetcode;
import java.util.Arrays;
/**
* 分配饼干.
*
* @author Xiong Raorao
* @since 2018-08-11-10:05
*/
public class AssignCookie {
public static void main(String[] args) {
}
private static int process(int[] childrens, int[] cookies) {
Arrays.sort(childrens);
Arrays.sort(cookies);
int child = 0;
int cookie = 0;
while (child < childrens.length && cookie < cookies.length) {
if (childrens[child] <= cookies[child]) {
child++;
}
cookie++;
}
return child;
}
}

View File

@ -0,0 +1,5 @@
# 大顶堆和小顶堆
比如求10亿个数中的最大的前10个数时时构建只有10个元素的小顶堆如果比堆顶小则不处理如果比堆顶大则替换堆顶然后依次下沉到适当的位置。
比如求10亿个数中的最小的前10个数时时构建只有10个元素的大顶堆如果比堆顶大则不处理如果比堆顶小则替换堆顶然后依次下沉到适当的位置。