今日头条笔试
This commit is contained in:
parent
bb67071d12
commit
05988b660b
11
code/src/main/java/com/raorao/interview/bytedance/Test.java
Normal file
11
code/src/main/java/com/raorao/interview/bytedance/Test.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.raorao.interview.bytedance;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-12-10:15
|
||||
*/
|
||||
public class Test {
|
||||
|
||||
}
|
123
code/src/main/java/com/raorao/interview/bytedance/q2/Main.java
Normal file
123
code/src/main/java/com/raorao/interview/bytedance/q2/Main.java
Normal file
@ -0,0 +1,123 @@
|
||||
package com.raorao.interview.bytedance.q2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-12-10:28
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int m = Integer.parseInt(scanner.nextLine());
|
||||
List<Node> nodes = new ArrayList<>();
|
||||
for (int i = 0; i < m; i++) {
|
||||
String[] input = scanner.nextLine().split(";");
|
||||
for (String s : input) {
|
||||
String[] t = s.split(",");
|
||||
int start = Integer.parseInt(t[0]);
|
||||
int end = Integer.parseInt(t[1]);
|
||||
nodes.add(new Node(start, end));
|
||||
}
|
||||
}
|
||||
List<Node> ret = process(nodes);
|
||||
|
||||
while (process(ret).size() != ret.size()) {
|
||||
ret = process(ret);
|
||||
}
|
||||
|
||||
// 排序
|
||||
ret.sort(Comparator.comparing(o -> o.start));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
ret.forEach(e -> sb.append(e.start).append(",").append(e.end).append(";"));
|
||||
System.out.println(sb.toString().substring(0, sb.toString().length() - 1));
|
||||
}
|
||||
|
||||
private static List<Node> process(List<Node> nodes) {
|
||||
List<Node> ret = new ArrayList<>();
|
||||
ret.add(nodes.get(0));
|
||||
int size = nodes.size();
|
||||
if (size == 1) {
|
||||
System.out.println(ret.get(0).start + "," + ret.get(0).end);
|
||||
return ret;
|
||||
}
|
||||
for (int i = 1; i < size; i++) {
|
||||
Node mNode = merge(ret, nodes.get(i));
|
||||
if (mNode == null) {
|
||||
ret.add(nodes.get(i));
|
||||
} else {
|
||||
ret.set(mNode.index, mNode);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static Node merge(List<Node> nodes, Node b) {
|
||||
Node ret = null;
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
Node tmp = merge(nodes.get(i), b);
|
||||
if (tmp != null) {
|
||||
ret = tmp;
|
||||
ret.index = i;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private static Node merge(Node a, Node b) {
|
||||
if (a.equals(b)) {
|
||||
return a;
|
||||
}
|
||||
|
||||
// 保证a在前,b在后
|
||||
if (a.start > b.start) {
|
||||
// 交换a
|
||||
Node tmp = a;
|
||||
a = b;
|
||||
b = tmp;
|
||||
}
|
||||
|
||||
if (a.end < b.start) {
|
||||
return null;
|
||||
} else {
|
||||
if (a.end < b.end) {
|
||||
return new Node(a.start, b.end);
|
||||
}
|
||||
if (a.end > b.end) {
|
||||
return new Node(a.start, a.end);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static class Node {
|
||||
|
||||
Integer start;
|
||||
Integer end;
|
||||
int index;
|
||||
|
||||
public Node(int start, int end) {
|
||||
this.start = start;
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
public boolean equals(Node obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (start == obj.start && end == obj.end) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package com.raorao.interview.bytedance.q5;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-12-10:16
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
}
|
||||
|
||||
private static void process(int n, int m, int[] s, int[] t) {
|
||||
|
||||
}
|
||||
|
||||
private static int getMin(int[] src) {
|
||||
int min = Integer.MAX_VALUE;
|
||||
for (int i = 0; i < src.length; i++) {
|
||||
if (min > src[i]) {
|
||||
min = src[i];
|
||||
}
|
||||
}
|
||||
return min;
|
||||
}
|
||||
|
||||
private static int getMax(int[] src) {
|
||||
int max = Integer.MIN_VALUE;
|
||||
for (int i = 0; i < src.length; i++) {
|
||||
if (max < src[i]) {
|
||||
max = src[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
static class ZB {
|
||||
|
||||
public int s;
|
||||
public int t;
|
||||
}
|
||||
|
||||
}
|
11
code/src/main/java/com/raorao/java/A.java
Normal file
11
code/src/main/java/com/raorao/java/A.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.raorao.java;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-10-23:27
|
||||
*/
|
||||
public class A {
|
||||
|
||||
}
|
28
code/src/main/java/com/raorao/java/Test.java
Normal file
28
code/src/main/java/com/raorao/java/Test.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.raorao.java;
|
||||
|
||||
public class Test {
|
||||
static int a;
|
||||
int b;
|
||||
static int c;
|
||||
|
||||
public int aMethod() {
|
||||
a++;
|
||||
return a;
|
||||
}
|
||||
|
||||
public int bMethod() {
|
||||
b++;
|
||||
return b;
|
||||
}
|
||||
|
||||
public static int cMethod() {
|
||||
c++;
|
||||
return c;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
String str = null;
|
||||
|
||||
System.out.println(str);
|
||||
}
|
||||
}
|
11
code/src/main/java/com/raorao/java/test/A.java
Normal file
11
code/src/main/java/com/raorao/java/test/A.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.raorao.java.test;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author Xiong Raorao
|
||||
* @since 2018-08-10-23:27
|
||||
*/
|
||||
public class A {
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user