Merge remote-tracking branch 'origin/master'
# Conflicts: # interview/Tencent/pom.xml
This commit is contained in:
commit
97094f1d88
|
|
@ -1,9 +1,7 @@
|
||||||
package com.markilue.leecode.hot100;
|
package com.markilue.leecode.hot100;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.omg.CORBA.PUBLIC_MEMBER;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,277 @@
|
||||||
|
package com.markilue.leecode.hot100;
|
||||||
|
|
||||||
|
import com.markilue.leecode.tree.TreeNode;
|
||||||
|
import com.markilue.leecode.tree.TreeUtils;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.hot100
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-29 10:24
|
||||||
|
*@Description:
|
||||||
|
* TODO 力扣297 二叉树得到序列化与反序列化:
|
||||||
|
* 序列化是将一个数据结构或者对象转换为连续的比特位的操作,进而可以将转换后的数据存储在一个文件或者内存中,同时也可以通过网络传输到另一个计算机环境,采取相反方式重构得到原数据。
|
||||||
|
* 请设计一个算法来实现二叉树的序列化与反序列化。这里不限定你的序列 / 反序列化算法执行逻辑,你只需要保证一个二叉树可以被序列化为一个字符串并且将这个字符串反序列化为原始的树结构。
|
||||||
|
* 提示: 输入输出格式与 LeetCode 目前使用的方式一致,详情请参阅 LeetCode 序列化二叉树的格式。你并非必须采取这种方式,你也可以采用其他的方法解决这个问题。
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class T77_Codec {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, null, null, 4, 5, 6, 7));
|
||||||
|
TreeNode root = TreeUtils.structureTree(list, 0);
|
||||||
|
System.out.println(serialize(root));
|
||||||
|
TreeUtils.printTreeByLevel(deserialize(serialize(root)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
// List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, null, null, 4, 5, 6, 7));
|
||||||
|
// TreeNode root = TreeUtils.structureTree(list, 0);
|
||||||
|
// System.out.println(serialize(root));
|
||||||
|
|
||||||
|
TreeUtils.printTreeByLevel(deserialize("4 -7 -3 null null -9 -3 9 -7 -4 null 6 null -6 -6 null null 0 6 5 null 9 null null -1 -4 null null null -2"));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Encodes a tree to a single string.
|
||||||
|
//层序遍历加入string,最后超时了,当一个单边树无限再深下去,后面会添加很多个null
|
||||||
|
public String serialize(TreeNode root) {
|
||||||
|
if (root == null) return "null";
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
|
queue.add(root);
|
||||||
|
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
int temp = 0;
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
TreeNode cur = queue.poll();
|
||||||
|
|
||||||
|
if (cur != null) {
|
||||||
|
sb.append(cur.val);
|
||||||
|
queue.offer(cur.left);
|
||||||
|
queue.offer(cur.right);
|
||||||
|
} else {
|
||||||
|
temp++;
|
||||||
|
sb.append("null");
|
||||||
|
queue.offer(null);
|
||||||
|
queue.offer(null);
|
||||||
|
}
|
||||||
|
sb.append(" ");
|
||||||
|
}
|
||||||
|
if (temp == size) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decodes your encoded data to tree.
|
||||||
|
public TreeNode deserialize(String data) {
|
||||||
|
String[] total = data.trim().split(" ");
|
||||||
|
int n = total.length;
|
||||||
|
if (n == 1 && "null".equals(total[0])) return null;
|
||||||
|
TreeNode root = new TreeNode(Integer.parseInt(total[0]));
|
||||||
|
TreeNode temp = root;
|
||||||
|
LinkedList<TreeNode> treeList = new LinkedList<>();
|
||||||
|
LinkedList<Integer> indexList = new LinkedList<>();
|
||||||
|
|
||||||
|
treeList.add(root);
|
||||||
|
indexList.add(0);
|
||||||
|
|
||||||
|
while (!treeList.isEmpty()) {
|
||||||
|
int size = treeList.size();
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
TreeNode node = treeList.poll();
|
||||||
|
Integer index = indexList.poll();
|
||||||
|
if ("null".equals(total[index])) continue;
|
||||||
|
int left = 2 * index + 1;
|
||||||
|
if (left < n && !"null".equals(total[left])) {
|
||||||
|
node.left = new TreeNode(Integer.parseInt(total[left]));
|
||||||
|
treeList.offer(node.left);
|
||||||
|
indexList.offer(left);
|
||||||
|
}
|
||||||
|
|
||||||
|
int right = 2 * (index + 1);
|
||||||
|
if (right < n && !"null".equals(total[right])) {
|
||||||
|
node.right = new TreeNode(Integer.parseInt(total[right]));
|
||||||
|
treeList.offer(node.right);
|
||||||
|
indexList.offer(right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 官方先序遍历法
|
||||||
|
* 速度击败18.26% 内存击败5.95% 94ms
|
||||||
|
* @param root
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String serialize1(TreeNode root) {
|
||||||
|
return rserialize(root, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode deserialize1(String data) {
|
||||||
|
String[] dataArray = data.split(",");
|
||||||
|
List<String> dataList = new LinkedList<String>(Arrays.asList(dataArray));
|
||||||
|
return rdeserialize(dataList);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String rserialize(TreeNode root, String str) {
|
||||||
|
if (root == null) {
|
||||||
|
str += "None,";
|
||||||
|
} else {
|
||||||
|
str += str.valueOf(root.val) + ",";
|
||||||
|
str = rserialize(root.left, str);
|
||||||
|
str = rserialize(root.right, str);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode rdeserialize(List<String> dataList) {
|
||||||
|
if (dataList.get(0).equals("None")) {
|
||||||
|
dataList.remove(0);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode root = new TreeNode(Integer.valueOf(dataList.get(0)));
|
||||||
|
dataList.remove(0);
|
||||||
|
root.left = rdeserialize(dataList);
|
||||||
|
root.right = rdeserialize(dataList);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 官方:括号表示编码 + 递归下降解码
|
||||||
|
* 速度击败58.49% 内存击败75.8% 15ms
|
||||||
|
* @param root
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String serialize2(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return "X";
|
||||||
|
}
|
||||||
|
String left = "(" + serialize2(root.left) + ")";
|
||||||
|
String right = "(" + serialize2(root.right) + ")";
|
||||||
|
return left + root.val + right;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode deserialize2(String data) {
|
||||||
|
int[] ptr = {0};
|
||||||
|
return parse(data, ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode parse(String data, int[] ptr) {
|
||||||
|
if (data.charAt(ptr[0]) == 'X') {
|
||||||
|
++ptr[0];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
TreeNode cur = new TreeNode(0);
|
||||||
|
cur.left = parseSubtree(data, ptr);
|
||||||
|
cur.val = parseInt(data, ptr);
|
||||||
|
cur.right = parseSubtree(data, ptr);
|
||||||
|
return cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TreeNode parseSubtree(String data, int[] ptr) {
|
||||||
|
++ptr[0]; // 跳过左括号
|
||||||
|
TreeNode subtree = parse(data, ptr);
|
||||||
|
++ptr[0]; // 跳过右括号
|
||||||
|
return subtree;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int parseInt(String data, int[] ptr) {
|
||||||
|
int x = 0, sgn = 1;
|
||||||
|
if (!Character.isDigit(data.charAt(ptr[0]))) {
|
||||||
|
sgn = -1;
|
||||||
|
++ptr[0];
|
||||||
|
}
|
||||||
|
while (Character.isDigit(data.charAt(ptr[0]))) {
|
||||||
|
x = x * 10 + data.charAt(ptr[0]++) - '0';
|
||||||
|
}
|
||||||
|
return x * sgn;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 官方合理且最快:本质上还是上面的编码写法:但是使用sb加速
|
||||||
|
* 速度假币98.68% 内存击败81.2% 1ms
|
||||||
|
*
|
||||||
|
* @param root
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String serialize3(TreeNode root) {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
serialize3(root, sb);
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void serialize3(TreeNode node, StringBuilder sb) {
|
||||||
|
if (node == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sb.append(node.val);
|
||||||
|
if (node.left != null) {
|
||||||
|
sb.append('(');
|
||||||
|
serialize3(node.left, sb);
|
||||||
|
sb.append(')');
|
||||||
|
}
|
||||||
|
if (node.right != null) {
|
||||||
|
if (node.left == null) {
|
||||||
|
sb.append("()");
|
||||||
|
}
|
||||||
|
sb.append('(');
|
||||||
|
serialize3(node.right, sb);
|
||||||
|
sb.append(')');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int ind;
|
||||||
|
|
||||||
|
// Decodes your encoded data to tree.
|
||||||
|
public TreeNode deserialize3(String data) {
|
||||||
|
if (ind >= data.length() || data.charAt(ind) == ')') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeNode node = new TreeNode(0);
|
||||||
|
int multi = 1;
|
||||||
|
if (data.charAt(ind) == '-') {
|
||||||
|
multi = -1;
|
||||||
|
ind++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (ind < data.length() && data.charAt(ind) >= '0' && data.charAt(ind) <= '9') {
|
||||||
|
node.val = node.val * 10 + data.charAt(ind) - '0';
|
||||||
|
ind++;
|
||||||
|
}
|
||||||
|
node.val *= multi;
|
||||||
|
|
||||||
|
if (ind < data.length() && data.charAt(ind) == '(') {
|
||||||
|
ind++;
|
||||||
|
node.left = deserialize3(data);
|
||||||
|
ind++;
|
||||||
|
}
|
||||||
|
if (ind < data.length() && data.charAt(ind) == '(') {
|
||||||
|
ind++;
|
||||||
|
node.right = deserialize3(data);
|
||||||
|
ind++;
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
package com.markilue.leecode.test;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.omg.CORBA.PUBLIC_MEMBER;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.test
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-28 19:27
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class TestByteDance {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
int n = sc.nextInt();
|
||||||
|
TestByteDance sc1 = new TestByteDance();
|
||||||
|
while (n-- > 0) {
|
||||||
|
int num = sc.nextInt();
|
||||||
|
int k = sc.nextInt();
|
||||||
|
int result = sc1.sovle(num, k);
|
||||||
|
if (result == -1) {
|
||||||
|
System.out.println(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Integer> cur = new ArrayList<Integer>();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
// System.out.println(Math.pow(2, 2));
|
||||||
|
// System.out.println((int) Math.pow(2, 2));
|
||||||
|
// System.out.println(-1);
|
||||||
|
System.out.println(sovle(10000, 500));
|
||||||
|
// cur.clear();
|
||||||
|
// System.out.println(sovle(96, 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
public int sovle(int num, int k) {
|
||||||
|
if (cur.size() == k) {
|
||||||
|
if (num == 0) {
|
||||||
|
for (Integer integer : cur) {
|
||||||
|
System.out.print(integer + " ");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = getI(num), z = 1 << i; i >= 0; i--, z >>= 1) {
|
||||||
|
cur.add(i);
|
||||||
|
if (sovle(num - z, k) == 0) return 0;
|
||||||
|
cur.remove(cur.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getI(int num) {
|
||||||
|
return (int) (Math.log(num) / Math.log(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
System.out.println(1 << 3);
|
||||||
|
System.out.println(getI(9));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
package com.markilue.leecode.test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.test
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-28 20:02
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class TestByteDance4 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int k = Integer.parseInt(sc.nextLine());
|
||||||
|
String[] strings = new String[k];
|
||||||
|
for (int i = 0; i < k; i++) {
|
||||||
|
strings[i] = sc.nextLine();
|
||||||
|
}
|
||||||
|
int operate = Integer.parseInt(sc.nextLine());
|
||||||
|
String[] operates = new String[operate];
|
||||||
|
for (int i = 0; i < operate; i++) {
|
||||||
|
operates[i] = sc.nextLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sovle(String[] strings, String[] operate) {
|
||||||
|
|
||||||
|
HashMap<String, HashSet<String>> map = new HashMap<>();
|
||||||
|
|
||||||
|
//原始情况
|
||||||
|
for (String string : strings) {
|
||||||
|
String[] split = string.split("/");
|
||||||
|
for (String s : split) {
|
||||||
|
map.getOrDefault(s,new HashSet<>());
|
||||||
|
// map.put()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
<?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>Ctrip</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>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.1</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
</project>
|
||||||
|
|
@ -0,0 +1,39 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Ctrip
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-29 19:10
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question1 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String s = sc.nextLine();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sovle(String s){
|
||||||
|
char[] chars = s.toCharArray();
|
||||||
|
|
||||||
|
int count =0 ;
|
||||||
|
|
||||||
|
for (char aChar : chars) {
|
||||||
|
if(aChar=='0'||aChar=='6'||aChar=='9'){
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if(aChar=='8'){
|
||||||
|
count+=2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(count);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Ctrip
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-29 19:16
|
||||||
|
*@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 k = sc.nextInt();
|
||||||
|
new Question2().sovle(n,k);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* n个相同的数
|
||||||
|
* k个好元素
|
||||||
|
* @param n
|
||||||
|
* @param k
|
||||||
|
*/
|
||||||
|
public void sovle(int n, int k) {
|
||||||
|
|
||||||
|
//倒序排列,然后需要的往前挪
|
||||||
|
//3 1 5 2 4
|
||||||
|
//最大的数放在2*(k-1)的位置
|
||||||
|
int[] nums = new int[n];
|
||||||
|
int i = n;
|
||||||
|
for (; k > 0; i--, k--) {
|
||||||
|
nums[2 * (k - 1)] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
//剩下的随便放
|
||||||
|
int index = 0;
|
||||||
|
for (int j = 1; j <= i; j++) {
|
||||||
|
while (nums[index] != 0) index++;
|
||||||
|
nums[index]=j;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j < nums.length; j++) {
|
||||||
|
System.out.print(nums[j]+" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,134 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Ctrip
|
||||||
|
*@BelongsPackage: com.markilue.interview
|
||||||
|
*@Author: markilue
|
||||||
|
*@CreateTime: 2023-03-29 19:50
|
||||||
|
*@Description: TODO
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class Question3 {
|
||||||
|
|
||||||
|
static Map<Integer, Integer> jie = new HashMap<>();
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int nums = sc.nextInt();
|
||||||
|
// getJieCheng();
|
||||||
|
|
||||||
|
// for (Map.Entry<Integer, Integer> entry : jie.entrySet()) {
|
||||||
|
//
|
||||||
|
// System.out.println(entry.getKey()+":"+entry.getValue());
|
||||||
|
// }
|
||||||
|
new Question3().sovle(nums);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sovle(int nums) {
|
||||||
|
|
||||||
|
//寻找num的最大因数
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
for (int i = nums - 1; i > 0; i--) {
|
||||||
|
if (nums % i == 0 && i != 2) list.add(i);
|
||||||
|
}
|
||||||
|
System.out.println(list);
|
||||||
|
|
||||||
|
int min = Integer.MAX_VALUE;
|
||||||
|
int x=0;
|
||||||
|
int y=0;
|
||||||
|
for (int i = list.size()-1; i >=0; i--) {
|
||||||
|
for (Map.Entry<Integer, Integer> entry : jie.entrySet()) {
|
||||||
|
Integer key = entry.getKey();
|
||||||
|
Integer value = entry.getValue();
|
||||||
|
Integer z = list.get(i);
|
||||||
|
int cur = Math.abs(key - 1 - (nums / z));
|
||||||
|
if (cur < min) {
|
||||||
|
min = cur;
|
||||||
|
x = value;
|
||||||
|
y = z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.print(x+" "+y);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sovle1(int nums) {
|
||||||
|
|
||||||
|
//寻找num的最大因数
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
for (int i = nums - 1; i > 0; i--) {
|
||||||
|
if (nums % i == 0 && i != 2) list.add(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
int min = Integer.MAX_VALUE;
|
||||||
|
int x=0;
|
||||||
|
int y=0;
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
for (Map.Entry<Integer, Integer> entry : jie.entrySet()) {
|
||||||
|
Integer key = entry.getKey();
|
||||||
|
Integer value = entry.getValue();
|
||||||
|
Integer z = list.get(i);
|
||||||
|
int cur = Math.abs(key - 1 - (nums / z));
|
||||||
|
if (cur < min) {
|
||||||
|
min = cur;
|
||||||
|
x = value;
|
||||||
|
y = z;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.print(x+" "+y);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() {
|
||||||
|
// Question3.getJieCheng();
|
||||||
|
sovle(100);
|
||||||
|
// System.out.println(Math.pow(10, 9));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void getJieCheng() {
|
||||||
|
int total = 1;
|
||||||
|
double pow = Math.pow(10, 9);
|
||||||
|
for (int i = 1; total < pow; i++) {
|
||||||
|
total *= i;
|
||||||
|
if (i != 2) {
|
||||||
|
jie.put(total, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getI(int nums){
|
||||||
|
if(nums==0)return 1;
|
||||||
|
return nums*getI(nums-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void solve(int nums){
|
||||||
|
int resx =0;
|
||||||
|
int resy=0;
|
||||||
|
int min=Integer.MAX_VALUE;
|
||||||
|
|
||||||
|
for (int i = 0; i <= nums + 1; i++) {
|
||||||
|
long fac=getI(i);
|
||||||
|
if(fac>nums+1)break;
|
||||||
|
if(i==2)continue;
|
||||||
|
int left=1;
|
||||||
|
int right=nums;
|
||||||
|
while (left<=right){
|
||||||
|
int y = left+(right-left)/2;
|
||||||
|
int num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -4,21 +4,57 @@
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.markilue.interview</groupId>
|
<groupId>org.example</groupId>
|
||||||
<artifactId>Tencent</artifactId>
|
<artifactId>Leecode</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.projectlombok</groupId>-->
|
||||||
|
<!-- <artifactId>lombok</artifactId>-->
|
||||||
|
<!-- <version>RELEASE</version>-->
|
||||||
|
<!--<!– <scope>compile</scope>–>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>1.18.12</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.13.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.13.2</version>
|
<version>4.13.2</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>8</maven.compiler.source>
|
|
||||||
<maven.compiler.target>8</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*@BelongsProject: Leecode
|
||||||
|
*@BelongsPackage: com.markilue.leecode.listnode
|
||||||
|
*@Author: dingjiawen
|
||||||
|
*@CreateTime: 2022-12-22 11:55
|
||||||
|
*@Description: TODO 链表的使用工具
|
||||||
|
*@Version: 1.0
|
||||||
|
*/
|
||||||
|
public class ListNodeUtils {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPrint(){
|
||||||
|
int[] nodeList= {2, 4, 3};
|
||||||
|
ListNode root = build(nodeList);
|
||||||
|
print(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过一个val数组建立链表
|
||||||
|
* @param nodeList
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static ListNode build(int[] nodeList){
|
||||||
|
if(nodeList.length==0||nodeList==null)return null;
|
||||||
|
ListNode root =new ListNode(nodeList[0]);
|
||||||
|
ListNode temp=root;
|
||||||
|
for (int i = 1; i < nodeList.length; i++) {
|
||||||
|
temp.next= new ListNode(nodeList[i]);
|
||||||
|
temp=temp.next;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印链表
|
||||||
|
* @param root
|
||||||
|
*/
|
||||||
|
public static void print(ListNode root){
|
||||||
|
StringBuilder builder=new StringBuilder("[");
|
||||||
|
ListNode temp=root;
|
||||||
|
while (temp!=null){
|
||||||
|
builder.append(temp.val);
|
||||||
|
builder.append(",");
|
||||||
|
temp=temp.next;
|
||||||
|
}
|
||||||
|
builder.append("]");
|
||||||
|
|
||||||
|
System.out.println(builder.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
package com.markilue.interview;
|
package com.markilue.interview;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -13,6 +15,14 @@ import java.util.List;
|
||||||
*/
|
*/
|
||||||
public class Question1 {
|
public class Question1 {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test(){
|
||||||
|
|
||||||
|
ListNode root = ListNodeUtils.build(new int[]{1, 2, 3, 4, 5});
|
||||||
|
ListNodeUtils.print(reorderList(root));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
|
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue