From ef108c1def7203c7a7f3f5384d40599e341b191c Mon Sep 17 00:00:00 2001 From: markilue <745518019@qq.com> Date: Tue, 28 Mar 2023 22:10:09 +0800 Subject: [PATCH 1/2] =?UTF-8?q?leecode=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../markilue/leecode/test/TestByteDance.java | 80 +++++++++++++++++++ .../markilue/leecode/test/TestByteDance4.java | 47 +++++++++++ 2 files changed, 127 insertions(+) create mode 100644 Leecode/src/main/java/com/markilue/leecode/test/TestByteDance.java create mode 100644 Leecode/src/main/java/com/markilue/leecode/test/TestByteDance4.java diff --git a/Leecode/src/main/java/com/markilue/leecode/test/TestByteDance.java b/Leecode/src/main/java/com/markilue/leecode/test/TestByteDance.java new file mode 100644 index 0000000..373c116 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/test/TestByteDance.java @@ -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 cur = new ArrayList(); + + @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)); + } +} diff --git a/Leecode/src/main/java/com/markilue/leecode/test/TestByteDance4.java b/Leecode/src/main/java/com/markilue/leecode/test/TestByteDance4.java new file mode 100644 index 0000000..ebdd7cd --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/test/TestByteDance4.java @@ -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> map = new HashMap<>(); + + //原始情况 + for (String string : strings) { + String[] split = string.split("/"); + for (String s : split) { + map.getOrDefault(s,new HashSet<>()); +// map.put() + } + } + + + } +} From e6858db14413597a0a87f330f2494f354cfb3806 Mon Sep 17 00:00:00 2001 From: markilue <745518019@qq.com> Date: Wed, 29 Mar 2023 21:08:58 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=94=E8=AF=95=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../markilue/leecode/hot100/T55_LRUCache.java | 2 - .../markilue/leecode/hot100/T77_Codec.java | 277 ++++++++++++++++++ interview/Ctrip/pom.xml | 43 +++ .../com/markilue/interview/Question1.java | 39 +++ .../com/markilue/interview/Question2.java | 53 ++++ .../com/markilue/interview/Question3.java | 134 +++++++++ interview/Tencent/pom.xml | 60 ++++ .../com/markilue/interview/ListNodeUtils.java | 55 ++++ .../com/markilue/interview/Question1.java | 10 + 9 files changed, 671 insertions(+), 2 deletions(-) create mode 100644 Leecode/src/main/java/com/markilue/leecode/hot100/T77_Codec.java create mode 100644 interview/Ctrip/pom.xml create mode 100644 interview/Ctrip/src/main/java/com/markilue/interview/Question1.java create mode 100644 interview/Ctrip/src/main/java/com/markilue/interview/Question2.java create mode 100644 interview/Ctrip/src/main/java/com/markilue/interview/Question3.java create mode 100644 interview/Tencent/pom.xml create mode 100644 interview/Tencent/src/main/java/com/markilue/interview/ListNodeUtils.java diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/T55_LRUCache.java b/Leecode/src/main/java/com/markilue/leecode/hot100/T55_LRUCache.java index 07f6edc..c71bb02 100644 --- a/Leecode/src/main/java/com/markilue/leecode/hot100/T55_LRUCache.java +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/T55_LRUCache.java @@ -1,9 +1,7 @@ package com.markilue.leecode.hot100; import org.junit.Test; -import org.omg.CORBA.PUBLIC_MEMBER; -import java.util.ArrayList; import java.util.HashMap; import java.util.Map; diff --git a/Leecode/src/main/java/com/markilue/leecode/hot100/T77_Codec.java b/Leecode/src/main/java/com/markilue/leecode/hot100/T77_Codec.java new file mode 100644 index 0000000..eea0124 --- /dev/null +++ b/Leecode/src/main/java/com/markilue/leecode/hot100/T77_Codec.java @@ -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 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 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 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 treeList = new LinkedList<>(); + LinkedList 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 dataList = new LinkedList(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 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; + } + + + +} diff --git a/interview/Ctrip/pom.xml b/interview/Ctrip/pom.xml new file mode 100644 index 0000000..5bf7c80 --- /dev/null +++ b/interview/Ctrip/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + com.markilue.interview + Ctrip + 1.0-SNAPSHOT + + + 8 + 8 + + + + + junit + junit + 4.13.2 + test + + + junit + junit + 4.13.2 + compile + + + org.projectlombok + lombok + RELEASE + compile + + + junit + junit + 4.13.1 + compile + + + + \ No newline at end of file diff --git a/interview/Ctrip/src/main/java/com/markilue/interview/Question1.java b/interview/Ctrip/src/main/java/com/markilue/interview/Question1.java new file mode 100644 index 0000000..96646ff --- /dev/null +++ b/interview/Ctrip/src/main/java/com/markilue/interview/Question1.java @@ -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); + + + } +} diff --git a/interview/Ctrip/src/main/java/com/markilue/interview/Question2.java b/interview/Ctrip/src/main/java/com/markilue/interview/Question2.java new file mode 100644 index 0000000..41f0e28 --- /dev/null +++ b/interview/Ctrip/src/main/java/com/markilue/interview/Question2.java @@ -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]+" "); + } + + + } +} diff --git a/interview/Ctrip/src/main/java/com/markilue/interview/Question3.java b/interview/Ctrip/src/main/java/com/markilue/interview/Question3.java new file mode 100644 index 0000000..779bff3 --- /dev/null +++ b/interview/Ctrip/src/main/java/com/markilue/interview/Question3.java @@ -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 jie = new HashMap<>(); + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int nums = sc.nextInt(); +// getJieCheng(); + +// for (Map.Entry entry : jie.entrySet()) { +// +// System.out.println(entry.getKey()+":"+entry.getValue()); +// } + new Question3().sovle(nums); + } + + public void sovle(int nums) { + + //寻找num的最大因数 + List 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 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 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 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; + } + } + } + + + +} diff --git a/interview/Tencent/pom.xml b/interview/Tencent/pom.xml new file mode 100644 index 0000000..e45ab83 --- /dev/null +++ b/interview/Tencent/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + org.example + Leecode + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 8 + 8 + + + + + + + + + + + + + + + org.projectlombok + lombok + 1.18.12 + provided + + + + + junit + junit + 4.13.2 + test + + + junit + junit + 4.13.2 + compile + + + org.projectlombok + lombok + RELEASE + compile + + + + + \ No newline at end of file diff --git a/interview/Tencent/src/main/java/com/markilue/interview/ListNodeUtils.java b/interview/Tencent/src/main/java/com/markilue/interview/ListNodeUtils.java new file mode 100644 index 0000000..f7a0168 --- /dev/null +++ b/interview/Tencent/src/main/java/com/markilue/interview/ListNodeUtils.java @@ -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()); + + } +} diff --git a/interview/Tencent/src/main/java/com/markilue/interview/Question1.java b/interview/Tencent/src/main/java/com/markilue/interview/Question1.java index 19c9559..6b4597e 100644 --- a/interview/Tencent/src/main/java/com/markilue/interview/Question1.java +++ b/interview/Tencent/src/main/java/com/markilue/interview/Question1.java @@ -1,5 +1,7 @@ package com.markilue.interview; +import org.junit.Test; + import java.util.ArrayList; import java.util.List; @@ -13,6 +15,14 @@ import java.util.List; */ public class Question1 { + @Test + public void test(){ + + ListNode root = ListNodeUtils.build(new int[]{1, 2, 3, 4, 5}); + ListNodeUtils.print(reorderList(root)); + + } + /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 *