华为笔试
This commit is contained in:
parent
5b3e132105
commit
87cd1739e4
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.markilue.interview</groupId>
|
||||||
|
<artifactId>Huawei</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,132 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Huawei
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-04-19 19:02
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question1 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int count = sc.nextInt();
|
||||||
|
int[][] nums = new int[count][2];
|
||||||
|
int index = 0;
|
||||||
|
while (count-- > 0) {
|
||||||
|
nums[index][0] = sc.nextInt();
|
||||||
|
nums[index][1] = sc.nextInt();
|
||||||
|
}
|
||||||
|
Arrays.sort(nums, new Comparator<int[]>() {
|
||||||
|
@Override
|
||||||
|
public int compare(int[] o1, int[] o2) {
|
||||||
|
return o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
int res=0;
|
||||||
|
ArrayList<int[]> single = new ArrayList<>();
|
||||||
|
ArrayList<int[]> multi = new ArrayList<>();
|
||||||
|
for (int i = 0; i < nums.length; i++) {
|
||||||
|
if(nums[i][0]<=nums[i-1][1]){
|
||||||
|
if(multi.size()>0&&nums[i][0]<multi.get(multi.size()-1)[1]){
|
||||||
|
nums[i][0] = multi.get(multi.size()-1)[1];
|
||||||
|
if(nums[i][1]<nums[i-1][1]){
|
||||||
|
int[] temp = nums[i];
|
||||||
|
nums[i]=nums[i-1];
|
||||||
|
nums[i-1]=temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(nums[i-1][0]!=nums[i][0])single.add(new int[]{nums[i-1][0],nums[i][0]});
|
||||||
|
if(nums[i][0]!=nums[i-1][1])multi.add(new int[]{nums[i][0],nums[i-1][1]});
|
||||||
|
}else {
|
||||||
|
single.add(nums[i-1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int count = sc.nextInt();
|
||||||
|
int[][] nums = new int[count][2];
|
||||||
|
int index = 0;
|
||||||
|
while (count-- > 0) {
|
||||||
|
nums[index][0] = sc.nextInt();
|
||||||
|
nums[index][1] = sc.nextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
int[][] nums = new int[][]{{2, 5}, {8, 9}};
|
||||||
|
// int[][] nums = new int[][]{{4,8}, {1,6},{2,9}};
|
||||||
|
solve(nums);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void solve(int[][] nums) {
|
||||||
|
Arrays.sort(nums, new Comparator<int[]>() {
|
||||||
|
@Override
|
||||||
|
public int compare(int[] o1, int[] o2) {
|
||||||
|
return o1[0] == o2[0] ? o1[1] - o2[1] : o1[0] - o2[0];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ArrayList<int[]> total = new ArrayList<>();
|
||||||
|
ArrayList<Integer> condition = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
|
int[] last = nums[0];
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
for (int i = 1; i < nums.length; i++) {
|
||||||
|
if (last[1] > nums[i][0]) {
|
||||||
|
|
||||||
|
total.add(new int[]{last[0], nums[i][0]-1});
|
||||||
|
condition.add(1);
|
||||||
|
if (nums[i][1] < last[1]) {
|
||||||
|
total.add(new int[]{nums[i][0], nums[i][1]});
|
||||||
|
condition.add(2);
|
||||||
|
last[0] = nums[i][1];
|
||||||
|
} else {
|
||||||
|
total.add(new int[]{nums[i][0], last[1]});
|
||||||
|
condition.add(2);
|
||||||
|
last[0] = last[1];
|
||||||
|
last[1] = nums[i][1];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
total.add(new int[]{last[0], last[1]});
|
||||||
|
condition.add(1);
|
||||||
|
total.add(new int[]{last[1] + 1, nums[i][0] - 1});
|
||||||
|
condition.add(0);
|
||||||
|
last = nums[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
total.add(new int[]{last[0], last[1]});
|
||||||
|
condition.add(1);
|
||||||
|
|
||||||
|
for (int i = 0; i < total.size(); i++) {
|
||||||
|
int[] ints = total.get(i);
|
||||||
|
Integer condition1 = condition.get(i);
|
||||||
|
if(condition1==0){
|
||||||
|
result+=(ints[1]-ints[0]+1);
|
||||||
|
}else if(condition1==1){
|
||||||
|
result+=(ints[1]-ints[0]+1)*3;
|
||||||
|
}else {
|
||||||
|
result+=(ints[1]-ints[0]+1)*4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Huawei
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-04-19 20:02
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question2 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int n = sc.nextInt();
|
||||||
|
int numEdges = sc.nextInt();
|
||||||
|
ArrayList<ArrayList<Integer>> edges = new ArrayList<>();
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
edges.add(new ArrayList<>());
|
||||||
|
}
|
||||||
|
for (int i = 0; i < numEdges; i++) {
|
||||||
|
int a = sc.nextInt();
|
||||||
|
int b = sc.nextInt();
|
||||||
|
edges.get(a).add(b);
|
||||||
|
edges.get(b).add(a);
|
||||||
|
}
|
||||||
|
int numObs = sc.nextInt();
|
||||||
|
HashSet<Integer> set = new HashSet<>();
|
||||||
|
for (int i = 0; i < numObs; i++) {
|
||||||
|
set.add(sc.nextInt());
|
||||||
|
}
|
||||||
|
recur(edges,-1,0,set,0);
|
||||||
|
if(res.length()==0){
|
||||||
|
System.out.println("NULL");
|
||||||
|
}else {
|
||||||
|
System.out.println(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static int minLen = Integer.MAX_VALUE;
|
||||||
|
static StringBuilder res = new StringBuilder();
|
||||||
|
static StringBuilder path = new StringBuilder();
|
||||||
|
|
||||||
|
public static void recur(ArrayList<ArrayList<Integer>> edges, int parent, int cur, HashSet<Integer> set, int len) {
|
||||||
|
//如果当前节点是故障,直接return
|
||||||
|
if (set.contains(cur)) return;
|
||||||
|
|
||||||
|
len += 1;
|
||||||
|
if (path.length() == 0) {
|
||||||
|
path.append(String.valueOf(cur));
|
||||||
|
} else {
|
||||||
|
path.append("->").append(String.valueOf(cur));
|
||||||
|
}
|
||||||
|
int size = edges.get(cur).size();
|
||||||
|
if (size == 1 && edges.get(cur).get(0) == parent) {
|
||||||
|
if(len<minLen){
|
||||||
|
minLen=len;
|
||||||
|
res=new StringBuilder(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
int child=edges.get(cur).get(i);
|
||||||
|
if(child==parent)continue;//父节点直接跳过
|
||||||
|
else {
|
||||||
|
recur(edges,cur,child,set,len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(path.length()>3){
|
||||||
|
path.delete(path.length()-3,path.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,93 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Huawei
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-04-19 20:00
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question3 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String[] s = sc.nextLine().split(" ");
|
||||||
|
int[] nums = Arrays.stream(s).mapToInt(Integer::parseInt).toArray();
|
||||||
|
new Question3().solve(nums);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
int[] nums={1,5,3,4,6,5,1};
|
||||||
|
solve(nums);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void solve(int[] nums) {
|
||||||
|
List<List<Integer>> result = new ArrayList<>();
|
||||||
|
boolean[] used = new boolean[nums.length];
|
||||||
|
for (int i = 1; i < nums.length - 1; i++) {
|
||||||
|
if (nums[i] > nums[i - 1] && nums[i] > nums[i + 1]) {
|
||||||
|
//是引流点
|
||||||
|
used[i] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < nums.length - 1; i++) {
|
||||||
|
if (used[i]) {
|
||||||
|
//是引流点
|
||||||
|
|
||||||
|
result.add(new ArrayList<>(Arrays.asList(nums[i])));
|
||||||
|
//判断他左右能不能加入
|
||||||
|
int left = i - 1;
|
||||||
|
List<Integer> list = result.get(result.size() - 1);
|
||||||
|
while (left > 0 && !used[left]) {
|
||||||
|
if (nums[left - 1] < nums[left] && nums[left + 1] > nums[left]) {
|
||||||
|
list.add(nums[left]);
|
||||||
|
}
|
||||||
|
if (nums[left - 1] > nums[left] && nums[left + 1] > nums[left]&&nums[left + 1] >nums[left-1]) {
|
||||||
|
list.add(nums[left]);
|
||||||
|
}
|
||||||
|
if (nums[left - 1] > nums[left] && nums[left + 1] > nums[left]&&nums[left + 1] <nums[left-1]&&used[left+1]) {
|
||||||
|
//是引流点
|
||||||
|
list.add(nums[left]);
|
||||||
|
}
|
||||||
|
left--;
|
||||||
|
}
|
||||||
|
int right = i + 1;
|
||||||
|
boolean flag =true;
|
||||||
|
while (right < nums.length-1 && !used[right]&&flag) {
|
||||||
|
//往那边引流
|
||||||
|
if (nums[right - 1] < nums[right] && nums[right + 1] > nums[right]) {
|
||||||
|
list.add(nums[right]);
|
||||||
|
flag=false;
|
||||||
|
}
|
||||||
|
if (nums[right - 1] > nums[right] && nums[right + 1] > nums[right]&&nums[right + 1] <=nums[right-1]) {
|
||||||
|
int temp=right;
|
||||||
|
while (temp<nums.length&&!used[temp]){
|
||||||
|
temp++;
|
||||||
|
}
|
||||||
|
if(temp<nums.length&&nums[temp]<nums[i]){
|
||||||
|
list.add(nums[right]);
|
||||||
|
flag=false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
flag=!flag;
|
||||||
|
right++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue