From 59987eb7ff39a153052f20eb88027f22fbca2feb Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月14日 22:44:14 +0800 Subject: [PATCH 001/115] =?UTF-8?q?add:=20LeetCode116=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/tree/leetcode116/Solution.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ds/tree/leetcode116/Solution.java b/src/main/java/ds/tree/leetcode116/Solution.java index 57c88d3..20c7dea 100644 --- a/src/main/java/ds/tree/leetcode116/Solution.java +++ b/src/main/java/ds/tree/leetcode116/Solution.java @@ -3,6 +3,7 @@ /** * 填充每个节点的下一个右侧节点指针 * LeetCode 116 https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/ + * eg: https://assets.leetcode.com/uploads/2019/02/14/116_sample.png * * @author yangyi 2020年11月25日11:10:04 */ From f6f798caf9271d373a4be0c12d2a9012bd9193f5 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月19日 23:32:02 +0800 Subject: [PATCH 002/115] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=88=86=E7=B1=BB=EF=BC=8C=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?leetcode222=EF=BC=8C=E4=BF=AE=E6=94=B9leercode404=E5=8F=8Aleetc?= =?UTF-8?q?ode617=E7=9A=84=E4=B9=A6=E5=86=99=E4=B9=A0=E6=83=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/main/java/ds/tree/leetcode222/Solution.java | 2 +- src/main/java/ds/tree/leetcode222/TreeNodeCount.java | 2 +- src/main/java/ds/tree/leetcode404/Solution.java | 1 + src/main/java/ds/tree/leetcode617/Solution.java | 2 +- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 61d12db..07e66f3 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,6 @@ - [111.二叉树的最小深度](/src/main/java/ds/tree/leetcode111/Solution.java) - [二叉树的节点个数](/src/main/java/ds/tree/leetcode222/TreeNodeCount.java) - [222.完全二叉树的节点个数](/src/main/java/ds/tree/leetcode222/Solution.java) - - [257.二叉树的所有路径](/src/main/java/ds/tree/leetcode257/Solution.java) - [572.另一个树的子树](/src/main/java/ds/tree/leetcode572/Solution.java) - [404.左叶子之和](/src/main/java/ds/tree/leetcode404/Solution.java) - [617.合并二叉树](/src/main/java/ds/tree/leetcode617/Solution.java) @@ -195,7 +194,8 @@ - 路径 - [112.路径总和](/src/main/java/ds/tree/leetcode112/Solution.java) - [113.路径总和II](/src/main/java/ds/tree/leetcode113/Solution.java) - + - [257.二叉树的所有路径](/src/main/java/ds/tree/leetcode257/Solution.java) + 10. 广度优先搜索(BFS) - [111.二叉树的最小深度](/src/main/java/ds/bfs/leetcode111/Solution.java) - [102.二叉树的层序遍历](/src/main/java/ds/bfs/leetcode102/Solution.java) diff --git a/src/main/java/ds/tree/leetcode222/Solution.java b/src/main/java/ds/tree/leetcode222/Solution.java index 89d40a9..1b11756 100644 --- a/src/main/java/ds/tree/leetcode222/Solution.java +++ b/src/main/java/ds/tree/leetcode222/Solution.java @@ -52,9 +52,9 @@ public int countNodes2(TreeNode root) { if (root == null) { return 0; } + int nodeNum = 0; Queue
queue = new LinkedList(); queue.offer(root); - int nodeNum = 0; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { diff --git a/src/main/java/ds/tree/leetcode222/TreeNodeCount.java b/src/main/java/ds/tree/leetcode222/TreeNodeCount.java index 0480d47..55cd204 100644 --- a/src/main/java/ds/tree/leetcode222/TreeNodeCount.java +++ b/src/main/java/ds/tree/leetcode222/TreeNodeCount.java @@ -63,8 +63,8 @@ public int countTree2(TreeNode root) { if (root == null) { return 0; } - Queue
queue = new LinkedList(); int count = 0; + Queue
queue = new LinkedList(); queue.offer(root); while (!queue.isEmpty()) { int size = queue.size(); diff --git a/src/main/java/ds/tree/leetcode404/Solution.java b/src/main/java/ds/tree/leetcode404/Solution.java index eab8074..7478278 100644 --- a/src/main/java/ds/tree/leetcode404/Solution.java +++ b/src/main/java/ds/tree/leetcode404/Solution.java @@ -3,6 +3,7 @@ /** * 左叶子之和 * LeetCode 404 https://leetcode-cn.com/problems/sum-of-left-leaves/ + * https://tva1.sinaimg.cn/large/e6c9d24ely1h19ol4nngyj20hy0jbgm5.jpg * * @author yangyi 2021年02月04日17:33:34 */ diff --git a/src/main/java/ds/tree/leetcode617/Solution.java b/src/main/java/ds/tree/leetcode617/Solution.java index c1a0b57..cc97262 100644 --- a/src/main/java/ds/tree/leetcode617/Solution.java +++ b/src/main/java/ds/tree/leetcode617/Solution.java @@ -6,7 +6,7 @@ import java.util.Queue; /** - * 合并二叉树 + * 合并二叉树(二叉树相加) * LeetCode 617 https://leetcode-cn.com/problems/merge-two-binary-trees/ * * @author yangyi 2021年02月12日20:38:40 From 1b8563b8ad63732b0804fe1aece9493c8b0c65bf Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月20日 00:18:29 +0800 Subject: [PATCH 003/115] =?UTF-8?q?297.=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=8E=E5=8F=8D=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../Solution.java} | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) rename src/main/java/ds/tree/{SerializePreOrderTree.java => leetcode297/Solution.java} (91%) diff --git a/README.md b/README.md index 07e66f3..a5ba5c1 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ - [105.从前序与中序遍历序列构造二叉树](/src/main/java/ds/tree/leetcode105/Solution.java) - [106.从中序与后序遍历序列构造二叉树](/src/main/java/ds/tree/leetcode106/Solution.java) - [寻找重复的子树](/src/main/java/ds/tree/FindDuplicateSubtrees.java) - - [二叉树的序列化和反序列化(前序遍历的序列化方式实现)](/src/main/java/ds/tree/SerializePreOrderTree.java) + - [二叉树的序列化和反序列化(前序遍历的序列化方式实现)](/src/main/java/ds/tree/leetcode297/Solution.java) - [二叉树的序列化和反序列化(后序遍历的序列化方式实现)](/src/main/java/ds/tree/SerializePostOrderTree.java) - [110.平衡二叉树](/src/main/java/ds/tree/leetcode110/Solution.java) - [剑指offer 55.二叉树的深度](/src/main/java/ds/tree/targetoffer55/Solution.java) diff --git a/src/main/java/ds/tree/SerializePreOrderTree.java b/src/main/java/ds/tree/leetcode297/Solution.java similarity index 91% rename from src/main/java/ds/tree/SerializePreOrderTree.java rename to src/main/java/ds/tree/leetcode297/Solution.java index baa90ad..117ef3f 100644 --- a/src/main/java/ds/tree/SerializePreOrderTree.java +++ b/src/main/java/ds/tree/leetcode297/Solution.java @@ -1,4 +1,4 @@ -package ds.tree; +package ds.tree.leetcode297; import java.util.Arrays; import java.util.LinkedList; @@ -9,7 +9,7 @@ * * @author yangyi 2020年12月07日15:28:43 */ -public class SerializePreOrderTree { +public class Solution { public class TreeNode { int val; @@ -83,10 +83,10 @@ private void inOrder(TreeNode root) { public static void main(String[] args) { //Your Codec object will be instantiated and called as such: - SerializePreOrderTree create = new SerializePreOrderTree(); + Solution create = new Solution(); TreeNode root = create.createTree(); - SerializePreOrderTree ser = new SerializePreOrderTree(); - SerializePreOrderTree deser = new SerializePreOrderTree(); + Solution ser = new Solution(); + Solution deser = new Solution(); TreeNode ans = deser.deserialize(ser.serialize(root)); System.out.println("中序遍历输出序列化和反序列化之后的结果: "); create.inOrder(ans); From 1fe9d9b0e028cb06a031fe39da4f857300b33542 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月21日 23:46:32 +0800 Subject: [PATCH 004/115] =?UTF-8?q?783.=20=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E8=8A=82=E7=82=B9=E6=9C=80=E5=B0=8F=E8=B7=9D?= =?UTF-8?q?=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/bst/leetcode530/Solution.java | 2 +- .../java/ds/bst/leetcode783/Solution.java | 89 +++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ds/bst/leetcode783/Solution.java diff --git a/README.md b/README.md index a5ba5c1..60e6328 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ - [700.二叉搜索树中的搜索](/src/main/java/ds/bst/leetcode700/Solution.java) - [98.验证二叉搜索树](/src/main/java/ds/bst/leetcode98/Solution.java) - [530.二叉搜索树的最小绝对差](/src/main/java/ds/bst/leetcode530/Solution.java) + - [783.二叉搜索树节点最小距离](/src/main/java/ds/bst/leetcode783/Solution.java) - [501.二叉搜索树中的众数](/src/main/java/ds/bst/leetcode501/Solution.java) - [二叉搜索树中第K小的元素](/src/main/java/ds/bst/KthSmallestInBST.java) - [538.把二叉搜索树转换为累加树](/src/main/java/ds/bst/leetcode538/Solution.java) diff --git a/src/main/java/ds/bst/leetcode530/Solution.java b/src/main/java/ds/bst/leetcode530/Solution.java index 85f6e58..f7c4862 100644 --- a/src/main/java/ds/bst/leetcode530/Solution.java +++ b/src/main/java/ds/bst/leetcode530/Solution.java @@ -29,7 +29,7 @@ public class TreeNode { */ public int getMinimumDifference(TreeNode root) { inOrder(root); - if (result.size() < 2) { + if (result.size() <= 1) { return 0; } int min = Integer.MAX_VALUE; diff --git a/src/main/java/ds/bst/leetcode783/Solution.java b/src/main/java/ds/bst/leetcode783/Solution.java new file mode 100644 index 0000000..aa5d8e7 --- /dev/null +++ b/src/main/java/ds/bst/leetcode783/Solution.java @@ -0,0 +1,89 @@ +package ds.bst.leetcode783; + +import java.util.LinkedList; + +/** + * 二叉搜索树节点最小距离 + * LeetCode 783 https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/ + * + * @author yangyi 2022年04月19日16:20:47 + */ +public class Solution { + + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode() { + } + + TreeNode(int val) { + this.val = val; + } + + TreeNode(int val, TreeNode left, TreeNode right) { + this.val = val; + this.left = left; + this.right = right; + } + } + + public LinkedList result = new LinkedList(); + + private void inOrder(TreeNode root) { + if (root == null) { + return; + } + inOrder(root.left); + result.add(root.val); + inOrder(root.right); + } + + public int minDiffInBST(TreeNode root) { + inOrder(root); + if (result.size() <= 1) { + return 0; + } + int min = Integer.MAX_VALUE; + for (int i = 1; i < result.size(); i++) { + min = Math.min(min, Math.abs(result.get(i) - result.get(i - 1))); + } + return min; + } + + private TreeNode createTree(){ + TreeNode node_4 = new TreeNode(4); + TreeNode node_2 = new TreeNode(2); + TreeNode node_6 = new TreeNode(6); + TreeNode node_1 = new TreeNode(1); + TreeNode node_3 = new TreeNode(3); + node_4.left = node_2; + node_4.right = node_6; + node_2.left = node_1; + node_2.right = node_3; + return node_4; + } + + private TreeNode createTree2(){ + TreeNode node_1 = new TreeNode(1); + TreeNode node_0 = new TreeNode(0); + TreeNode node_48 = new TreeNode(48); + TreeNode node_12 = new TreeNode(12); + TreeNode node_49 = new TreeNode(49); + node_1.left = node_0; + node_1.right = node_48; + node_48.left = node_12; + node_48.right = node_49; + return node_1; + } + + public static void main(String[] args) { + Solution solution = new Solution(); + TreeNode tree = solution.createTree(); + TreeNode tree2 = solution.createTree2(); + System.out.println(solution.minDiffInBST(tree)); + System.out.println(solution.minDiffInBST(tree2)); + } + +} From 8f4982035aed1250ee13a4c8f30fa274b803e79c Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月22日 00:26:26 +0800 Subject: [PATCH 005/115] =?UTF-8?q?114.=20=E5=B0=86=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=B1=95=E5=BC=80=E4=B8=BA=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../Solution.java} | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) rename src/main/java/ds/tree/{FlattenLink.java => leetcode114/Solution.java} (88%) diff --git a/README.md b/README.md index 60e6328..dce3547 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ - [100.相同的树](/src/main/java/ds/tree/leetcode100/Solution.java) - [101.对称二叉树](/src/main/java/ds/tree/leetcode101/Solution.java) - [116.填充每个节点的下一个右侧节点指针](/src/main/java/ds/tree/leetcode116/Solution.java) - - [将二叉树展开为链表](/src/main/java/ds/tree/FlattenLink.java) + - [114.将二叉树展开为链表](/src/main/java/ds/tree/leetcode114/Solution.java) - [654.最大二叉树](/src/main/java/ds/tree/leetcode654/Solution.java) - [105.从前序与中序遍历序列构造二叉树](/src/main/java/ds/tree/leetcode105/Solution.java) - [106.从中序与后序遍历序列构造二叉树](/src/main/java/ds/tree/leetcode106/Solution.java) diff --git a/src/main/java/ds/tree/FlattenLink.java b/src/main/java/ds/tree/leetcode114/Solution.java similarity index 88% rename from src/main/java/ds/tree/FlattenLink.java rename to src/main/java/ds/tree/leetcode114/Solution.java index ddc8b13..4eac0ab 100644 --- a/src/main/java/ds/tree/FlattenLink.java +++ b/src/main/java/ds/tree/leetcode114/Solution.java @@ -1,12 +1,13 @@ -package ds.tree; +package ds.tree.leetcode114; /** * 将二叉树展开为链表 * LeetCode 114 https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/ + * https://assets.leetcode.com/uploads/2021/01/14/flaten.jpg * * @author yangyi 2020年11月25日16:22:39 */ -public class FlattenLink { +public class Solution { public static class TreeNode { int val; @@ -93,14 +94,14 @@ private void preOrder(TreeNode root) { } public static void main(String[] args) { - FlattenLink flattenLink = new FlattenLink(); - TreeNode root = flattenLink.createTree(); + Solution solution = new Solution(); + TreeNode root = solution.createTree(); System.out.println("中序遍历创建完成的二叉树: "); - flattenLink.inOrder(root); + solution.inOrder(root); System.out.println(); System.out.println("将此二叉树拉平成为一个链表: "); - flattenLink.flatten(root); + solution.flatten(root); System.out.println("打印这条二叉树被拉平后生成的链表: "); - flattenLink.preOrder(root); + solution.preOrder(root); } } From 284897953b01e1adb9060e49e9a623fa26d0bd6d Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月23日 23:55:47 +0800 Subject: [PATCH 006/115] =?UTF-8?q?297.=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=8E=E5=8F=8D=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96=20=EF=BC=88=E5=8C=BA=E5=88=86=E5=89=8D?= =?UTF-8?q?=E5=BA=8F=E9=81=8D=E5=8E=86=E5=90=8E=E5=90=8E=E5=BA=8F=E9=81=8D?= =?UTF-8?q?=E5=8E=86=E4=B8=A4=E7=A7=8D=E5=AE=9E=E7=8E=B0=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- .../postorder/Solution.java} | 10 +++++----- .../ds/tree/leetcode297/{ => preorder}/Solution.java | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) rename src/main/java/ds/tree/{SerializePostOrderTree.java => leetcode297/postorder/Solution.java} (90%) rename src/main/java/ds/tree/leetcode297/{ => preorder}/Solution.java (98%) diff --git a/README.md b/README.md index dce3547..3d49bec 100644 --- a/README.md +++ b/README.md @@ -89,8 +89,8 @@ - [105.从前序与中序遍历序列构造二叉树](/src/main/java/ds/tree/leetcode105/Solution.java) - [106.从中序与后序遍历序列构造二叉树](/src/main/java/ds/tree/leetcode106/Solution.java) - [寻找重复的子树](/src/main/java/ds/tree/FindDuplicateSubtrees.java) - - [二叉树的序列化和反序列化(前序遍历的序列化方式实现)](/src/main/java/ds/tree/leetcode297/Solution.java) - - [二叉树的序列化和反序列化(后序遍历的序列化方式实现)](/src/main/java/ds/tree/SerializePostOrderTree.java) + - [297. 二叉树的序列化和反序列化(前序遍历的序列化方式实现)](/src/main/java/ds/tree/leetcode297/preorder/Solution.java) + - [297. 二叉树的序列化和反序列化(后序遍历的序列化方式实现)](/src/main/java/ds/tree/leetcode297/postorder/Solution.java) - [110.平衡二叉树](/src/main/java/ds/tree/leetcode110/Solution.java) - [剑指offer 55.二叉树的深度](/src/main/java/ds/tree/targetoffer55/Solution.java) - [104.二叉树的最大深度](/src/main/java/ds/tree/leetcode104/Solution.java) diff --git a/src/main/java/ds/tree/SerializePostOrderTree.java b/src/main/java/ds/tree/leetcode297/postorder/Solution.java similarity index 90% rename from src/main/java/ds/tree/SerializePostOrderTree.java rename to src/main/java/ds/tree/leetcode297/postorder/Solution.java index 1223971..e791782 100644 --- a/src/main/java/ds/tree/SerializePostOrderTree.java +++ b/src/main/java/ds/tree/leetcode297/postorder/Solution.java @@ -1,4 +1,4 @@ -package ds.tree; +package ds.tree.leetcode297.postorder; import java.util.Arrays; import java.util.LinkedList; @@ -9,7 +9,7 @@ * * @author yangyi 2020年12月07日15:28:43 */ -public class SerializePostOrderTree { +public class Solution { public class TreeNode { int val; @@ -82,10 +82,10 @@ private void inOrder(TreeNode root) { public static void main(String[] args) { //Your Codec object will be instantiated and called as such: - SerializePostOrderTree create = new SerializePostOrderTree(); + Solution create = new Solution(); TreeNode root = create.createTree(); - SerializePostOrderTree ser = new SerializePostOrderTree(); - SerializePostOrderTree deser = new SerializePostOrderTree(); + Solution ser = new Solution(); + Solution deser = new Solution(); TreeNode ans = deser.deserialize(ser.serialize(root)); System.out.println("中序遍历输出序列化和反序列化之后的结果: "); create.inOrder(ans); diff --git a/src/main/java/ds/tree/leetcode297/Solution.java b/src/main/java/ds/tree/leetcode297/preorder/Solution.java similarity index 98% rename from src/main/java/ds/tree/leetcode297/Solution.java rename to src/main/java/ds/tree/leetcode297/preorder/Solution.java index 117ef3f..365fed1 100644 --- a/src/main/java/ds/tree/leetcode297/Solution.java +++ b/src/main/java/ds/tree/leetcode297/preorder/Solution.java @@ -1,4 +1,4 @@ -package ds.tree.leetcode297; +package ds.tree.leetcode297.preorder; import java.util.Arrays; import java.util.LinkedList; From c0c807df2dac9241a34bd16c3ead5565a2f7cbe4 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月24日 00:00:53 +0800 Subject: [PATCH 007/115] =?UTF-8?q?297.=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=8E=E5=8F=8D=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/bst/leetcode501/Solution.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ds/bst/leetcode501/Solution.java b/src/main/java/ds/bst/leetcode501/Solution.java index 05f15b9..58723d6 100644 --- a/src/main/java/ds/bst/leetcode501/Solution.java +++ b/src/main/java/ds/bst/leetcode501/Solution.java @@ -32,11 +32,11 @@ public int[] findMode(TreeNode root) { return new int[]{}; } search(root); - int[] res = new int[result.size()]; - for (int i = 0; i < result.size(); i++) { - res[i] = result.get(i); - } - return res; + // int[] res = new int[result.size()]; +// for (int i = 0; i < result.size(); i++) { +// res[i] = result.get(i); +// } + return result.stream().mapToInt(value -> value).toArray(); } private void search(TreeNode cur) { From 6bc7ac673a5152de6a2451e41eeda87462da77bf Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月28日 00:31:32 +0800 Subject: [PATCH 008/115] =?UTF-8?q?230.=20=E4=BA=8C=E5=8F=89=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=AC=ACK=E5=B0=8F=E7=9A=84?= =?UTF-8?q?=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../Solution.java} | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) rename src/main/java/ds/bst/{KthSmallestInBST.java => leetcode230/Solution.java} (86%) diff --git a/README.md b/README.md index 3d49bec..13bda82 100644 --- a/README.md +++ b/README.md @@ -111,7 +111,7 @@ - [530.二叉搜索树的最小绝对差](/src/main/java/ds/bst/leetcode530/Solution.java) - [783.二叉搜索树节点最小距离](/src/main/java/ds/bst/leetcode783/Solution.java) - [501.二叉搜索树中的众数](/src/main/java/ds/bst/leetcode501/Solution.java) - - [二叉搜索树中第K小的元素](/src/main/java/ds/bst/KthSmallestInBST.java) + - [230.二叉搜索树中第K小的元素](/src/main/java/ds/bst/leetcode230/Solution.java) - [538.把二叉搜索树转换为累加树](/src/main/java/ds/bst/leetcode538/Solution.java) - [701.二叉搜索树中的插入操作](/src/main/java/ds/bst/leetcode701/Solution.java) - [删除二叉搜索树中的节点](/src/main/java/ds/bst/BstDelete.java) diff --git a/src/main/java/ds/bst/KthSmallestInBST.java b/src/main/java/ds/bst/leetcode230/Solution.java similarity index 86% rename from src/main/java/ds/bst/KthSmallestInBST.java rename to src/main/java/ds/bst/leetcode230/Solution.java index db8a2ab..a61f98f 100644 --- a/src/main/java/ds/bst/KthSmallestInBST.java +++ b/src/main/java/ds/bst/leetcode230/Solution.java @@ -1,4 +1,4 @@ -package ds.bst; +package ds.bst.leetcode230; /** * 二叉搜索树中第K小的元素 @@ -6,7 +6,7 @@ * * @author yangyi 2019年02月10日10:34:52 */ -public class KthSmallestInBST { +public class Solution { public class TreeNode { int val; @@ -76,12 +76,12 @@ private void inOrder(TreeNode root) { } public static void main(String[] args) { - KthSmallestInBST kthSmallestInBST = new KthSmallestInBST(); - TreeNode root = kthSmallestInBST.createTreeNode(); + Solution solution = new Solution(); + TreeNode root = solution.createTreeNode(); System.out.println("中序遍历构造出来的第一棵树: "); - kthSmallestInBST.inOrder(root); + solution.inOrder(root); System.out.println(); System.out.println("搜索此树的第1个最小的元素: "); - System.out.println(kthSmallestInBST.kthSmallest(root, 1)); + System.out.println(solution.kthSmallest(root, 1)); } } From d23cd28eaca648f4906755429f368df3aa13fad2 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月29日 00:21:07 +0800 Subject: [PATCH 009/115] =?UTF-8?q?450.=20=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../{BstDelete.java => leetcode450/Solution.java} | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) rename src/main/java/ds/bst/{BstDelete.java => leetcode450/Solution.java} (90%) diff --git a/README.md b/README.md index 13bda82..bc0ba09 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ - [230.二叉搜索树中第K小的元素](/src/main/java/ds/bst/leetcode230/Solution.java) - [538.把二叉搜索树转换为累加树](/src/main/java/ds/bst/leetcode538/Solution.java) - [701.二叉搜索树中的插入操作](/src/main/java/ds/bst/leetcode701/Solution.java) - - [删除二叉搜索树中的节点](/src/main/java/ds/bst/BstDelete.java) + - [450.删除二叉搜索树中的节点](/src/main/java/ds/bst/leetcode450/Solution.java) - [二叉查找树的插入、遍历、查找、删除、反转](/src/main/java/ds/bst/BinarySearchTree.java) - [二叉查找树的最近公共祖先](/src/main/java/ds/bst/BSTreeLowestCommonAncestor.java) - [剑指offer 36. 二叉搜索树与双向链表](/src/main/java/ds/bst/targetoffer36/Solution.java) diff --git a/src/main/java/ds/bst/BstDelete.java b/src/main/java/ds/bst/leetcode450/Solution.java similarity index 90% rename from src/main/java/ds/bst/BstDelete.java rename to src/main/java/ds/bst/leetcode450/Solution.java index 89433e6..c8f56ea 100644 --- a/src/main/java/ds/bst/BstDelete.java +++ b/src/main/java/ds/bst/leetcode450/Solution.java @@ -1,4 +1,4 @@ -package ds.bst; +package ds.bst.leetcode450; /** * 删除二叉搜索树中的节点 @@ -6,7 +6,7 @@ * * @author yangyi 2020年12月05日16:11:25 */ -public class BstDelete { +public class Solution { public class TreeNode { int val; @@ -93,15 +93,15 @@ private TreeNode getMin(TreeNode root) { } public static void main(String[] args) { - BstDelete bstDelete = new BstDelete(); + Solution solution = new Solution(); System.out.println("构建一棵树"); - TreeNode root = bstDelete.createTree(); + TreeNode root = solution.createTree(); System.out.println("中序遍历构建好的树: "); - bstDelete.inOrder(root); + solution.inOrder(root); System.out.println(); System.out.println("删除树中值为3的节点: "); - TreeNode result = bstDelete.deleteNode(root, 3); + TreeNode result = solution.deleteNode(root, 3); System.out.println("中序遍历删除对应的3节点之后剩余的树: "); - bstDelete.inOrder(result); + solution.inOrder(result); } } From 698bc818c4d299bfa4f3dcb39ff3b8b76e5be272 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月30日 23:59:23 +0800 Subject: [PATCH 010/115] =?UTF-8?q?235.=20=E4=BA=8C=E5=8F=89=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=E6=A0=91=E7=9A=84=E6=9C=80=E8=BF=91=E5=85=AC=E5=85=B1?= =?UTF-8?q?=E7=A5=96=E5=85=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../ds/bst/BSTreeLowestCommonAncestor.java | 73 ------------------- .../java/ds/bst/leetcode235/Solution.java | 67 +++++++++++++++++ 3 files changed, 68 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/ds/bst/BSTreeLowestCommonAncestor.java create mode 100644 src/main/java/ds/bst/leetcode235/Solution.java diff --git a/README.md b/README.md index bc0ba09..e5a86ad 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ - [701.二叉搜索树中的插入操作](/src/main/java/ds/bst/leetcode701/Solution.java) - [450.删除二叉搜索树中的节点](/src/main/java/ds/bst/leetcode450/Solution.java) - [二叉查找树的插入、遍历、查找、删除、反转](/src/main/java/ds/bst/BinarySearchTree.java) - - [二叉查找树的最近公共祖先](/src/main/java/ds/bst/BSTreeLowestCommonAncestor.java) + - [235.二叉查找树的最近公共祖先](/src/main/java/ds/bst/leetcode235/Solution.java) - [剑指offer 36. 二叉搜索树与双向链表](/src/main/java/ds/bst/targetoffer36/Solution.java) - [108.将有序数组转换为二叉搜索树](/src/main/java/ds/bst/leetcode108/Solution.java) - [109.有序链表转换二叉搜索树](/src/main/java/ds/bst/leetcode109/Solution.java) diff --git a/src/main/java/ds/bst/BSTreeLowestCommonAncestor.java b/src/main/java/ds/bst/BSTreeLowestCommonAncestor.java deleted file mode 100644 index 8ab34ae..0000000 --- a/src/main/java/ds/bst/BSTreeLowestCommonAncestor.java +++ /dev/null @@ -1,73 +0,0 @@ -package ds.bst; - -/** - * 二叉搜索树的最近公共祖先 - *

- *

- * 给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先。 - *

- * 百度百科中最近公共祖先的定义为:"对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。" - *

- * 例如,给定如下二叉搜索树: root = [6,2,8,0,4,7,9,null,null,3,5] - *

- *

- *

- *

- *

- * 示例 1: - *

- * 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 - * 输出: 6 - * 解释: 节点 2 和节点 8 的最近公共祖先是 6。 - * 示例 2: - *

- * 输入: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 - * 输出: 2 - * 解释: 节点 2 和节点 4 的最近公共祖先是 2, 因为根据定义最近公共祖先节点可以为节点本身。 - *

- *

- * 说明: - *

- * 所有节点的值都是唯一的。 - * p、q 为不同节点且均存在于给定的二叉搜索树中。 - *

- * - * @author yangyi 2019年01月25日19:15:29 - */ -public class BSTreeLowestCommonAncestor { - - public BinarySearchTree.TreeNode lowestCommonAncestor(BinarySearchTree.TreeNode root, BinarySearchTree.TreeNode p, BinarySearchTree.TreeNode q) { - if (p.value < root.value && q.value < root.value) { - return lowestCommonAncestor( - root.left, - new BinarySearchTree.TreeNode(p.value), - new BinarySearchTree.TreeNode(q.value)); - } else if (p.value> root.value && q.value> root.value) { - return lowestCommonAncestor( - root.right, - new BinarySearchTree.TreeNode(p.value), - new BinarySearchTree.TreeNode(q.value)); - } else { - return root; - } - } - - private BinarySearchTree.TreeNode createTree() { - BinarySearchTree binarySearchTree = new BinarySearchTree(); - BinarySearchTree.TreeNode node = null; - int[] array = {6, 2, 8, 0, 4, 7, 9, 0, 0, 3, 5}; - for (int i : array) { - node = binarySearchTree.insert(i); - } - return node; - } - - public static void main(String[] args) { - BSTreeLowestCommonAncestor bsTreeLowestCommonAncestor = new BSTreeLowestCommonAncestor(); - BinarySearchTree.TreeNode resultNode = bsTreeLowestCommonAncestor.lowestCommonAncestor( - bsTreeLowestCommonAncestor.createTree(), - new BinarySearchTree.TreeNode(2), - new BinarySearchTree.TreeNode(8)); - System.out.println("输出结果为:" + resultNode.value); - } -} diff --git a/src/main/java/ds/bst/leetcode235/Solution.java b/src/main/java/ds/bst/leetcode235/Solution.java new file mode 100644 index 0000000..431755f --- /dev/null +++ b/src/main/java/ds/bst/leetcode235/Solution.java @@ -0,0 +1,67 @@ +package ds.bst.leetcode235; + +/** + * 二叉搜索树的最近公共祖先 + * LeetCode 235 https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/submissions/ + * 剑指 Offer 68 - I https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/ + * + * @author yangyi 2019年01月25日19:15:29 + */ +public class Solution { + + public static class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } + } + + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (p.val < root.val && q.val < root.val) { + return lowestCommonAncestor( + root.left, + new TreeNode(p.val), + new TreeNode(q.val)); + } else if (p.val> root.val && q.val> root.val) { + return lowestCommonAncestor( + root.right, + new TreeNode(p.val), + new TreeNode(q.val)); + } else { + return root; + } + } + + private TreeNode createTree() { + TreeNode node_6 = new TreeNode(6); + TreeNode node_2 = new TreeNode(2); + TreeNode node_8 = new TreeNode(8); + TreeNode node_0 = new TreeNode(0); + TreeNode node_4 = new TreeNode(4); + TreeNode node_7 = new TreeNode(7); + TreeNode node_9 = new TreeNode(9); + TreeNode node_3 = new TreeNode(3); + TreeNode node_5 = new TreeNode(5); + node_6.left = node_2; + node_6.right = node_8; + node_2.left = node_0; + node_2.right = node_4; + node_4.left = node_3; + node_4.right = node_5; + node_8.left = node_7; + node_8.right = node_9; + return node_6; + } + + public static void main(String[] args) { + Solution solution = new Solution(); + TreeNode resultNode = solution.lowestCommonAncestor( + solution.createTree(), + new TreeNode(2), + new TreeNode(8)); + System.out.println("输出结果为:" + resultNode.val); + } +} From bc2b055a963f05b2f27065f0109c6ad60f9ea39a Mon Sep 17 00:00:00 2001 From: yangyi Date: Sun, 1 May 2022 00:08:40 +0800 Subject: [PATCH 011/115] =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E6=8F=92=E5=85=A5=E3=80=81=E9=81=8D=E5=8E=86?= =?UTF-8?q?=E3=80=81=E6=9F=A5=E6=89=BE=E3=80=81=E5=88=A0=E9=99=A4=E3=80=81?= =?UTF-8?q?=E5=8F=8D=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/ds/LevelPrint.java | 2 +- src/main/java/ds/bst/{ => base}/BinarySearchTree.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename src/main/java/ds/bst/{ => base}/BinarySearchTree.java (99%) diff --git a/README.md b/README.md index e5a86ad..82a3f1e 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ - [538.把二叉搜索树转换为累加树](/src/main/java/ds/bst/leetcode538/Solution.java) - [701.二叉搜索树中的插入操作](/src/main/java/ds/bst/leetcode701/Solution.java) - [450.删除二叉搜索树中的节点](/src/main/java/ds/bst/leetcode450/Solution.java) - - [二叉查找树的插入、遍历、查找、删除、反转](/src/main/java/ds/bst/BinarySearchTree.java) + - [二叉查找树的插入、遍历、查找、删除、反转](/src/main/java/ds/bst/base/BinarySearchTree.java) - [235.二叉查找树的最近公共祖先](/src/main/java/ds/bst/leetcode235/Solution.java) - [剑指offer 36. 二叉搜索树与双向链表](/src/main/java/ds/bst/targetoffer36/Solution.java) - [108.将有序数组转换为二叉搜索树](/src/main/java/ds/bst/leetcode108/Solution.java) diff --git a/src/main/java/ds/LevelPrint.java b/src/main/java/ds/LevelPrint.java index 4c7766c..248233e 100644 --- a/src/main/java/ds/LevelPrint.java +++ b/src/main/java/ds/LevelPrint.java @@ -1,6 +1,6 @@ package ds; -import ds.bst.BinarySearchTree; +import ds.bst.base.BinarySearchTree; import java.util.ArrayDeque; import java.util.HashSet; diff --git a/src/main/java/ds/bst/BinarySearchTree.java b/src/main/java/ds/bst/base/BinarySearchTree.java similarity index 99% rename from src/main/java/ds/bst/BinarySearchTree.java rename to src/main/java/ds/bst/base/BinarySearchTree.java index 57de066..8a357da 100644 --- a/src/main/java/ds/bst/BinarySearchTree.java +++ b/src/main/java/ds/bst/base/BinarySearchTree.java @@ -1,4 +1,4 @@ -package ds.bst; +package ds.bst.base; /** * 二叉查找树的插入、遍历、查找、删除 From 4eee93e7e4a27397503ef12e781a86b4736db85f Mon Sep 17 00:00:00 2001 From: yangyi Date: Mon, 2 May 2022 10:52:27 +0800 Subject: [PATCH 012/115] =?UTF-8?q?108.=20=E5=B0=86=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=BD=AC=E6=8D=A2=E4=B8=BA=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ds/bst/leetcode108/Solution.java | 33 +++++++++++++++++- target/classes/ds/LevelPrint.class | Bin 2014 -> 2054 bytes 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/main/java/ds/bst/leetcode108/Solution.java b/src/main/java/ds/bst/leetcode108/Solution.java index ff4c24f..543aae8 100644 --- a/src/main/java/ds/bst/leetcode108/Solution.java +++ b/src/main/java/ds/bst/leetcode108/Solution.java @@ -1,5 +1,11 @@ package ds.bst.leetcode108; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + /** * 将有序数组转换为二叉搜索树 * LeetCode 108 https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/ @@ -45,7 +51,32 @@ private TreeNode bst(int[] nums, int start, int end) { return cur; } + private List

levelOrder(TreeNode root) { + if (root == null) { + return new LinkedList(); + } + List
result = new LinkedList(); + Queue
queue = new ArrayDeque(); + queue.offer(root); + while (!queue.isEmpty()) { + for (int i = 0; i < queue.size(); i++) { + TreeNode cur = queue.poll(); + if (cur != null) { + result.add(cur); + } + if (cur.left != null) { + queue.offer(cur.left); + } + if (cur.right != null) { + queue.offer(cur.right); + } + } + } + return result; + } + public static void main(String[] args) { - System.out.println(new Solution().sortedArrayToBST(new int[]{-10, -3, 0, 5, 9}).val); + List
result = new Solution().levelOrder(new Solution().sortedArrayToBST(new int[]{-10, -3, 0, 5, 9})); + System.out.println(Arrays.toString(result.stream().mapToInt(value -> value.val).toArray())); } } diff --git a/target/classes/ds/LevelPrint.class b/target/classes/ds/LevelPrint.class index 94d41eec66a09bfe70fa84d6d4da640e5af74966..1b76dfc7246813eda177658b02ea470bf4da4e80 100644 GIT binary patch delta 196 zcmcb|-zG5O1D}?LPfD?VQgMlXQetuHu(M`lu<*%bekafcey>K4ドル^Azy!O+LbRLU!dBHF|>mq!)Vda20{%xXL` YNP?SlnJpOwv@|?5k@arg$fC;(08@}Y>;M1& delta 154 zcmZn@xW_-?1E;EnPfD?VQgO+|f0`UhaJKYhf5tFLQ=hEFvPAvTlFS_az|z#xR2!&* z$^RJT#f_0 Date: Wed, 4 May 2022 23:47:49 +0800 Subject: [PATCH 013/115] =?UTF-8?q?109.=20=E6=9C=89=E5=BA=8F=E9=93=BE?= =?UTF-8?q?=E8=A1=A8=E8=BD=AC=E6=8D=A2=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ds/bst/leetcode109/Solution.java | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/main/java/ds/bst/leetcode109/Solution.java b/src/main/java/ds/bst/leetcode109/Solution.java index 525ef34..11041e6 100644 --- a/src/main/java/ds/bst/leetcode109/Solution.java +++ b/src/main/java/ds/bst/leetcode109/Solution.java @@ -1,14 +1,19 @@ package ds.bst.leetcode109; +import java.util.ArrayDeque; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + /** * 有序链表转换二叉搜索树 * LeetCode 109 https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ * - * @author yangyi 2021年05月02日14:50:14 + * @author yangyi 2022年04月22日11:49:00 */ public class Solution { - public class ListNode { + public static class ListNode { int val; ListNode next; @@ -69,4 +74,57 @@ private ListNode getLinkMiddle(ListNode start, ListNode end) { return slow; } + private List> levelOrder(TreeNode root) { + if (root == null) { + return new LinkedList(); + } + List> result = new LinkedList(); + Queue
queue = new ArrayDeque(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + List
level = new LinkedList(); + for (int i = 0; i < size; i++) { + TreeNode cur = queue.poll(); + if (cur != null) { + level.add(cur); + } + if (cur.left != null) { + queue.offer(cur.left); + } + if (cur.right != null) { + queue.offer(cur.right); + } + } + result.add(level); + } + return result; + } + + private ListNode createListNode() { + ListNode val_10 = new ListNode(-10); + ListNode val_3 = new ListNode(-3); + ListNode val_0 = new ListNode(0); + ListNode val_5 = new ListNode(5); + ListNode val_9 = new ListNode(9); + val_10.next = val_3; + val_3.next = val_0; + val_0.next = val_5; + val_5.next = val_9; + return val_10; + } + + public static void main(String[] args) { + Solution solution = new Solution(); + ListNode link = solution.createListNode(); + TreeNode tree = solution.sortedListToBST(link); + List> result = solution.levelOrder(tree); + for (List
treeNodes : result) { + for (TreeNode treeNode : treeNodes) { + System.out.print(treeNode.val + " "); + } + System.out.println(); + } + } + } From e475afb176abc7a7cc0727619f88d2c50bee9cf9 Mon Sep 17 00:00:00 2001 From: yangyi Date: Thu, 5 May 2022 00:08:54 +0800 Subject: [PATCH 014/115] =?UTF-8?q?111.=20=E4=BA=8C=E5=8F=89=E6=A0=91?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=B0=8F=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/bfs/leetcode111/Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ds/bfs/leetcode111/Solution.java b/src/main/java/ds/bfs/leetcode111/Solution.java index c4e96a9..7973cc2 100644 --- a/src/main/java/ds/bfs/leetcode111/Solution.java +++ b/src/main/java/ds/bfs/leetcode111/Solution.java @@ -48,7 +48,7 @@ public int minDepth(TreeNode root) { return 0; } Queue
treeNodeQueue = new LinkedList(); - treeNodeQueue.add(root); + treeNodeQueue.offer(root); int minDepth = 1; while (!treeNodeQueue.isEmpty()) { int size = treeNodeQueue.size(); @@ -58,10 +58,10 @@ public int minDepth(TreeNode root) { return minDepth; } if (cur.left != null) { - treeNodeQueue.add(cur.left); + treeNodeQueue.offer(cur.left); } if (cur.right != null) { - treeNodeQueue.add(cur.right); + treeNodeQueue.offer(cur.right); } } minDepth++; From 512d0e2f6995642257e3af730ce1694ec897db1e Mon Sep 17 00:00:00 2001 From: yangyi Date: Thu, 5 May 2022 22:55:30 +0800 Subject: [PATCH 015/115] =?UTF-8?q?11.=20=E7=9B=9B=E6=9C=80=E5=A4=9A?= =?UTF-8?q?=E6=B0=B4=E7=9A=84=E5=AE=B9=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/pointer/leetcode11/Solution.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/main/java/ds/pointer/leetcode11/Solution.java diff --git a/README.md b/README.md index 82a3f1e..3f88b08 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ - [剑指 Offer II 011.0 和 1 个数相同的子数组](/src/main/java/ds/hashmap/targetofferII011/Solution.java) - [剑指 Offer II 012.左右两边子数组的和相等](/src/main/java/ds/hashmap/targetofferII012/Solution.java) - [剑指 Offer II 015.字符串中的所有变位词](/src/main/java/ds/pointer/targetofferII015/Solution.java) + - [11.盛最多水的容器](/src/main/java/ds/pointer/leetcode11/Solution.java) 6. 滑动窗口 - [一个数组所有连续K个元素构成的子集的平均数](/src/main/java/ds/sliding/ArrayAverages.java) diff --git a/src/main/java/ds/pointer/leetcode11/Solution.java b/src/main/java/ds/pointer/leetcode11/Solution.java new file mode 100644 index 0000000..a609946 --- /dev/null +++ b/src/main/java/ds/pointer/leetcode11/Solution.java @@ -0,0 +1,30 @@ +package ds.pointer.leetcode11; + +/** + * 盛最多水的容器 + * LeetCode 11 https://leetcode-cn.com/problems/container-with-most-water/ + * + * @author yangyi 2022年05月05日22:47:18 + */ +public class Solution { + + public int maxArea(int[] height) { + int result = 0, start = 0, end = height.length - 1; + while (start < end) { + int area = Math.min(height[start], height[end]) * (end - start); + result = Math.max(result, area); + //矮的边向内移动,因为整体的盛水面积是由矮的边所决定的 + if (height[start] < height[end]) { + start++; + } else { + end--; + } + } + return result; + } + + public static void main(String[] args) { + System.out.println(new Solution().maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7})); + System.out.println(new Solution().maxArea(new int[]{1, 1})); + } +} From 1d141aab74592f60cf7833d4407bbf57274b4966 Mon Sep 17 00:00:00 2001 From: yangyi Date: Fri, 6 May 2022 00:11:47 +0800 Subject: [PATCH 016/115] =?UTF-8?q?42.=20=E6=8E=A5=E9=9B=A8=E6=B0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/pointer/leetcode42/Solution.java | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/main/java/ds/pointer/leetcode42/Solution.java diff --git a/README.md b/README.md index 3f88b08..d06f0a5 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,7 @@ - [剑指 Offer II 012.左右两边子数组的和相等](/src/main/java/ds/hashmap/targetofferII012/Solution.java) - [剑指 Offer II 015.字符串中的所有变位词](/src/main/java/ds/pointer/targetofferII015/Solution.java) - [11.盛最多水的容器](/src/main/java/ds/pointer/leetcode11/Solution.java) + - [42.接雨水](/src/main/java/ds/pointer/leetcode42/Solution.java) 6. 滑动窗口 - [一个数组所有连续K个元素构成的子集的平均数](/src/main/java/ds/sliding/ArrayAverages.java) diff --git a/src/main/java/ds/pointer/leetcode42/Solution.java b/src/main/java/ds/pointer/leetcode42/Solution.java new file mode 100644 index 0000000..1a1f419 --- /dev/null +++ b/src/main/java/ds/pointer/leetcode42/Solution.java @@ -0,0 +1,35 @@ +package ds.pointer.leetcode42; + +/** + * 接雨水 + * LeetCode 42 https://leetcode-cn.com/problems/trapping-rain-water/ + * + * @author yangyi 2022年05月05日23:31:02 + */ +public class Solution { + + /** + * 左右指针两边高度的最大值中的较小的那个 - 当前高度 = 当前接水量 + */ + public int trap(int[] height) { + int result = 0, start = 0, end = height.length - 1; + int startMax = 0, endMax = 0; + while (start < end) { + startMax = Math.max(startMax, height[start]); + endMax = Math.max(endMax, height[end]); + if (height[start] < height[end]) { + result += Math.min(startMax, endMax) - height[start]; + start++; + } else { + result += Math.min(startMax, endMax) - height[end]; + end--; + } + } + return result; + } + + public static void main(String[] args) { + System.out.println(new Solution().trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1})); + System.out.println(new Solution().trap(new int[]{4, 2, 0, 3, 2, 5})); + } +} From ebe64aef7bd8fa802c18f51e59251e0edf65d5f2 Mon Sep 17 00:00:00 2001 From: yangyi Date: Fri, 6 May 2022 00:31:50 +0800 Subject: [PATCH 017/115] =?UTF-8?q?80.=20=E5=88=A0=E9=99=A4=E6=9C=89?= =?UTF-8?q?=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E7=9A=84=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E9=A1=B9II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/pointer/leetcode80/Solution.java | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 src/main/java/ds/pointer/leetcode80/Solution.java diff --git a/README.md b/README.md index d06f0a5..1c4335e 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ - [167.两数之和II - 输入有序数组](/src/main/java/ds/pointer/leetcode167/Solution.java) - [905.按奇偶排序数组](/src/main/java/ds/pointer/leetcode905/Solution.java) - [26.删除排序数组中的重复项](/src/main/java/ds/pointer/leetcode26/Solution.java) + - [80.删除有序数组中的重复项II](/src/main/java/ds/pointer/leetcode80/Solution.java) - [83.删除排序链表中的重复元素](/src/main/java/ds/pointer/leetcode83/Solution.java) - [27.移除元素](/src/main/java/ds/pointer/leetcode27/Solution.java) - [283.移动零](/src/main/java/ds/pointer/leetcode283/Solution.java) diff --git a/src/main/java/ds/pointer/leetcode80/Solution.java b/src/main/java/ds/pointer/leetcode80/Solution.java new file mode 100644 index 0000000..c20c450 --- /dev/null +++ b/src/main/java/ds/pointer/leetcode80/Solution.java @@ -0,0 +1,33 @@ +package ds.pointer.leetcode80; + +/** + * 删除有序数组中的重复项 II + * LeetCode 80 https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/ + * + * @author yangyi 2022年05月06日00:23:37 + */ +public class Solution { + + /** + * 起始位置相当于一个最多出现几次的【游标卡尺】 + */ + public int removeDuplicates(int[] nums) { + if (nums.length <= 2) { + return nums.length; + } + int fast = 2, slow = 2; + while (fast < nums.length) { + if (nums[slow - 2] != nums[fast]) { + nums[slow] = nums[fast]; + slow++; + } + fast++; + } + return slow; + } + + public static void main(String[] args) { + System.out.println(new Solution().removeDuplicates(new int[]{1, 1, 1, 2, 2, 3})); + System.out.println(new Solution().removeDuplicates(new int[]{0, 0, 1, 1, 1, 1, 2, 3, 3})); + } +} From 0bf6a49fc5a65b0ec74206105f21b5b19dc6e012 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2021年3月10日 16:52:24 +0800 Subject: [PATCH 018/115] =?UTF-8?q?337.=E6=89=93=E5=AE=B6=E5=8A=AB?= =?UTF-8?q?=E8=88=8DIII?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5256円266円345円212円253円350円210円215円II.patch" | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 "213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" diff --git "a/213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" "b/213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" new file mode 100644 index 0000000..d8ea552 --- /dev/null +++ "b/213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" @@ -0,0 +1,94 @@ +Index: README.md +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +diff --git a/README.md b/README.md +--- a/README.md (revision d3d4e43f2411b4c70b7d14bb1fe610f8393da4bc) ++++ b/README.md (revision 60cf2229b154b490134bd9777d946aa758ae2514) +@@ -239,17 +239,20 @@ + - [122.买卖股票的最佳时机II](/src/main/java/ds/greedy/leetcode122/Solution.java) + + 17. 动态规划(DP) +- - [斐波拉契数列的4种解法](/src/main/java/ds/dp/Fipolach.java) +- - [509.斐波那契数](/src/main/java/ds/dp/leetcode509/Solution.java) +- - [70.爬楼梯](/src/main/java/ds/dp/leetcode70/Solution.java) +- - [746.使用最小花费爬楼梯](/src/main/java/ds/dp/leetcode746/Solution.java) +- - [60.不同路径](/src/main/java/ds/dp/leetcode60/Solution.java) +- - [63.不同路径II](/src/main/java/ds/dp/leetcode63/Solution.java) ++ - 斐波拉切 ++ - [斐波拉契数列的4种解法](/src/main/java/ds/dp/Fipolach.java) ++ - [509.斐波那契数](/src/main/java/ds/dp/leetcode509/Solution.java) ++ - [70.爬楼梯](/src/main/java/ds/dp/leetcode70/Solution.java) ++ - [746.使用最小花费爬楼梯](/src/main/java/ds/dp/leetcode746/Solution.java) ++ - 路径 ++ - [60.不同路径](/src/main/java/ds/dp/leetcode60/Solution.java) ++ - [63.不同路径II](/src/main/java/ds/dp/leetcode63/Solution.java) + - [343.整数拆分](/src/main/java/ds/dp/leetcode343/Solution.java) + - [三角形最小路径和](/src/main/java/ds/dp/MinimumTotal.java) + - [乘积最大子序列](/src/main/java/ds/dp/MaxProduct.java) + - 打家劫舍 + - [198.打家劫舍](/src/main/java/ds/dp/leetcode198/Solution.java) ++ - [213.打家劫舍II](/src/main/java/ds/dp/leetcode213/Solution.java) + + + ## PAT +Index: src/main/java/ds/dp/leetcode213/Solution.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +diff --git a/src/main/java/ds/dp/leetcode213/Solution.java b/src/main/java/ds/dp/leetcode213/Solution.java +new file mode 100644 +--- /dev/null (revision 60cf2229b154b490134bd9777d946aa758ae2514) ++++ b/src/main/java/ds/dp/leetcode213/Solution.java (revision 60cf2229b154b490134bd9777d946aa758ae2514) +@@ -0,0 +1,49 @@ ++package ds.dp.leetcode213; ++ ++/** ++ * 打家劫舍II ++ * LeetCode 213 https://leetcode-cn.com/problems/house-robber-ii/ ++ * ++ * @author yangyi 2021年03月10日12:21:09 ++ */ ++public class Solution { ++ ++ public int rob(int[] nums) { ++ if (nums.length == 0) { ++ return 0; ++ } ++ if (nums.length == 1) { ++ return nums[0]; ++ } ++ int result1 = robRange(nums, 0, nums.length - 2); ++ int result2 = robRange(nums, 1, nums.length - 1); ++ return Math.max(result1, result2); ++ } ++ ++ public int robRange(int[] nums, int start, int end) { ++ if (nums.length == 0) { ++ return 0; ++ } ++ if (nums.length == 1) { ++ return nums[0]; ++ } ++ if (start == end) { ++ return nums[start]; ++ } ++ int[] dp = new int[nums.length]; ++ dp[start] = nums[start]; ++ dp[start + 1] = Math.max(nums[start], nums[start + 1]); ++ for (int i = start + 2; i <= end; i++) { ++ dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); ++ } ++ return dp[end]; ++ } ++ ++ public static void main(String[] args) { ++ System.out.println(new Solution().rob(new int[]{2, 3, 2})); ++ System.out.println(new Solution().rob(new int[]{1, 2, 3, 1})); ++ System.out.println(new Solution().rob(new int[]{0})); ++ System.out.println(new Solution().rob(new int[]{0, 0})); ++ System.out.println(new Solution().rob(new int[]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5})); ++ } ++} From 173eb8dc113877a4deff3951d3beec05ee92c525 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2021年4月30日 18:02:20 +0800 Subject: [PATCH 019/115] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=2032=20-=20I?= =?UTF-8?q?.=20=E4=BB=8E=E4=B8=8A=E5=88=B0=E4=B8=8B=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5256円266円345円212円253円350円210円215円II.patch" | 94 ------------------- .../java/ds/bfs/targetoffer32/Solution.java | 67 +++++++++++++ 2 files changed, 67 insertions(+), 94 deletions(-) delete mode 100644 "213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" create mode 100644 src/main/java/ds/bfs/targetoffer32/Solution.java diff --git "a/213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" "b/213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" deleted file mode 100644 index d8ea552..0000000 --- "a/213_346円211円223円345円256円266円345円212円253円350円210円215円II.patch" +++ /dev/null @@ -1,94 +0,0 @@ -Index: README.md -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== -diff --git a/README.md b/README.md ---- a/README.md (revision d3d4e43f2411b4c70b7d14bb1fe610f8393da4bc) -+++ b/README.md (revision 60cf2229b154b490134bd9777d946aa758ae2514) -@@ -239,17 +239,20 @@ - - [122.买卖股票的最佳时机II](/src/main/java/ds/greedy/leetcode122/Solution.java) - - 17. 动态规划(DP) -- - [斐波拉契数列的4种解法](/src/main/java/ds/dp/Fipolach.java) -- - [509.斐波那契数](/src/main/java/ds/dp/leetcode509/Solution.java) -- - [70.爬楼梯](/src/main/java/ds/dp/leetcode70/Solution.java) -- - [746.使用最小花费爬楼梯](/src/main/java/ds/dp/leetcode746/Solution.java) -- - [60.不同路径](/src/main/java/ds/dp/leetcode60/Solution.java) -- - [63.不同路径II](/src/main/java/ds/dp/leetcode63/Solution.java) -+ - 斐波拉切 -+ - [斐波拉契数列的4种解法](/src/main/java/ds/dp/Fipolach.java) -+ - [509.斐波那契数](/src/main/java/ds/dp/leetcode509/Solution.java) -+ - [70.爬楼梯](/src/main/java/ds/dp/leetcode70/Solution.java) -+ - [746.使用最小花费爬楼梯](/src/main/java/ds/dp/leetcode746/Solution.java) -+ - 路径 -+ - [60.不同路径](/src/main/java/ds/dp/leetcode60/Solution.java) -+ - [63.不同路径II](/src/main/java/ds/dp/leetcode63/Solution.java) - - [343.整数拆分](/src/main/java/ds/dp/leetcode343/Solution.java) - - [三角形最小路径和](/src/main/java/ds/dp/MinimumTotal.java) - - [乘积最大子序列](/src/main/java/ds/dp/MaxProduct.java) - - 打家劫舍 - - [198.打家劫舍](/src/main/java/ds/dp/leetcode198/Solution.java) -+ - [213.打家劫舍II](/src/main/java/ds/dp/leetcode213/Solution.java) - - - ## PAT -Index: src/main/java/ds/dp/leetcode213/Solution.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== -diff --git a/src/main/java/ds/dp/leetcode213/Solution.java b/src/main/java/ds/dp/leetcode213/Solution.java -new file mode 100644 ---- /dev/null (revision 60cf2229b154b490134bd9777d946aa758ae2514) -+++ b/src/main/java/ds/dp/leetcode213/Solution.java (revision 60cf2229b154b490134bd9777d946aa758ae2514) -@@ -0,0 +1,49 @@ -+package ds.dp.leetcode213; -+ -+/** -+ * 打家劫舍II -+ * LeetCode 213 https://leetcode-cn.com/problems/house-robber-ii/ -+ * -+ * @author yangyi 2021年03月10日12:21:09 -+ */ -+public class Solution { -+ -+ public int rob(int[] nums) { -+ if (nums.length == 0) { -+ return 0; -+ } -+ if (nums.length == 1) { -+ return nums[0]; -+ } -+ int result1 = robRange(nums, 0, nums.length - 2); -+ int result2 = robRange(nums, 1, nums.length - 1); -+ return Math.max(result1, result2); -+ } -+ -+ public int robRange(int[] nums, int start, int end) { -+ if (nums.length == 0) { -+ return 0; -+ } -+ if (nums.length == 1) { -+ return nums[0]; -+ } -+ if (start == end) { -+ return nums[start]; -+ } -+ int[] dp = new int[nums.length]; -+ dp[start] = nums[start]; -+ dp[start + 1] = Math.max(nums[start], nums[start + 1]); -+ for (int i = start + 2; i <= end; i++) { -+ dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); -+ } -+ return dp[end]; -+ } -+ -+ public static void main(String[] args) { -+ System.out.println(new Solution().rob(new int[]{2, 3, 2})); -+ System.out.println(new Solution().rob(new int[]{1, 2, 3, 1})); -+ System.out.println(new Solution().rob(new int[]{0})); -+ System.out.println(new Solution().rob(new int[]{0, 0})); -+ System.out.println(new Solution().rob(new int[]{1, 2, 3, 4, 5, 1, 2, 3, 4, 5})); -+ } -+} diff --git a/src/main/java/ds/bfs/targetoffer32/Solution.java b/src/main/java/ds/bfs/targetoffer32/Solution.java new file mode 100644 index 0000000..b18c2d4 --- /dev/null +++ b/src/main/java/ds/bfs/targetoffer32/Solution.java @@ -0,0 +1,67 @@ +package ds.bfs.targetoffer32; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Queue; + +/** + * 从上到下打印二叉树 + * 剑指 Offer 32 - I. https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/ + * + * @author yangyi 2021年04月30日17:43:21 + */ +public class Solution { + + public class TreeNode { + int val; + TreeNode left; + TreeNode right; + + TreeNode(int x) { + val = x; + } + } + + public int[] levelOrder(TreeNode root) { + if (root == null) { + return new int[]{}; + } + Queue
queue = new LinkedList(); + queue.offer(root); + List
levelList = new LinkedList(); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + TreeNode cur = queue.poll(); + if (cur != null) { + levelList.add(cur); + } + if (cur.left != null) { + queue.add(cur.left); + } + if (cur.right != null) { + queue.add(cur.right); + } + } + } + return levelList.stream().mapToInt(value -> value.val).toArray(); + } + + private TreeNode createTree() { + TreeNode node_3 = new TreeNode(3); + TreeNode node_9 = new TreeNode(9); + TreeNode node_20 = new TreeNode(20); + TreeNode node_15 = new TreeNode(15); + TreeNode node_7 = new TreeNode(7); + node_3.left = node_9; + node_3.right = node_20; + node_20.left = node_15; + node_20.right = node_7; + return node_3; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(new Solution().levelOrder(new Solution().createTree()))); + } +} From 55216f317baa1605fce6877f1fce0f482d7dea5b Mon Sep 17 00:00:00 2001 From: yangyi Date: 2021年7月12日 16:55:05 +0800 Subject: [PATCH 020/115] =?UTF-8?q?347.=E5=89=8D=20K=20=E4=B8=AA=E9=AB=98?= =?UTF-8?q?=E9=A2=91=E5=85=83=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/TopKFrequent.java | 68 ------------------ .../java/ds/heap/leetcode347/Solution.java | 44 ++++++++++++ target/classes/ds/MaxSlidingWindow.class | Bin 1605 -> 0 bytes 3 files changed, 44 insertions(+), 68 deletions(-) delete mode 100644 src/main/java/ds/TopKFrequent.java create mode 100644 src/main/java/ds/heap/leetcode347/Solution.java delete mode 100644 target/classes/ds/MaxSlidingWindow.class diff --git a/src/main/java/ds/TopKFrequent.java b/src/main/java/ds/TopKFrequent.java deleted file mode 100644 index 2f95916..0000000 --- a/src/main/java/ds/TopKFrequent.java +++ /dev/null @@ -1,68 +0,0 @@ -package ds; - -import java.util.*; - -/** - * 前K个高频元素 - *

- * 给定一个非空的整数数组,返回其中出现频率前 k 高的元素。 - *

- * 示例 1: - *

- * 输入: nums = [1,1,1,2,2,3], k = 2 - * 输出: [1,2] - * 示例 2: - *

- * 输入: nums = [1], k = 1 - * 输出: [1] - * 说明: - *

- * 你可以假设给定的 k 总是合理的,且 1 ≤ k ≤ 数组中不相同的元素的个数。 - * 你的算法的时间复杂度必须优于 O(n log n) , n 是数组的大小。 - * - * @author yangyi 2019年02月17日23:40:36 - */ -public class TopKFrequent { - - public List topKFrequent(int[] nums, int k) { - Map map = new HashMap(); - for (int i = 0; i < nums.length; i++) { - if (!map.containsKey(nums[i])) { - map.put(nums[i], 1); - } else { - map.put(nums[i], map.get(nums[i]) + 1); - } - } - //按照get出来的元素对应的次数排序 - PriorityQueue priorityQueue = new PriorityQueue(k, - new Comparator() { - @Override - public int compare(Integer o1, Integer o2) { - return map.get(o1).compareTo(map.get(o2)); - } - }); - //将元素加入小顶堆 - for (Map.Entry integerIntegerEntry : map.entrySet()) { - if (priorityQueue.size() < k) { - priorityQueue.offer(integerIntegerEntry.getKey()); - } else if (integerIntegerEntry.getValue()> map.get(priorityQueue.peek())) { - //比较元素出现的次数,如果次数比小顶堆的堆顶大,则取而代之,自己做堆顶 - priorityQueue.poll(); - priorityQueue.offer(integerIntegerEntry.getKey()); - } - } - List list = new ArrayList(k); - list.addAll(priorityQueue); - return list; - } - - public static void main(String[] args) { - int[] a = {1, 1, 1, 2, 2, 3}; - int[] b = {1}; - int[] c = {4, 1, -1, 2, -1, 2, 3}; - TopKFrequent topKFrequent = new TopKFrequent(); - System.out.println(Arrays.toString(topKFrequent.topKFrequent(a, 2).toArray())); - System.out.println(Arrays.toString(topKFrequent.topKFrequent(b, 1).toArray())); - System.out.println(Arrays.toString(topKFrequent.topKFrequent(c, 2).toArray())); - } -} diff --git a/src/main/java/ds/heap/leetcode347/Solution.java b/src/main/java/ds/heap/leetcode347/Solution.java new file mode 100644 index 0000000..7dd7b6b --- /dev/null +++ b/src/main/java/ds/heap/leetcode347/Solution.java @@ -0,0 +1,44 @@ +package ds.heap.leetcode347; + +import java.util.*; + +/** + * 前 K 个高频元素 + * LeetCode 347 https://leetcode-cn.com/problems/top-k-frequent-elements/ + * + * @author yangyi 2007月12日16:00:48git + */ +public class Solution { + + public int[] topKFrequent(int[] nums, int k) { + Map map = new LinkedHashMap(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + map.put(nums[i], map.getOrDefault(nums[i], -1) + 1); + } else { + map.put(nums[i], 1); + } + } + PriorityQueue priorityQueue = new PriorityQueue( + k, Comparator.comparing(o -> map.getOrDefault(o, -1))); + for (Map.Entry kv : map.entrySet()) { + if (priorityQueue.size() < k) { + priorityQueue.offer(kv.getKey()); + } else if (kv.getValue()> map.getOrDefault(priorityQueue.peek(), -1)) { + priorityQueue.poll(); + priorityQueue.offer(kv.getKey()); + } + } + return priorityQueue.stream().mapToInt(value -> value).toArray(); + } + + public static void main(String[] args) { + int[] a = {1, 1, 1, 2, 2, 3}; + int[] b = {1}; + int[] c = {4, 1, -1, 2, -1, 2, 3}; + Solution solution = new Solution(); + System.out.println(Arrays.toString(solution.topKFrequent(a, 2))); + System.out.println(Arrays.toString(solution.topKFrequent(b, 1))); + System.out.println(Arrays.toString(solution.topKFrequent(c, 2))); + } +} diff --git a/target/classes/ds/MaxSlidingWindow.class b/target/classes/ds/MaxSlidingWindow.class deleted file mode 100644 index 3582fa6dcb81d0691f3dc5e21a4de257e9f7046f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1605 zcmZux&r@4f6#i~<$xxtegu=t3v|5ta5j)ows_-avc=v~%hwdtpc#tpp$pavzdyeal zbX>U685eCAtejRyTWcA{l?#{t6Yg9)&Q$#F3oVFUTLQfk8fq;8K2MDor`#q#Q|EpYnq{aC|LvMMX7OvSTT zY>K#B#T`D)!= zwkI9koq4$Wq^8%vF?0!Z>{ESNL->3f=zys(Y2Y19aUQIgqbEqCiYg=U&-R^h# zUo)3!T|1Sy5t4Px;v}C;JD(u8Nl%C0PM!jI2|?=C53Q|R?c zyHCs>(%Ffx7}?9LZ_u+1DiVB(W`(#IIFtIDKoKVFZ?K9#2^XfD6$@`i3qhGH$WX4( z6v!1qv8TC3ドルg-%88e*ZCmMW%l*(W%{F}6+*BuH#*X7If1k~`_9U^--m%%;0I)+NnQ zGOfNxo2ee+=SHY70y`bY2rlxCUcx-Ti@5T<-y1au_p7}vb*+jl ubCxXWeut3aa-3klBjj+u-{T1EVggDb$(9vTzKjd5)n6QgAi;W5-~1OKV@tdM From 883849fe97fb21052684b946a00f2a6c4790c469 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月21日 15:00:46 +0800 Subject: [PATCH 021/115] =?UTF-8?q?450.=20=E5=88=A0=E9=99=A4=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E4=B8=AD=E7=9A=84=E8=8A=82?= =?UTF-8?q?=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/bst/leetcode450/Solution.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/ds/bst/leetcode450/Solution.java b/src/main/java/ds/bst/leetcode450/Solution.java index c8f56ea..e195596 100644 --- a/src/main/java/ds/bst/leetcode450/Solution.java +++ b/src/main/java/ds/bst/leetcode450/Solution.java @@ -62,7 +62,11 @@ public TreeNode deleteNode(TreeNode root, int key) { if (root == null) { return null; } - if (root.val == key) { + if (root.val> key) { + root.left = deleteNode(root.left, key); + } else if (root.val < key) { + root.right = deleteNode(root.right, key); + } else if (root.val == key) { if (root.left == null) { return root.right; } @@ -74,10 +78,6 @@ public TreeNode deleteNode(TreeNode root, int key) { root.val = min.val; //删除右子树中找出来的最小值 root.right = deleteNode(root.right, min.val); - } else if (key < root.val) { - root.left = deleteNode(root.left, key); - } else { - root.right = deleteNode(root.right, key); } return root; } From b513de0189b2f0147bc93fd83b7e66947eacfdac Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月22日 16:59:44 +0800 Subject: [PATCH 022/115] =?UTF-8?q?513.=20=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/tree/leetcode513/Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ds/tree/leetcode513/Solution.java b/src/main/java/ds/tree/leetcode513/Solution.java index d3d9ea3..3bfa5c0 100644 --- a/src/main/java/ds/tree/leetcode513/Solution.java +++ b/src/main/java/ds/tree/leetcode513/Solution.java @@ -7,7 +7,7 @@ * 找树左下角的值 * LeetCode 513 https://leetcode-cn.com/problems/find-bottom-left-tree-value/ * - * @author yangyi 2021年02月04日17:48:32 + * @author yangyi 2022年04月22日16:59:26 */ public class Solution { @@ -34,14 +34,14 @@ public int findBottomLeftValue(TreeNode root) { if (root == null) { return 0; } + int result = 0; Queue

queue = new LinkedList(); queue.offer(root); - int result = 0; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode cur = queue.poll(); - if (i == 0) { + if (cur != null && i == 0) { result = cur.val; } if (cur.left != null) { From c6b5e3a2a7dfc892bcaa1cfa3358e941bb0067ec Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月22日 18:30:27 +0800 Subject: [PATCH 023/115] =?UTF-8?q?=E5=89=91=E6=8C=87offer=2054.=20?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E7=AC=AC?= =?UTF-8?q?k=E5=A4=A7=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../java/ds/{bst => tree}/target54/Solution.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) rename src/main/java/ds/{bst => tree}/target54/Solution.java (84%) diff --git a/README.md b/README.md index d06f0a5..b8fa86b 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ - [剑指offer 36. 二叉搜索树与双向链表](/src/main/java/ds/bst/targetoffer36/Solution.java) - [108.将有序数组转换为二叉搜索树](/src/main/java/ds/bst/leetcode108/Solution.java) - [109.有序链表转换二叉搜索树](/src/main/java/ds/bst/leetcode109/Solution.java) + - [剑指offer 54.二叉搜索树的第k大节点](/src/main/java/ds/tree/target54/Solution.java) 4. 二分查找 - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) @@ -211,7 +212,6 @@ - [剑指offer 32 - II.从上到下打印二叉树II](/src/main/java/ds/bfs/targetoffer322/Solution.java) - [剑指offer 32 - III.从上到下打印二叉树III](/src/main/java/ds/bfs/targetoffer323/Solution.java) - [103.二叉树的锯齿形层次遍历](/src/main/java/ds/bfs/leetcode103/Solution.java) - - [剑指offer 54.二叉搜索树的第k大节点](/src/main/java/ds/bst/target54/Solution.java) - [117.填充每个节点的下一个右侧节点指针II](/src/main/java/ds/bfs/leetcode117/Solution.java) 11. 数组 diff --git a/src/main/java/ds/bst/target54/Solution.java b/src/main/java/ds/tree/target54/Solution.java similarity index 84% rename from src/main/java/ds/bst/target54/Solution.java rename to src/main/java/ds/tree/target54/Solution.java index 8db9260..e5ffe4e 100644 --- a/src/main/java/ds/bst/target54/Solution.java +++ b/src/main/java/ds/tree/target54/Solution.java @@ -1,10 +1,10 @@ -package ds.bst.target54; +package ds.tree.target54; /** * 二叉搜索树的第k大节点 * 剑指 Offer 54 https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/ * - * @author yangyi 2021年05月02日00:06:06 + * @author yangyi 2022年04月22日17:44:46 */ public class Solution { @@ -22,21 +22,21 @@ public class TreeNode { public int kthLargest(TreeNode root, int k) { this.k = k; - inOrderReserve(root); + kthLargestHelper(root); return result; } - private void inOrderReserve(TreeNode cur) { - if (cur == null) { + public void kthLargestHelper(TreeNode root) { + if (root == null) { return; } - inOrderReserve(cur.right); + kthLargestHelper(root.right); k--; if (k == 0) { - result = cur.val; + result = root.val; return; } - inOrderReserve(cur.left); + kthLargestHelper(root.left); } private TreeNode createTree() { From 03875f246be697fef1ab9f0e7f46d81a2e8a0dac Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月22日 18:53:43 +0800 Subject: [PATCH 024/115] =?UTF-8?q?117.=20=E5=A1=AB=E5=85=85=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ds/bfs/leetcode117/Solution.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main/java/ds/bfs/leetcode117/Solution.java b/src/main/java/ds/bfs/leetcode117/Solution.java index 9b02807..162e811 100644 --- a/src/main/java/ds/bfs/leetcode117/Solution.java +++ b/src/main/java/ds/bfs/leetcode117/Solution.java @@ -1,6 +1,9 @@ package ds.bfs.leetcode117; +import java.util.ArrayDeque; +import java.util.Arrays; import java.util.LinkedList; +import java.util.List; import java.util.Queue; /** @@ -57,4 +60,51 @@ public Node connect(Node root) { } return root; } + + private Node createTree() { + Node node_1 = new Node(1); + Node node_2 = new Node(2); + Node node_3 = new Node(3); + Node node_4 = new Node(4); + Node node_5 = new Node(5); + Node node_7 = new Node(7); + node_1.left = node_2; + node_1.right = node_3; + node_2.left = node_4; + node_2.right = node_5; + node_3.right = node_7; + return node_1; + } + + private List levelOrder(Node root) { + if (root == null) { + return new LinkedList(); + } + List result = new LinkedList(); + Queue queue = new ArrayDeque(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Node cur = queue.poll(); + if (cur != null) { + result.add(cur.val); + } + if (cur.left != null) { + queue.offer(cur.left); + } + if (cur.right != null) { + queue.offer(cur.right); + } + } + } + return result; + } + + public static void main(String[] args) { + Solution solution = new Solution(); + Node root = solution.createTree(); + Node result = solution.connect(root); + System.out.println(Arrays.toString(solution.levelOrder(result).stream().mapToInt(value -> value).toArray())); + } } From 441a591a69be2187549675be44ab8c75998c032f Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月24日 15:17:54 +0800 Subject: [PATCH 025/115] =?UTF-8?q?141.=20=E7=8E=AF=E5=BD=A2=E9=93=BE?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/pointer/leetcode141/Solution.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/ds/pointer/leetcode141/Solution.java b/src/main/java/ds/pointer/leetcode141/Solution.java index 08e4edc..4cf529d 100644 --- a/src/main/java/ds/pointer/leetcode141/Solution.java +++ b/src/main/java/ds/pointer/leetcode141/Solution.java @@ -7,7 +7,7 @@ * 环形链表 * LeetCode 141 https://leetcode-cn.com/problems/linked-list-cycle/ * - * @author yangyi 2020年12月10日22:35:31 + * @author yangyi 2022年04月24日15:07:52 */ public class Solution { @@ -85,14 +85,16 @@ private boolean hasCycleSet(ListNode head) { public boolean hasCycle(ListNode head) { ListNode fast = head; ListNode slow = head; - while (fast != null && fast.next != null) { + while (true) { + if (fast == null || fast.next == null) { + return false; + } fast = fast.next.next; slow = slow.next; - if (fast == slow) { + if (slow == fast) { return true; } } - return false; } public static void main(String[] args) { From 56d3d0d0080d3404e82b6f16d7fedac9f9d34370 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月24日 17:33:53 +0800 Subject: [PATCH 026/115] =?UTF-8?q?876.=20=E9=93=BE=E8=A1=A8=E7=9A=84?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/pointer/leetcode876/Solution.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/ds/pointer/leetcode876/Solution.java b/src/main/java/ds/pointer/leetcode876/Solution.java index 9f1183a..b7814fe 100644 --- a/src/main/java/ds/pointer/leetcode876/Solution.java +++ b/src/main/java/ds/pointer/leetcode876/Solution.java @@ -4,7 +4,7 @@ * 链表的中间节点 * LeetCode 876 https://leetcode-cn.com/problems/middle-of-the-linked-list/ * - * @author yangyi 2020年12月11日00:06:32 + * @author yangyi 2022年04月24日17:33:20 */ public class Solution { @@ -54,7 +54,10 @@ private ListNode createLink2() { public ListNode middleNode(ListNode head) { ListNode fast = head; ListNode slow = head; - while (fast != null && fast.next != null) { + while (true) { + if (fast == null || fast.next == null) { + break; + } fast = fast.next.next; slow = slow.next; } From 80b08b2bf2838271e535bd23929a042145d8bef2 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月24日 17:51:22 +0800 Subject: [PATCH 027/115] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=2022.=20?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E4=B8=AD=E5=80=92=E6=95=B0=E7=AC=ACk?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/pointer/targetoffer22/Solution.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/ds/pointer/targetoffer22/Solution.java b/src/main/java/ds/pointer/targetoffer22/Solution.java index 5c933df..221e4c9 100644 --- a/src/main/java/ds/pointer/targetoffer22/Solution.java +++ b/src/main/java/ds/pointer/targetoffer22/Solution.java @@ -4,7 +4,7 @@ * 链表中倒数第k个节点 * LeetCode 剑指offer 22 https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/ * - * @author yangyi 2020年12月11日00:26:01 + * @author yangyi 2022年04月24日17:50:02 */ public class Solution { @@ -36,10 +36,14 @@ private ListNode createLink() { public ListNode getKthFromEnd(ListNode head, int k) { ListNode fast = head; ListNode slow = head; - while (k--> 0) { + while (k != 0) { fast = fast.next; + k--; } - while (fast != null) { + while (true) { + if (fast == null) { + break; + } fast = fast.next; slow = slow.next; } From 7836858bb2186f1eba447bcd608b43a71af50a70 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月24日 18:55:32 +0800 Subject: [PATCH 028/115] =?UTF-8?q?167.=20=E4=B8=A4=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C=20II=20-=20=E8=BE=93=E5=85=A5=E6=9C=89=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/pointer/leetcode167/Solution.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/ds/pointer/leetcode167/Solution.java b/src/main/java/ds/pointer/leetcode167/Solution.java index 5b5f459..a648952 100644 --- a/src/main/java/ds/pointer/leetcode167/Solution.java +++ b/src/main/java/ds/pointer/leetcode167/Solution.java @@ -6,7 +6,7 @@ * 两数之和 II - 输入有序数组 * LeetCode 167 https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/ * - * @author yangyi 2020年12月14日11:52:36 + * @author yangyi 2022年04月24日18:47:00 */ public class Solution { @@ -15,12 +15,12 @@ public int[] twoSum(int[] numbers, int target) { int end = numbers.length - 1; while (start < end) { int sum = numbers[start] + numbers[end]; - if (sum == target) { - return new int[]{start + 1, end + 1}; - } else if (sum < target) { + if (sum < target) { start++; } else if (sum> target) { end--; + } else if (sum == target) { + return new int[]{start + 1, end + 1}; } } return new int[]{-1, -1}; @@ -28,7 +28,7 @@ public int[] twoSum(int[] numbers, int target) { public static void main(String[] args) { int[] a = {2, 7, 11, 15}; - Solution twoSumII = new Solution(); - System.out.println(Arrays.toString(a) + "数组中和为9对应的数的索引为:" + Arrays.toString(twoSumII.twoSum(a, 9))); + Solution twoSum2 = new Solution(); + System.out.println(Arrays.toString(a) + "数组中和为9对应的数的索引为:" + Arrays.toString(twoSum2.twoSum(a, 9))); } } From 0370aa6a812ce3faa7890f94c7454dbf60341ec7 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年4月24日 19:20:27 +0800 Subject: [PATCH 029/115] =?UTF-8?q?905.=20=E6=8C=89=E5=A5=87=E5=81=B6?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ds/pointer/leetcode905/Solution.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/main/java/ds/pointer/leetcode905/Solution.java b/src/main/java/ds/pointer/leetcode905/Solution.java index fd44d70..d902d20 100644 --- a/src/main/java/ds/pointer/leetcode905/Solution.java +++ b/src/main/java/ds/pointer/leetcode905/Solution.java @@ -6,33 +6,30 @@ * 按奇偶排序数组 * LeetCode 905 https://leetcode-cn.com/problems/sort-array-by-parity/ * - * @author yangyi 2020年12月15日14:12:15 + * @author yangyi 2022年04月24日19:17:01 */ public class Solution { - public int[] sortArrayByParity(int[] A) { - if (A == null) { - return new int[]{}; - } - if (A.length == 0) { - return A; + public int[] sortArrayByParity(int[] nums) { + if (nums == null || nums.length == 0) { + return new int[]{0}; } int start = 0; - int end = A.length - 1; + int end = nums.length - 1; while (start < end) { - if (A[start] % 2 == 0) { + if (nums[start] % 2 == 0) { start++; continue; } - if (A[end] % 2 != 0) { + if (nums[end] % 2 != 0) { end--; continue; } - int temp = A[start]; - A[start] = A[end]; - A[end] = temp; + int temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; } - return A; + return nums; } public static void main(String[] args) { From b944af3554edf5b9ba936e63041c49b335837e82 Mon Sep 17 00:00:00 2001 From: yangyi Date: Sat, 7 May 2022 00:16:13 +0800 Subject: [PATCH 030/115] =?UTF-8?q?513.=20=E6=89=BE=E6=A0=91=E5=B7=A6?= =?UTF-8?q?=E4=B8=8B=E8=A7=92=E7=9A=84=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/tree/leetcode513/Solution.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ds/tree/leetcode513/Solution.java b/src/main/java/ds/tree/leetcode513/Solution.java index d3d9ea3..3bfa5c0 100644 --- a/src/main/java/ds/tree/leetcode513/Solution.java +++ b/src/main/java/ds/tree/leetcode513/Solution.java @@ -7,7 +7,7 @@ * 找树左下角的值 * LeetCode 513 https://leetcode-cn.com/problems/find-bottom-left-tree-value/ * - * @author yangyi 2021年02月04日17:48:32 + * @author yangyi 2022年04月22日16:59:26 */ public class Solution { @@ -34,14 +34,14 @@ public int findBottomLeftValue(TreeNode root) { if (root == null) { return 0; } + int result = 0; Queue
queue = new LinkedList(); queue.offer(root); - int result = 0; while (!queue.isEmpty()) { int size = queue.size(); for (int i = 0; i < size; i++) { TreeNode cur = queue.poll(); - if (i == 0) { + if (cur != null && i == 0) { result = cur.val; } if (cur.left != null) { From 953ae7ebfa6bc83465f51892bc213bad3c07aa1b Mon Sep 17 00:00:00 2001 From: yangyi Date: Sun, 8 May 2022 23:49:35 +0800 Subject: [PATCH 031/115] =?UTF-8?q?=E5=89=91=E6=8C=87offer=2054.=20?= =?UTF-8?q?=E4=BA=8C=E5=8F=89=E6=90=9C=E7=B4=A2=E6=A0=91=E7=9A=84=E7=AC=AC?= =?UTF-8?q?k=E5=A4=A7=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../java/ds/{bst => tree}/target54/Solution.java | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) rename src/main/java/ds/{bst => tree}/target54/Solution.java (84%) diff --git a/README.md b/README.md index 1c4335e..067e1e1 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ - [剑指offer 36. 二叉搜索树与双向链表](/src/main/java/ds/bst/targetoffer36/Solution.java) - [108.将有序数组转换为二叉搜索树](/src/main/java/ds/bst/leetcode108/Solution.java) - [109.有序链表转换二叉搜索树](/src/main/java/ds/bst/leetcode109/Solution.java) + - [剑指offer 54.二叉搜索树的第k大节点](/src/main/java/ds/tree/target54/Solution.java) 4. 二分查找 - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) @@ -212,7 +213,6 @@ - [剑指offer 32 - II.从上到下打印二叉树II](/src/main/java/ds/bfs/targetoffer322/Solution.java) - [剑指offer 32 - III.从上到下打印二叉树III](/src/main/java/ds/bfs/targetoffer323/Solution.java) - [103.二叉树的锯齿形层次遍历](/src/main/java/ds/bfs/leetcode103/Solution.java) - - [剑指offer 54.二叉搜索树的第k大节点](/src/main/java/ds/bst/target54/Solution.java) - [117.填充每个节点的下一个右侧节点指针II](/src/main/java/ds/bfs/leetcode117/Solution.java) 11. 数组 diff --git a/src/main/java/ds/bst/target54/Solution.java b/src/main/java/ds/tree/target54/Solution.java similarity index 84% rename from src/main/java/ds/bst/target54/Solution.java rename to src/main/java/ds/tree/target54/Solution.java index 8db9260..e5ffe4e 100644 --- a/src/main/java/ds/bst/target54/Solution.java +++ b/src/main/java/ds/tree/target54/Solution.java @@ -1,10 +1,10 @@ -package ds.bst.target54; +package ds.tree.target54; /** * 二叉搜索树的第k大节点 * 剑指 Offer 54 https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/ * - * @author yangyi 2021年05月02日00:06:06 + * @author yangyi 2022年04月22日17:44:46 */ public class Solution { @@ -22,21 +22,21 @@ public class TreeNode { public int kthLargest(TreeNode root, int k) { this.k = k; - inOrderReserve(root); + kthLargestHelper(root); return result; } - private void inOrderReserve(TreeNode cur) { - if (cur == null) { + public void kthLargestHelper(TreeNode root) { + if (root == null) { return; } - inOrderReserve(cur.right); + kthLargestHelper(root.right); k--; if (k == 0) { - result = cur.val; + result = root.val; return; } - inOrderReserve(cur.left); + kthLargestHelper(root.left); } private TreeNode createTree() { From a048ddda637ef206b4f555fc2c80d872f9bfdaac Mon Sep 17 00:00:00 2001 From: yangyi Date: Mon, 9 May 2022 00:02:59 +0800 Subject: [PATCH 032/115] =?UTF-8?q?117.=20=E5=A1=AB=E5=85=85=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E8=8A=82=E7=82=B9=E7=9A=84=E4=B8=8B=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8F=B3=E4=BE=A7=E8=8A=82=E7=82=B9=E6=8C=87=E9=92=88II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ds/bfs/leetcode117/Solution.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/main/java/ds/bfs/leetcode117/Solution.java b/src/main/java/ds/bfs/leetcode117/Solution.java index 9b02807..162e811 100644 --- a/src/main/java/ds/bfs/leetcode117/Solution.java +++ b/src/main/java/ds/bfs/leetcode117/Solution.java @@ -1,6 +1,9 @@ package ds.bfs.leetcode117; +import java.util.ArrayDeque; +import java.util.Arrays; import java.util.LinkedList; +import java.util.List; import java.util.Queue; /** @@ -57,4 +60,51 @@ public Node connect(Node root) { } return root; } + + private Node createTree() { + Node node_1 = new Node(1); + Node node_2 = new Node(2); + Node node_3 = new Node(3); + Node node_4 = new Node(4); + Node node_5 = new Node(5); + Node node_7 = new Node(7); + node_1.left = node_2; + node_1.right = node_3; + node_2.left = node_4; + node_2.right = node_5; + node_3.right = node_7; + return node_1; + } + + private List levelOrder(Node root) { + if (root == null) { + return new LinkedList(); + } + List result = new LinkedList(); + Queue queue = new ArrayDeque(); + queue.offer(root); + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Node cur = queue.poll(); + if (cur != null) { + result.add(cur.val); + } + if (cur.left != null) { + queue.offer(cur.left); + } + if (cur.right != null) { + queue.offer(cur.right); + } + } + } + return result; + } + + public static void main(String[] args) { + Solution solution = new Solution(); + Node root = solution.createTree(); + Node result = solution.connect(root); + System.out.println(Arrays.toString(solution.levelOrder(result).stream().mapToInt(value -> value).toArray())); + } } From a4d9883fe1f337c86208a6db899538cdfa8a41c9 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年5月10日 23:24:59 +0800 Subject: [PATCH 033/115] =?UTF-8?q?20.=20=E6=9C=89=E6=95=88=E7=9A=84?= =?UTF-8?q?=E6=8B=AC=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/stack/leetcode20/Solution.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/ds/stack/leetcode20/Solution.java b/src/main/java/ds/stack/leetcode20/Solution.java index be810c9..f2df444 100644 --- a/src/main/java/ds/stack/leetcode20/Solution.java +++ b/src/main/java/ds/stack/leetcode20/Solution.java @@ -23,7 +23,7 @@ public boolean isValid(String s) { return false; } else if (!stack.peek().equals(String.valueOf(s.charAt(i)))) { return false; - }else { + } else { stack.pop(); } } From e88d651e31be499e69a1a45d7fa9a735e5479475 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年5月12日 22:14:35 +0800 Subject: [PATCH 034/115] =?UTF-8?q?287.=20=E5=AF=BB=E6=89=BE=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/pointer/leetcode287/Solution.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ds/pointer/leetcode287/Solution.java b/src/main/java/ds/pointer/leetcode287/Solution.java index 8d3c0df..a0b44e1 100644 --- a/src/main/java/ds/pointer/leetcode287/Solution.java +++ b/src/main/java/ds/pointer/leetcode287/Solution.java @@ -9,8 +9,7 @@ public class Solution { public int findDuplicate(int[] nums) { - int slow = 0; - int fast = 0; + int slow = 0, fast = 0; slow = nums[slow]; fast = nums[nums[fast]]; while (slow != fast) { From 100840361faf53a0ccc75baa6fb9eb65a2c9f303 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年5月12日 23:03:30 +0800 Subject: [PATCH 035/115] =?UTF-8?q?15.=20=E4=B8=89=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ds/pointer/leetcode15/Solution.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/ds/pointer/leetcode15/Solution.java b/src/main/java/ds/pointer/leetcode15/Solution.java index 6365916..0117fcb 100644 --- a/src/main/java/ds/pointer/leetcode15/Solution.java +++ b/src/main/java/ds/pointer/leetcode15/Solution.java @@ -22,28 +22,28 @@ public List> threeSum(int[] nums) { if (i> 0 && nums[i] == nums[i - 1]) { continue; } - int left = i + 1; - int right = nums.length - 1; - while (left < right) { - int threeSum = nums[i] + nums[left] + nums[right]; - if (threeSum < 0) { - left++; - } else if (threeSum> 0) { - right--; + int start = i + 1; + int end = nums.length - 1; + while (start < end) { + int sum = nums[i] + nums[start] + nums[end]; + if (sum < 0) { + start++; + } else if (sum> 0) { + end--; } else { - LinkedList res = new LinkedList(); - res.add(nums[i]); - res.add(nums[left]); - res.add(nums[right]); - result.add(res); - while (left < right && nums[left] == nums[left + 1]) { - left++; + List num = new LinkedList(); + num.add(nums[i]); + num.add(nums[start]); + num.add(nums[end]); + result.add(num); + while (start < end && nums[start] == nums[start + 1]) { + start++; } - while (left < right && nums[right] == nums[right - 1]) { - right--; + while (start < end && nums[end] == nums[end - 1]) { + end--; } - right--; - left++; + start++; + end--; } } } From be0c9b381d3c2da845b32a94eca2225fb94df33d Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年5月13日 00:01:54 +0800 Subject: [PATCH 036/115] =?UTF-8?q?18.=20=E5=9B=9B=E6=95=B0=E4=B9=8B?= =?UTF-8?q?=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/ds/pointer/leetcode18/Solution.java | 33 +++++++++++-------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/main/java/ds/pointer/leetcode18/Solution.java b/src/main/java/ds/pointer/leetcode18/Solution.java index bd590b9..27d35bf 100644 --- a/src/main/java/ds/pointer/leetcode18/Solution.java +++ b/src/main/java/ds/pointer/leetcode18/Solution.java @@ -15,7 +15,7 @@ public class Solution { public List> fourSum(int[] nums, int target) { Arrays.sort(nums); - List> result = new ArrayList(); + List> result = new LinkedList(); for (int i = 0; i < nums.length; i++) { if (i> 0 && nums[i] == nums[i - 1]) { continue; @@ -24,24 +24,29 @@ public List> fourSum(int[] nums, int target) { if (j> i + 1 && nums[j] == nums[j - 1]) { continue; } - int left = j + 1; - int right = nums.length - 1; - while (left < right) { - int fourSum = nums[i] + nums[j] + nums[left] + nums[right]; + int start = j + 1; + int end = nums.length - 1; + while (start < end) { + int fourSum = nums[i] + nums[j] + nums[start] + nums[end]; if (fourSum < target) { - left++; + start++; } else if (fourSum> target) { - right--; + end--; } else { - result.add(new LinkedList(Arrays.asList(nums[i], nums[j], nums[left], nums[right]))); - while (left < right && nums[left] == nums[left + 1]) { - left++; + List four = new LinkedList(); + four.add(nums[i]); + four.add(nums[j]); + four.add(nums[start]); + four.add(nums[end]); + result.add(four); + while (start < end && nums[start] == nums[start + 1]) { + start++; } - while (left < right && nums[right] == nums[right - 1]) { - right--; + while (start < end && nums[end] == nums[end - 1]) { + end--; } - left++; - right--; + start++; + end--; } } } From 5e228c73610c8775f3d95f4535297b4e552fd7ec Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月11日 17:23:18 +0800 Subject: [PATCH 037/115] =?UTF-8?q?=E3=80=900=20=E5=92=8C=201=20=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=9B=B8=E5=90=8C=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E3=80=91=E5=92=8C=E3=80=90=E5=B7=A6=E5=8F=B3=E4=B8=A4=E8=BE=B9?= =?UTF-8?q?=E5=AD=90=E6=95=B0=E7=BB=84=E7=9A=84=E5=92=8C=E7=9B=B8=E7=AD=89?= =?UTF-8?q?=E3=80=91=E5=88=86=E7=B1=BB=E8=87=B3=E5=93=88=E5=B8=8C=E8=A1=A8?= =?UTF-8?q?=E7=B1=BB=E5=88=AB=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index b8fa86b..de74614 100644 --- a/README.md +++ b/README.md @@ -147,8 +147,6 @@ - [18.四数之和](/src/main/java/ds/pointer/leetcode18/Solution.java) - [剑指offer 05.替换空格](/src/main/java/ds/pointer/targetoffer05/Solution.java) - [剑指 Offer II 014.字符串中的变位词](/src/main/java/ds/pointer/targetofferII014/Solution.java) - - [剑指 Offer II 011.0 和 1 个数相同的子数组](/src/main/java/ds/hashmap/targetofferII011/Solution.java) - - [剑指 Offer II 012.左右两边子数组的和相等](/src/main/java/ds/hashmap/targetofferII012/Solution.java) - [剑指 Offer II 015.字符串中的所有变位词](/src/main/java/ds/pointer/targetofferII015/Solution.java) - [11.盛最多水的容器](/src/main/java/ds/pointer/leetcode11/Solution.java) - [42.接雨水](/src/main/java/ds/pointer/leetcode42/Solution.java) @@ -240,7 +238,7 @@ - [剑指 Offer II 011.0 和 1 个数相同的子数组](/src/main/java/ds/hashmap/targetofferII011/Solution.java) - [剑指 Offer II 012.左右两边子数组的和相等](/src/main/java/ds/hashmap/targetofferII012/Solution.java) - [剑指 Offer II 013.二维子矩阵的和](/src/main/java/ds/hashmap/targetofferII013/NumMatrix.java) - + 14. 字符串 - [Fizz Buzz](/src/main/java/ds/string/FizzBuzz.java) - [验证回文字符](/src/main/java/ds/string/ValidPalindromeString.java) From 9a50e75ef55cc30cd83f348924518663d7f01ef1 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月11日 22:34:43 +0800 Subject: [PATCH 038/115] =?UTF-8?q?=E3=80=90=E5=89=91=E6=8C=87=20Offer=20I?= =?UTF-8?q?I=20011.=200=20=E5=92=8C=201=20=E4=B8=AA=E6=95=B0=E7=9B=B8?= =?UTF-8?q?=E5=90=8C=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84=E3=80=91=E5=92=8C?= =?UTF-8?q?=E3=80=90=E5=89=91=E6=8C=87=20Offer=20II=20012.=20=E5=B7=A6?= =?UTF-8?q?=E5=8F=B3=E4=B8=A4=E8=BE=B9=E5=AD=90=E6=95=B0=E7=BB=84=E7=9A=84?= =?UTF-8?q?=E5=92=8C=E7=9B=B8=E7=AD=89=E3=80=91=E5=88=86=E7=B1=BB=E8=87=B3?= =?UTF-8?q?=E5=93=88=E5=B8=8C=E8=A1=A8=E7=B1=BB=E5=88=AB=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 067e1e1..fca84a3 100644 --- a/README.md +++ b/README.md @@ -148,8 +148,6 @@ - [18.四数之和](/src/main/java/ds/pointer/leetcode18/Solution.java) - [剑指offer 05.替换空格](/src/main/java/ds/pointer/targetoffer05/Solution.java) - [剑指 Offer II 014.字符串中的变位词](/src/main/java/ds/pointer/targetofferII014/Solution.java) - - [剑指 Offer II 011.0 和 1 个数相同的子数组](/src/main/java/ds/hashmap/targetofferII011/Solution.java) - - [剑指 Offer II 012.左右两边子数组的和相等](/src/main/java/ds/hashmap/targetofferII012/Solution.java) - [剑指 Offer II 015.字符串中的所有变位词](/src/main/java/ds/pointer/targetofferII015/Solution.java) - [11.盛最多水的容器](/src/main/java/ds/pointer/leetcode11/Solution.java) - [42.接雨水](/src/main/java/ds/pointer/leetcode42/Solution.java) @@ -241,7 +239,7 @@ - [剑指 Offer II 011.0 和 1 个数相同的子数组](/src/main/java/ds/hashmap/targetofferII011/Solution.java) - [剑指 Offer II 012.左右两边子数组的和相等](/src/main/java/ds/hashmap/targetofferII012/Solution.java) - [剑指 Offer II 013.二维子矩阵的和](/src/main/java/ds/hashmap/targetofferII013/NumMatrix.java) - + 14. 字符串 - [Fizz Buzz](/src/main/java/ds/string/FizzBuzz.java) - [验证回文字符](/src/main/java/ds/string/ValidPalindromeString.java) From 488e5e851148b7bbb5f145bcef1cd6ee30a7efbe Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月11日 23:04:12 +0800 Subject: [PATCH 039/115] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=20II=20006.?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E5=AD=97=E4=B9=8B=E5=92=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../ds/binary/targetofferII06/Solution.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/ds/binary/targetofferII06/Solution.java diff --git a/README.md b/README.md index fca84a3..101c7bf 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,7 @@ - [剑指offer 54.二叉搜索树的第k大节点](/src/main/java/ds/tree/target54/Solution.java) 4. 二分查找 + - [剑指 Offer II 006.排序数组中两个数字之和](/src/main/java/ds/binary/targetofferII06/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) diff --git a/src/main/java/ds/binary/targetofferII06/Solution.java b/src/main/java/ds/binary/targetofferII06/Solution.java new file mode 100644 index 0000000..9871be3 --- /dev/null +++ b/src/main/java/ds/binary/targetofferII06/Solution.java @@ -0,0 +1,32 @@ +package ds.binary.targetofferII06; + +import java.util.Arrays; + +/** + * 排序数组中两个数字之和 + * 剑指 Offer II 006 https://leetcode.cn/problems/kLl5u1/ + * + * @author yangyi 2022年07月11日22:55:07 + */ +public class Solution { + + public int[] twoSum(int[] numbers, int target) { + int start = 0, end = numbers.length - 1; + while (start < end) { + if (numbers[start] + numbers[end] < target) { + start++; + } else if (numbers[start] + numbers[end]> target) { + end--; + } else { + return new int[]{start, end}; + } + } + return new int[]{-1, -1}; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(new Solution().twoSum(new int[]{1, 2, 4, 6, 10}, 8))); + System.out.println(Arrays.toString(new Solution().twoSum(new int[]{2, 3, 4}, 6))); + System.out.println(Arrays.toString(new Solution().twoSum(new int[]{-1, 0}, -1))); + } +} From 910ec52cd97b6310caee9ebd5ea66bf308da6fa4 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 11:19:28 +0800 Subject: [PATCH 040/115] =?UTF-8?q?=E5=89=91=E6=8C=87=20Offer=20II=20068.?= =?UTF-8?q?=20=E6=9F=A5=E6=89=BE=E6=8F=92=E5=85=A5=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../ds/binary/targetofferII068/Solution.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/ds/binary/targetofferII068/Solution.java diff --git a/README.md b/README.md index de74614..b28c5f0 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,7 @@ - [查找最后一个小于等于给定值的元素](/src/main/java/ds/binary/BSEndLess.java) - [x的平方根](/src/main/java/ds/binary/MySqrt.java) - [35.搜索插入位置](/src/main/java/ds/binary/leetcode35/Solution.java) + - [剑指 Offer II 068.查找插入位置](/src/main/java/ds/binary/targetofferII068/Solution.java) 5. 双指针 - [141.环形链表](/src/main/java/ds/pointer/leetcode141/Solution.java) diff --git a/src/main/java/ds/binary/targetofferII068/Solution.java b/src/main/java/ds/binary/targetofferII068/Solution.java new file mode 100644 index 0000000..e9e7ec3 --- /dev/null +++ b/src/main/java/ds/binary/targetofferII068/Solution.java @@ -0,0 +1,32 @@ +package ds.binary.targetofferII068; + +/** + * 查找插入位置 + * 剑指 Offer II 068. https://leetcode.cn/problems/N6YdxV/ + * + * @author yangyi 2021年01月15日16:35:55 + */ +public class Solution { + + public int searchInsert(int[] nums, int target) { + int start = 0, end = nums.length - 1; + while (start <= end) { + int middle = start + (end - start) / 2; + if (nums[middle] < target) { + start = middle + 1; + } else if (nums[middle]> target) { + end = middle - 1; + } else { + return middle; + } + } + return end + 1; + } + + public static void main(String[] args) { + System.out.println(new Solution().searchInsert(new int[]{1, 3, 5, 6}, 5)); + System.out.println(new Solution().searchInsert(new int[]{1, 3, 5, 6}, 2)); + System.out.println(new Solution().searchInsert(new int[]{1, 3, 5, 6}, 7)); + System.out.println(new Solution().searchInsert(new int[]{1, 3, 5, 6}, 0)); + } +} From ad8775846d327d53998875c9b63d9e49909d1f3c Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 12:16:03 +0800 Subject: [PATCH 041/115] =?UTF-8?q?704.=20=E4=BA=8C=E5=88=86=E6=9F=A5?= =?UTF-8?q?=E6=89=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 113 +++++++++--------- .../java/ds/binary/leetcode704/Solution.java | 30 +++++ 2 files changed, 88 insertions(+), 55 deletions(-) create mode 100644 src/main/java/ds/binary/leetcode704/Solution.java diff --git a/README.md b/README.md index b28c5f0..76864c8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # 常见数据结构与算法小结(Java语言描述) -这是一个数据结构和算法笔记本,**书写** 并 **整理**一些常见的数据结构和其对应的相关操作。这其中**每一个类文件都是一个可以单独运行查看结果的main方法类**,相关的关键描述和想说的话都在代码的注释中。(欢迎一同补充和完善,2019年01月04日00:07:40置为public) +这是一个数据结构和算法笔记本,**书写** 并 **整理**一些常见的数据结构和其对应的相关操作。这其中**每一个类文件都是一个可以单独运行查看结果的main方法类** +,相关的关键描述和想说的话都在代码的注释中。(欢迎一同补充和完善,2019年01月04日00:07:40置为public) 1. 数组 - [搜索二维矩阵](/src/main/java/ds/FindInDoubleArray.java) @@ -26,32 +27,32 @@ - [线性表的删除(基于链式存储的物理结构实现)](/src/main/java/ds/LinkDelete.java) - [线性表的创建(基于链式存储的物理结构)头插法和尾插法](/src/main/java/ds/LinkCreate.java) - [查找线性表中的某一个元素(基于链式存储的物理结构实现)](/src/main/java/ds/LinkGet.java) - + 2. 栈 - [顺序栈(基于数组实现的栈)](/src/main/java/ds/ArrayStack.java) - [链式栈(基于链表实现的栈)](/src/main/java/ds/LinkStack.java) - + 3. 队列 - [顺序队列(基于数组实现的队列)](/src/main/java/ds/ArrayQueue.java) - [链式队列(基于链表实现的队列)](/src/main/java/ds/LinkQueue.java) - [循环队列(基于数组成环)](/src/main/java/ds/CircleQueue.java) - + 5. 递归 - [n的阶乘](/src/main/java/ds/Factorial.java) - [斐波拉切数列](/src/main/java/ds/FibonacciArray.java) - [位于第几排问题(递归、非递归分别实现)](/src/main/java/ds/LocationRow.java) - [n个台阶走法问题(每次可以走1个台阶或者2个台阶)](/src/main/java/ds/OneTwoStep.java) - [输出指定路径下的所有文件名(递归、非递归分别实现)](/src/main/java/ds/FileSearch.java) - + 6. 分治 - - [x的n次方](/src/main/java/ds/Pow.java) + - [x的n次方](/src/main/java/ds/Pow.java) 11. 堆 - [TopK](/src/main/java/ds/KthLargest.java) - [数组中的第K个最大元素](/src/main/java/ds/FindKthLargest.java) - [前K个高频元素](/src/main/java/ds/TopKFrequent.java) - [前K个高频单词](/src/main/java/ds/TopKWord.java) - + 13. 求数 - [求众数](/src/main/java/ds/MajorityElement.java) - [加一](/src/main/java/ds/PlusOne.java) @@ -62,10 +63,10 @@ 14. 搜索 - [广度优先搜索](/src/main/java/ds/LevelPrint.java) - [深度优先搜索](/src/main/java/ds/DepthPrint.java) - + 15. 图 - [图的表示](/src/main/java/ds/Graph.java) - + ## LeetCode 1. 反转 @@ -77,7 +78,7 @@ - [557.反转字符串中的单词III](/src/main/java/ds/reverse/leetcode557/Solution.java) - [867.转置矩阵](/src/main/java/ds/reverse/leetcode867/Solution.java) - [反转单链表的一部分区间](/src/main/java/ds/reverse/ReverseLinkN.java) - + 2. 二叉树 - [144.二叉树的前序遍历](/src/main/java/ds/tree/leetcode144/Solution.java) - [二叉树的下一个节点](/src/main/java/ds/tree/NextNodeInTree.java) @@ -104,7 +105,7 @@ - [236.二叉树的最近公共祖先](/src/main/java/ds/tree/leetcode236/Solution.java) - [814.二叉树剪枝](/src/main/java/ds/tree/leetcode814/Solution.java) - [965.单值二叉树](/src/main/java/ds/tree/leetcode965/Solution.java) - + 3. 二叉查找树 - [700.二叉搜索树中的搜索](/src/main/java/ds/bst/leetcode700/Solution.java) - [98.验证二叉搜索树](/src/main/java/ds/bst/leetcode98/Solution.java) @@ -123,6 +124,7 @@ - [剑指offer 54.二叉搜索树的第k大节点](/src/main/java/ds/tree/target54/Solution.java) 4. 二分查找 + - [704.二分查找](/src/main/java/ds/binary/leetcode704/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) @@ -131,7 +133,7 @@ - [x的平方根](/src/main/java/ds/binary/MySqrt.java) - [35.搜索插入位置](/src/main/java/ds/binary/leetcode35/Solution.java) - [剑指 Offer II 068.查找插入位置](/src/main/java/ds/binary/targetofferII068/Solution.java) - + 5. 双指针 - [141.环形链表](/src/main/java/ds/pointer/leetcode141/Solution.java) - [142.环形链表II (已知链表当中有环,返回这个环的起始位置)](/src/main/java/ds/pointer/leetcode142/Solution.java) @@ -151,7 +153,7 @@ - [剑指 Offer II 015.字符串中的所有变位词](/src/main/java/ds/pointer/targetofferII015/Solution.java) - [11.盛最多水的容器](/src/main/java/ds/pointer/leetcode11/Solution.java) - [42.接雨水](/src/main/java/ds/pointer/leetcode42/Solution.java) - + 6. 滑动窗口 - [一个数组所有连续K个元素构成的子集的平均数](/src/main/java/ds/sliding/ArrayAverages.java) - [最小覆盖子串](/src/main/java/ds/sliding/MinWindow.java) @@ -166,38 +168,38 @@ - [380.常数时间插入、删除和获取随机元素](/src/main/java/ds/design/leetcode380/RandomizedSet.java) - [232.用栈实现队列](/src/main/java/ds/stack/leetcode232/MyQueue.java) - [225.用队列实现栈](/src/main/java/ds/queue/leetcode225/MyStack.java) - + 8. 位运算 - [位1的个数](/src/main/java/ds/bit/HammingWeight.java) - [2的幂](/src/main/java/ds/bit/IsPowerOfTwo.java) - [比特位计数](/src/main/java/ds/bit/CountBits.java) - [136.只出现一次的数字](/src/main/java/ds/bit/leetcode136/Solution.java) - [缺失数字](/src/main/java/ds/bit/MissingNumber.java) - + 9. 回溯(DFS) + 剪枝 - 排列 - - [46.全排列](/src/main/java/ds/backtrack/Permute.java) - - [47.全排列II](/src/main/java/ds/backtrack/PermuteUnique.java) + - [46.全排列](/src/main/java/ds/backtrack/Permute.java) + - [47.全排列II](/src/main/java/ds/backtrack/PermuteUnique.java) - 组合 - - [77.组合](/src/main/java/ds/backtrack/Combine.java) - - [216.组合总和III](/src/main/java/ds/backtrack/CombinationSum3.java) - - [39.组合总和](/src/main/java/ds/backtrack/CombinationSum.java) - - [17.电话号码的字母组合](/src/main/java/ds/backtrack/LetterCombinations.java) - - [40.组合总和II](/src/main/java/ds/backtrack/CombinationSum2.java) + - [77.组合](/src/main/java/ds/backtrack/Combine.java) + - [216.组合总和III](/src/main/java/ds/backtrack/CombinationSum3.java) + - [39.组合总和](/src/main/java/ds/backtrack/CombinationSum.java) + - [17.电话号码的字母组合](/src/main/java/ds/backtrack/LetterCombinations.java) + - [40.组合总和II](/src/main/java/ds/backtrack/CombinationSum2.java) - 分割 - - [131.分割回文串](/src/main/java/ds/backtrack/Partition.java) - - [93. 复原IP地址](/src/main/java/ds/backtrack/RestoreIpAddresses.java) + - [131.分割回文串](/src/main/java/ds/backtrack/Partition.java) + - [93. 复原IP地址](/src/main/java/ds/backtrack/RestoreIpAddresses.java) - 子集 - - [78.子集](/src/main/java/ds/backtrack/Subsets.java) - - [90.子集II](/src/main/java/ds/backtrack/SubsetsWithDup.java) - - [491.递增子序列](/src/main/java/ds/backtrack/FindSubsequences.java) + - [78.子集](/src/main/java/ds/backtrack/Subsets.java) + - [90.子集II](/src/main/java/ds/backtrack/SubsetsWithDup.java) + - [491.递增子序列](/src/main/java/ds/backtrack/FindSubsequences.java) - 棋盘 - - [51.N皇后](/src/main/java/ds/backtrack/SolveNQueens.java) - - [37.解数独](/src/main/java/ds/backtrack/SolveSudoku.java) + - [51.N皇后](/src/main/java/ds/backtrack/SolveNQueens.java) + - [37.解数独](/src/main/java/ds/backtrack/SolveSudoku.java) - 路径 - - [112.路径总和](/src/main/java/ds/tree/leetcode112/Solution.java) - - [113.路径总和II](/src/main/java/ds/tree/leetcode113/Solution.java) - - [257.二叉树的所有路径](/src/main/java/ds/tree/leetcode257/Solution.java) + - [112.路径总和](/src/main/java/ds/tree/leetcode112/Solution.java) + - [113.路径总和II](/src/main/java/ds/tree/leetcode113/Solution.java) + - [257.二叉树的所有路径](/src/main/java/ds/tree/leetcode257/Solution.java) 10. 广度优先搜索(BFS) - [111.二叉树的最小深度](/src/main/java/ds/bfs/leetcode111/Solution.java) @@ -223,7 +225,7 @@ - [剑指offer 06.从尾到头打印链表](/src/main/java/ds/link/targetoffer06/Solution.java) - [剑指offer 18.删除链表中的节点](/src/main/java/ds/link/targetoffer18/Solution.java) - [234.回文链表](/src/main/java/ds/link/leetcode234/Solution.java) - + 13. 哈希表 - [242.有效的字母异位词](/src/main/java/ds/hashmap/leetcode242/Solution.java) - [349.两个数组的交集](/src/main/java/ds/hashmap/leetcode349/Solution.java) @@ -249,7 +251,7 @@ - [根据字符出现频率排序](/src/main/java/ds/string/FrequencySort.java) - [验证回文串](/src/main/java/ds/string/PalindromeI.java) - [单词拆分](/src/main/java/ds/string/WordBreak.java) - + 15. 排序 - [冒泡排序](/src/main/java/ds/sort/BubbleSort.java) - [插入排序](/src/main/java/ds/sort/InsertSort.java) @@ -257,47 +259,48 @@ - [快速排序](/src/main/java/ds/sort/QuickSort.java) - [计数排序](/src/main/java/ds/sort/CountSort.java) - [堆排序](/src/main/java/ds/sort/BigHeapSort.java) - + 16. 贪心 - [455.分发饼干](/src/main/java/ds/greedy/leetcode455/Solution.java) - [376.摆动序列](/src/main/java/ds/greedy/leetcode376/Solution.java) - [53.最大子序和](/src/main/java/ds/greedy/leetcode53/Solution.java) - [122.买卖股票的最佳时机II](/src/main/java/ds/greedy/leetcode122/Solution.java) - + 17. 动态规划(DP) - 斐波拉切 - - [斐波拉契数列的4种解法](/src/main/java/ds/dp/Fipolach.java) - - [509.斐波那契数](/src/main/java/ds/dp/leetcode509/Solution.java) - - [70.爬楼梯](/src/main/java/ds/dp/leetcode70/Solution.java) - - [746.使用最小花费爬楼梯](/src/main/java/ds/dp/leetcode746/Solution.java) + - [斐波拉契数列的4种解法](/src/main/java/ds/dp/Fipolach.java) + - [509.斐波那契数](/src/main/java/ds/dp/leetcode509/Solution.java) + - [70.爬楼梯](/src/main/java/ds/dp/leetcode70/Solution.java) + - [746.使用最小花费爬楼梯](/src/main/java/ds/dp/leetcode746/Solution.java) - 路径 - - [60.不同路径](/src/main/java/ds/dp/leetcode60/Solution.java) - - [63.不同路径II](/src/main/java/ds/dp/leetcode63/Solution.java) + - [60.不同路径](/src/main/java/ds/dp/leetcode60/Solution.java) + - [63.不同路径II](/src/main/java/ds/dp/leetcode63/Solution.java) - [96.不同的二叉搜索树](/src/main/java/ds/dp/leetcode96/Solution.java) - [343.整数拆分](/src/main/java/ds/dp/leetcode343/Solution.java) - [三角形最小路径和](/src/main/java/ds/dp/MinimumTotal.java) - [乘积最大子序列](/src/main/java/ds/dp/MaxProduct.java) - 打家劫舍 - - [198.打家劫舍](/src/main/java/ds/dp/leetcode198/Solution.java) - - [213.打家劫舍II](/src/main/java/ds/dp/leetcode213/Solution.java) - - [337.打家劫舍III](/src/main/java/ds/dp/leetcode337/Solution.java) + - [198.打家劫舍](/src/main/java/ds/dp/leetcode198/Solution.java) + - [213.打家劫舍II](/src/main/java/ds/dp/leetcode213/Solution.java) + - [337.打家劫舍III](/src/main/java/ds/dp/leetcode337/Solution.java) - 杨辉三角 - - [118.杨辉三角](/src/main/java/ds/dp/leetcode118/Solution.java) - - [119.杨辉三角II](/src/main/java/ds/dp/leetcode119/Solution.java) - + - [118.杨辉三角](/src/main/java/ds/dp/leetcode118/Solution.java) + - [119.杨辉三角II](/src/main/java/ds/dp/leetcode119/Solution.java) + 18. 栈 - [20.有效的括号](/src/main/java/ds/stack/leetcode20/Solution.java) - [1047.删除字符串中的所有相邻重复项](/src/main/java/ds/stack/leetcode1047/Solution.java) - [150.逆波兰表达式求值](/src/main/java/ds/stack/leetcode150/Solution.java) - 单调栈 - - [739.每日温度](/src/main/java/ds/stack/leetcode739/Solution.java) - + - [739.每日温度](/src/main/java/ds/stack/leetcode739/Solution.java) + ## 多线程 - - [1114.按序打印](/src/main/java/thread/leetcode1114/Foo.java) - - [1115.交替打印FooBar](/src/main/java/thread/leetcode1115/FooBar.java) - - [1116.打印零与奇偶数](/src/main/java/thread/leetcode1116/ZeroEvenOdd.java) - - [1117.H2O生成](/src/main/java/thread/leetcode1117/H2O.java) - - [1195.交替打印字符串](/src/main/java/thread/leetcode1195/FizzBuzz.java) + +- [1114.按序打印](/src/main/java/thread/leetcode1114/Foo.java) +- [1115.交替打印FooBar](/src/main/java/thread/leetcode1115/FooBar.java) +- [1116.打印零与奇偶数](/src/main/java/thread/leetcode1116/ZeroEvenOdd.java) +- [1117.H2O生成](/src/main/java/thread/leetcode1117/H2O.java) +- [1195.交替打印字符串](/src/main/java/thread/leetcode1195/FizzBuzz.java) ## PAT diff --git a/src/main/java/ds/binary/leetcode704/Solution.java b/src/main/java/ds/binary/leetcode704/Solution.java new file mode 100644 index 0000000..b191b95 --- /dev/null +++ b/src/main/java/ds/binary/leetcode704/Solution.java @@ -0,0 +1,30 @@ +package ds.binary.leetcode704; + +/** + * 二分查找 + * LeetCode 704. https://leetcode.cn/problems/binary-search/ + * + * @author yangyi 2022年07月12日12:11:28 + */ +public class Solution { + + public int search(int[] nums, int target) { + int start = 0, end = nums.length - 1; + while (start <= end) { + int middle = start + (end - start) / 2; + if (nums[middle] < target) { + start = middle + 1; + } else if (nums[middle]> target) { + end = middle - 1; + } else { + return middle; + } + } + return -1; + } + + public static void main(String[] args) { + System.out.println(new Solution().search(new int[]{-1, 0, 3, 5, 9, 12}, 9)); + System.out.println(new Solution().search(new int[]{-1, 0, 3, 5, 9, 12}, 2)); + } +} From ee52cee97073693867a138fbac3f851fe6ec383a Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 14:22:54 +0800 Subject: [PATCH 042/115] =?UTF-8?q?69.=20x=20=E7=9A=84=E5=B9=B3=E6=96=B9?= =?UTF-8?q?=E6=A0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/ds/binary/MySqrt.java | 53 ------------------ .../java/ds/binary/leetcode69/Solution.java | 33 +++++++++++ target/classes/ds/binary/MySqrt.class | Bin 1147 -> 0 bytes 4 files changed, 34 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/ds/binary/MySqrt.java create mode 100644 src/main/java/ds/binary/leetcode69/Solution.java delete mode 100644 target/classes/ds/binary/MySqrt.class diff --git a/README.md b/README.md index 76864c8..6807664 100644 --- a/README.md +++ b/README.md @@ -125,12 +125,12 @@ 4. 二分查找 - [704.二分查找](/src/main/java/ds/binary/leetcode704/Solution.java) + - [69.x的平方根](/src/main/java/ds/binary/leetcode69/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) - [查找第一个大于等于给定值的元素](/src/main/java/ds/binary/BSFirstMore.java) - [查找最后一个小于等于给定值的元素](/src/main/java/ds/binary/BSEndLess.java) - - [x的平方根](/src/main/java/ds/binary/MySqrt.java) - [35.搜索插入位置](/src/main/java/ds/binary/leetcode35/Solution.java) - [剑指 Offer II 068.查找插入位置](/src/main/java/ds/binary/targetofferII068/Solution.java) diff --git a/src/main/java/ds/binary/MySqrt.java b/src/main/java/ds/binary/MySqrt.java deleted file mode 100644 index a41512a..0000000 --- a/src/main/java/ds/binary/MySqrt.java +++ /dev/null @@ -1,53 +0,0 @@ -package ds.binary; - -/** - * 实现 int sqrt(int x) 函数。 - *

- * 计算并返回 x 的平方根,其中 x 是非负整数。 - *

- * 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 - *

- * 示例 1: - *

- * 输入: 4 - * 输出: 2 - * 示例 2: - *

- * 输入: 8 - * 输出: 2 - * 说明: 8 的平方根是 2.82842..., - * 由于返回类型是整数,小数部分将被舍去。 - * - * @author yangyi 2019年01月29日22:12:30 - */ -public class MySqrt { - - public int mySqrt(int x) { - if (x == 0 || x == 1) { - return x; - } - int start = 0; - int end = x; - int result = 0; - while (start <= end) { - int m = start + ((end - start)>> 1); - if (m < x / m) { - start = m + 1; - //因为了开平方根取整数,有可能真正的结果比实际的算术结果小,所以要及时记录且返回 - result = m; - } else if (m> x / m) { - end = m - 1; - } else { - return m; - } - } - return result; - } - - public static void main(String[] args) { - MySqrt mySqrt = new MySqrt(); - int x = 8; - System.out.println(x + "的二次根号为:" + mySqrt.mySqrt(x)); - } - -} diff --git a/src/main/java/ds/binary/leetcode69/Solution.java b/src/main/java/ds/binary/leetcode69/Solution.java new file mode 100644 index 0000000..b5253ec --- /dev/null +++ b/src/main/java/ds/binary/leetcode69/Solution.java @@ -0,0 +1,33 @@ +package ds.binary.leetcode69; + +/** + * x的平方根 + * LeetCode 69. https://leetcode.cn/problems/sqrtx/ + * + * @author yangyi 2022年07月12日14:16:11 + */ +public class Solution { + + public int mySqrt(int x) { + if (x == 0 || x == 1) { + return x; + } + int start = 0, end = x; + while (start <= end) { + int middle = start + (end - start) / 2; + if (middle < x / middle) { + start = middle + 1; + } else if (middle> x / middle) { + end = middle - 1; + } else { + return middle; + } + } + return end; + } + + public static void main(String[] args) { + System.out.println(new Solution().mySqrt(4)); + System.out.println(new Solution().mySqrt(8)); + } +} diff --git a/target/classes/ds/binary/MySqrt.class b/target/classes/ds/binary/MySqrt.class deleted file mode 100644 index 2aac1e424afd84d8318288f6d04b2a323263e5a8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1147 zcmaJ=OHUI~7(KV0X-ivdEeu6lu)L(@RUT52M`JJ{At_=~Lt@-8Z6`R`2TrFdu3ehA z))hVyHr?o=C?xLncOd=;L(nrV1_k0`?)_f(`_4IY@3*7fF95D#){SQLI??Bb4ZTgc zihd`eZpi4hmY53z);?&Zhb(K@jSP6j53fQBu6#?m1DW?heQhHIlT`6QV z)j<#Wt~X*2!EFlX4fP`B7Jx)@s&5CwkL z{!~CNo2p@QrdG_c($LD4Jl*EBsb(K8sT+UV7gW6{&=$R)T2~*d@w{4Gi>FOPFRsnh z!K%g@$G4o57I3Fa6(g%H=oWfU1J;Pu#)}K^3HVQAc4EwnaZCsVPfwe#==q#xc&YUy z=lyv7e1G@FLG{hS*5~gpclJN+PFtyj7Yf?ExQ;0&Zg?>b%HTigwVYYkvZVP}D7{%W zH40-dRhUp3H1$&a4mp}+s;LD_N_AtSu2h&(o{{1h9P5UoXOc7jt9O~DhMEGc(X)l@ z*iuLTFCYQuh6OLrU!$BiaRptxXwAHufE~h;o2#E*oV(yI59~mEpkc#Bu8w*zhyZPG zqW~>vrN;?q<7yo~whsf%<h`a%id1DwdmToSIDyM(LI@sokU=LSR|qSDAUQ?Q zjTn0PbowyKN0vlC78qH?AW|5r!%q@ogs~vn`OXOTn22RvVvLs@^PJI6>OS1$q;^gZ zU=Cqg5~qSEoK4~g4k5%Lx`qEIY&L@GB$S2{PgrPY;qV@8_jX`U`CW4#A?-n?=Lpx} z47h4&8l>f>Q-h}l?}^+29tTF@!Wemu6Zu3Pd!*5n_`8^Qk)^_)z{f-07Iqu$Zf0!g F`3-vx=a>Kh From 30e1eb7b38bb0d19c0fba64babec83cdbe68d036 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 15:15:14 +0800 Subject: [PATCH 043/115] =?UTF-8?q?374.=20=E7=8C=9C=E6=95=B0=E5=AD=97?= =?UTF-8?q?=E5=A4=A7=E5=B0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/binary/leetcode374/Solution.java | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/ds/binary/leetcode374/Solution.java diff --git a/README.md b/README.md index 6807664..242cbc6 100644 --- a/README.md +++ b/README.md @@ -126,6 +126,7 @@ 4. 二分查找 - [704.二分查找](/src/main/java/ds/binary/leetcode704/Solution.java) - [69.x的平方根](/src/main/java/ds/binary/leetcode69/Solution.java) + - [374.猜数字大小](/src/main/java/ds/binary/leetcode374/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) diff --git a/src/main/java/ds/binary/leetcode374/Solution.java b/src/main/java/ds/binary/leetcode374/Solution.java new file mode 100644 index 0000000..b84cbe4 --- /dev/null +++ b/src/main/java/ds/binary/leetcode374/Solution.java @@ -0,0 +1,51 @@ +package ds.binary.leetcode374; + +/** + * 猜数字大小 + * LeetCode 374. https://leetcode.cn/problems/guess-number-higher-or-lower/ + * + * @author yangyi 2022年07月12日15:04:14 + */ +public class Solution { + + private final int pick; + + public Solution(int pick) { + this.pick = pick; + } + + public int guessNumber(int num) { + int start = 0, end = num; + while (start <= end) { + int middle = start + (end - start) / 2; + if (guess(middle) == 1) { + start = middle + 1; + } else if (guess(middle) == -1) { + end = middle - 1; + } else { + return middle; + } + } + return -1; + } + + /** + * Forward declaration of guess API. + * + * @param num your guess + * @return -1 if num is lower than the guess number + * 1 if num is higher than the guess number + * otherwise return 0 + * int guess(int num); + */ + private int guess(int num) { + return Integer.compare(pick, num); + } + + public static void main(String[] args) { + System.out.println(new Solution(6).guessNumber(10)); + System.out.println(new Solution(1).guessNumber(1)); + System.out.println(new Solution(1).guessNumber(2)); + System.out.println(new Solution(2).guessNumber(2)); + } +} From df61ddbe4231dd7cb36b1c02297317dea007684e Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 16:52:00 +0800 Subject: [PATCH 044/115] =?UTF-8?q?33.=E6=90=9C=E7=B4=A2=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/binary/leetcode33/Solution.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/ds/binary/leetcode33/Solution.java diff --git a/README.md b/README.md index 242cbc6..1e7de8b 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ - [704.二分查找](/src/main/java/ds/binary/leetcode704/Solution.java) - [69.x的平方根](/src/main/java/ds/binary/leetcode69/Solution.java) - [374.猜数字大小](/src/main/java/ds/binary/leetcode374/Solution.java) + - [33.搜索旋转排序数组](/src/main/java/ds/binary/leetcode33/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) diff --git a/src/main/java/ds/binary/leetcode33/Solution.java b/src/main/java/ds/binary/leetcode33/Solution.java new file mode 100644 index 0000000..2a3b497 --- /dev/null +++ b/src/main/java/ds/binary/leetcode33/Solution.java @@ -0,0 +1,47 @@ +package ds.binary.leetcode33; + +/** + * 搜索旋转排序数组 + * LeetCode 33. https://leetcode.cn/problems/search-in-rotated-sorted-array/ + * + * @author yangyi 2022年07月12日15:29:12 + */ +public class Solution { + + public int search(int[] nums, int target) { + int start = 0, end = nums.length - 1; + if (end == 0) { + return nums[end] == target ? end : -1; + } + while (start <= end) { + int middle = start + (end - start) / 2; + if (nums[middle] == target) { + return middle; + } + if (nums[start] <= nums[middle]) { + if (nums[start] <= target && target < nums[middle]) { + end = middle - 1; + } else { + start = middle + 1; + } + } else if (nums[start]> nums[middle]) { + if (nums[middle] < target && target <= nums[end]) { + start = middle + 1; + } else { + end = middle - 1; + } + } + } + return -1; + } + + public static void main(String[] args) { + System.out.println(new Solution().search(new int[]{4, 5, 6, 7, 0, 1, 2}, 0)); + System.out.println(new Solution().search(new int[]{4, 5, 6, 7, 0, 1, 2}, 3)); + System.out.println(new Solution().search(new int[]{1}, 0)); + System.out.println(new Solution().search(new int[]{1}, 1)); + System.out.println(new Solution().search(new int[]{1, 3}, 2)); + System.out.println(new Solution().search(new int[]{3, 1}, 0)); + System.out.println(new Solution().search(new int[]{5, 1, 3}, 3)); + } +} From 25098c2550aa4c98d7e3a54e5de60658337bac88 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 17:54:44 +0800 Subject: [PATCH 045/115] =?UTF-8?q?278.=20=E7=AC=AC=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/binary/leetcode278/Solution.java | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/ds/binary/leetcode278/Solution.java diff --git a/README.md b/README.md index 1e7de8b..929d645 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ - [69.x的平方根](/src/main/java/ds/binary/leetcode69/Solution.java) - [374.猜数字大小](/src/main/java/ds/binary/leetcode374/Solution.java) - [33.搜索旋转排序数组](/src/main/java/ds/binary/leetcode33/Solution.java) + - [278.第一个错误的版本](/src/main/java/ds/binary/leetcode278/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) diff --git a/src/main/java/ds/binary/leetcode278/Solution.java b/src/main/java/ds/binary/leetcode278/Solution.java new file mode 100644 index 0000000..4f6b2f6 --- /dev/null +++ b/src/main/java/ds/binary/leetcode278/Solution.java @@ -0,0 +1,43 @@ +package ds.binary.leetcode278; + +/** + * 第一个错误的版本 + * LeetCode 278. https://leetcode.cn/problems/first-bad-version/ + * + * @author yangyi 2022年07月12日17:33:15 + */ +public class Solution { + + public int firstBadVersion(int n) { + int start = 0, end = n; + while (start < end) { + int middle = start + (end - start) / 2; + if (isBadVersion(middle)) { + end = middle; + } else { + start = middle + 1; + } + } + if (start == end && isBadVersion(start)) { + return start; + } + return -1; + } + + private boolean isBadVersion(int version) { + if (version == 3) { + return false; + } else if (version == 5) { + return true; + } else if (version == 4) { + return true; + } else { + return false; + } + } + + public static void main(String[] args) { + Solution solution = new Solution(); + System.out.println(solution.firstBadVersion(5)); + } +} From 519b3b823168473abc0226fcb9a41f9c5f3a8dc3 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 18:12:31 +0800 Subject: [PATCH 046/115] =?UTF-8?q?=E4=BA=8C=E5=88=86=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF2=E4=BB=A5=E5=8F=8A=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E4=B9=A0=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++ .../java/ds/binary/leetcode162/Solution.java | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/ds/binary/leetcode162/Solution.java diff --git a/README.md b/README.md index 929d645..b178672 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,9 @@ - [374.猜数字大小](/src/main/java/ds/binary/leetcode374/Solution.java) - [33.搜索旋转排序数组](/src/main/java/ds/binary/leetcode33/Solution.java) - [278.第一个错误的版本](/src/main/java/ds/binary/leetcode278/Solution.java) + - [162.寻找峰值](/src/main/java/ds/binary/leetcode162/Solution.java) + - [852.山脉数组的峰顶索引](/src/main/java/ds/binary/leetcode162/Solution.java) + - [剑指 Offer II 069.山峰数组的顶部](/src/main/java/ds/binary/leetcode162/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) diff --git a/src/main/java/ds/binary/leetcode162/Solution.java b/src/main/java/ds/binary/leetcode162/Solution.java new file mode 100644 index 0000000..7db5835 --- /dev/null +++ b/src/main/java/ds/binary/leetcode162/Solution.java @@ -0,0 +1,35 @@ +package ds.binary.leetcode162; + +/** + * 寻找峰值 + * 山脉数组的峰顶索引 + * 山峰数组的顶部 + * LeetCode 162. https://leetcode.cn/problems/find-peak-element/ + * LeetCode 852. https://leetcode.cn/problems/peak-index-in-a-mountain-array/ + * 剑指 Offer II 069. https://leetcode.cn/problems/B1IidL/ + * + * @author yangyi 2022年07月12日17:59:02 + */ +public class Solution { + + public int findPeakElement(int[] nums) { + int start = 0, end = nums.length - 1; + while (start < end) { + int middle = start + (end - start) / 2; + if (nums[middle]> nums[middle + 1]) { + end = middle; + } else { + start = middle + 1; + } + } + if (start == end) { + return start; + } + return -1; + } + + public static void main(String[] args) { + System.out.println(new Solution().findPeakElement(new int[]{1, 2, 3, 1})); + System.out.println(new Solution().findPeakElement(new int[]{1, 2, 1, 3, 5, 6, 4})); + } +} From 1e6d3f51493409fc3d1b7d32b2c7460150223d24 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年7月12日 18:39:36 +0800 Subject: [PATCH 047/115] =?UTF-8?q?153.=E5=AF=BB=E6=89=BE=E6=97=8B?= =?UTF-8?q?=E8=BD=AC=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E6=9C=80=E5=B0=8F=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/binary/leetcode153/Solution.java | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/main/java/ds/binary/leetcode153/Solution.java diff --git a/README.md b/README.md index b178672..1eab045 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,7 @@ - [162.寻找峰值](/src/main/java/ds/binary/leetcode162/Solution.java) - [852.山脉数组的峰顶索引](/src/main/java/ds/binary/leetcode162/Solution.java) - [剑指 Offer II 069.山峰数组的顶部](/src/main/java/ds/binary/leetcode162/Solution.java) + - [153.寻找旋转排序数组中的最小值](/src/main/java/ds/binary/leetcode153/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) diff --git a/src/main/java/ds/binary/leetcode153/Solution.java b/src/main/java/ds/binary/leetcode153/Solution.java new file mode 100644 index 0000000..2651106 --- /dev/null +++ b/src/main/java/ds/binary/leetcode153/Solution.java @@ -0,0 +1,32 @@ +package ds.binary.leetcode153; + +/** + * 寻找旋转排序数组中的最小值 + * LeetCode 153. https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/ + * + * @author yangyi 2022年07月12日18:29:48 + */ +public class Solution { + + public int findMin(int[] nums) { + int start = 0, end = nums.length - 1; + while (start < end) { + int middle = start + (end - start) / 2; + if (nums[middle] < nums[end]) { + end = middle; + } else if (nums[middle]> nums[end]) { + start = middle + 1; + } + } + if (start == end) { + return nums[start]; + } + return -1; + } + + public static void main(String[] args) { + System.out.println(new Solution().findMin(new int[]{3, 4, 5, 1, 2})); + System.out.println(new Solution().findMin(new int[]{4, 5, 6, 7, 0, 1, 2})); + System.out.println(new Solution().findMin(new int[]{11, 13, 15, 17})); + } +} From 20b4c658c1a857438d318cf344f0ea84311139ab Mon Sep 17 00:00:00 2001 From: yangyi Date: Wed, 3 Aug 2022 15:47:16 +0800 Subject: [PATCH 048/115] =?UTF-8?q?34.=E5=9C=A8=E6=8E=92=E5=BA=8F=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83=E7=B4=A0=E7=9A=84?= =?UTF-8?q?=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80=E5=90=8E=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/binary/leetcode34/Solution.java | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/ds/binary/leetcode34/Solution.java diff --git a/README.md b/README.md index 1eab045..8679688 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ - [852.山脉数组的峰顶索引](/src/main/java/ds/binary/leetcode162/Solution.java) - [剑指 Offer II 069.山峰数组的顶部](/src/main/java/ds/binary/leetcode162/Solution.java) - [153.寻找旋转排序数组中的最小值](/src/main/java/ds/binary/leetcode153/Solution.java) + - [34.在排序数组中查找元素的第一个和最后一个位置](/src/main/java/ds/binary/leetcode34/Solution.java) - [二分查找(递归和非递归实现)](/src/main/java/ds/binary/BinarySearch.java) - [查找第一个值等于给定值的元素](/src/main/java/ds/binary/BSFirstEquals.java) - [查找最后一个值等于给定值的元素](/src/main/java/ds/binary/BSEndEquals.java) diff --git a/src/main/java/ds/binary/leetcode34/Solution.java b/src/main/java/ds/binary/leetcode34/Solution.java new file mode 100644 index 0000000..a72bfd0 --- /dev/null +++ b/src/main/java/ds/binary/leetcode34/Solution.java @@ -0,0 +1,51 @@ +package ds.binary.leetcode34; + +import java.util.Arrays; + +/** + * 在排序数组中查找元素的第一个和最后一个位置 + * LeetCode 34. https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/ + * + * @author yangyi 2022年07月12日18:50:04 + */ +public class Solution { + + public int[] searchRange(int[] nums, int target) { + if (nums == null || nums.length == 0) { + return new int[]{-1, -1}; + } + int left = 0, right = nums.length - 1; + while (left + 1 < right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + if (nums[left] == nums[right]) { + return new int[]{left, right}; + } else if (nums[left] != target) { + left++; + } else if (nums[right] != target) { + right--; + } + } else if (nums[mid] < target) { + left = mid; + } else { + right = mid; + } + } + if (nums[left] == target && nums[right] == target) { + return new int[]{left, right}; + } + if (nums[right] == target) { + return new int[]{right, right}; + } + if (nums[left] == target) { + return new int[]{left, left}; + } + return new int[]{-1, -1}; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(new Solution().searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8))); + System.out.println(Arrays.toString(new Solution().searchRange(new int[]{5, 7, 7, 8, 8, 10}, 6))); + System.out.println(Arrays.toString(new Solution().searchRange(new int[]{}, 0))); + } +} From ed028776012b6ffcc5088470c181693f33e70d49 Mon Sep 17 00:00:00 2001 From: yangyi Date: Wed, 3 Aug 2022 16:31:16 +0800 Subject: [PATCH 049/115] =?UTF-8?q?=E4=BA=8C=E5=88=86=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E7=9A=843=E4=B8=AA=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ds/binary/leetcode704/Solution.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ds/binary/leetcode704/Solution.java b/src/main/java/ds/binary/leetcode704/Solution.java index b191b95..2222d2d 100644 --- a/src/main/java/ds/binary/leetcode704/Solution.java +++ b/src/main/java/ds/binary/leetcode704/Solution.java @@ -2,6 +2,7 @@ /** * 二分查找 + * 二分查找的3个模板 https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/07/29/template_diagram.png * LeetCode 704. https://leetcode.cn/problems/binary-search/ * * @author yangyi 2022年07月12日12:11:28 From 669bc83beba447a20a55f17deed0b462ed205874 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月11日 16:01:43 +0800 Subject: [PATCH 050/115] =?UTF-8?q?1114.=20=E6=8C=89=E5=BA=8F=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/thread/leetcode1114/Foo2.java | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/thread/leetcode1114/Foo2.java diff --git a/src/main/java/thread/leetcode1114/Foo2.java b/src/main/java/thread/leetcode1114/Foo2.java new file mode 100644 index 0000000..8ca9b82 --- /dev/null +++ b/src/main/java/thread/leetcode1114/Foo2.java @@ -0,0 +1,63 @@ +package thread.leetcode1114; + +import java.util.concurrent.Semaphore; + +/** + * 按序打印 + * LeetCode 1114 https://leetcode-cn.com/problems/print-in-order/ + * + * @author yangyi 2022年08月11日15:52:55 + */ +public class Foo2 { + + private Semaphore firstFinish = new Semaphore(0); + private Semaphore secondFinish = new Semaphore(0); + + public Foo2() { + + } + + public void first(Runnable printFirst) throws InterruptedException { + printFirst.run(); + firstFinish.release(); + } + + public void second(Runnable printSecond) throws InterruptedException { + firstFinish.acquire(); + printSecond.run(); + secondFinish.release(); + } + + public void third(Runnable printThird) throws InterruptedException { + secondFinish.acquire(); + printThird.run(); + } + + public static void main(String[] args) throws InterruptedException { + Foo2 foo2 = new Foo2(); + Runnable printFirst = () -> System.out.println("first"); + Runnable printSecond = () -> System.out.println("second"); + Runnable printThird = () -> System.out.println("third"); + new Thread(() -> { + try { + foo2.third(printThird); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + new Thread(() -> { + try { + foo2.first(printFirst); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + new Thread(() -> { + try { + foo2.second(printSecond); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + } +} From 6cc644d1ce066397c64e09560ea9a2ab3c53968e Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月11日 16:14:13 +0800 Subject: [PATCH 051/115] =?UTF-8?q?1115.=20=E4=BA=A4=E6=9B=BF=E6=89=93?= =?UTF-8?q?=E5=8D=B0FooBar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/thread/leetcode1115/FooBar2.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/thread/leetcode1115/FooBar2.java diff --git a/src/main/java/thread/leetcode1115/FooBar2.java b/src/main/java/thread/leetcode1115/FooBar2.java new file mode 100644 index 0000000..e532f76 --- /dev/null +++ b/src/main/java/thread/leetcode1115/FooBar2.java @@ -0,0 +1,56 @@ +package thread.leetcode1115; + +import java.util.concurrent.Semaphore; + +/** + * 交替打印FooBar + * LeetCode 1115 https://leetcode-cn.com/problems/print-foobar-alternately/ + * + * @author yangyi 2022年08月11日16:05:19 + */ +public class FooBar2 { + + private int n; + private Semaphore foo = new Semaphore(0); + private Semaphore bar = new Semaphore(0); + + public FooBar2(int n) { + this.n = n; + } + + public void foo(Runnable printFoo) throws InterruptedException { + for (int i = 0; i < n; i++) { + foo.acquire(); + // printFoo.run() outputs "foo". Do not change or remove this line. + printFoo.run(); + bar.release(); + } + } + + public void bar(Runnable printBar) throws InterruptedException { + for (int i = 0; i < n; i++) { + foo.release(); + bar.acquire(); + // printBar.run() outputs "bar". Do not change or remove this line. + printBar.run(); + } + } + + public static void main(String[] args) { + FooBar2 fooBar2 = new FooBar2(100); + new Thread(() -> { + try { + fooBar2.foo(() -> System.out.println("foo")); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + new Thread(() -> { + try { + fooBar2.bar(() -> System.out.println("bar")); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + } +} From d6c1c1afd7b309d22d05b90636796314c4eaab50 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月11日 18:25:04 +0800 Subject: [PATCH 052/115] =?UTF-8?q?1117.=20H2O=20=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/thread/leetcode1117/H2O.java | 47 ++++++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main/java/thread/leetcode1117/H2O.java b/src/main/java/thread/leetcode1117/H2O.java index 2dda103..e01e07e 100644 --- a/src/main/java/thread/leetcode1117/H2O.java +++ b/src/main/java/thread/leetcode1117/H2O.java @@ -6,29 +6,60 @@ * H2O 生成 * LeetCode 1117 https://leetcode-cn.com/problems/building-h2o/ * - * @author yangyi 2022年02月08日15:39:38 + * @author yangyi 2022年08月11日17:49:01 */ public class H2O { - private Semaphore semaphoreH,semaphoreO; + private Semaphore h = new Semaphore(2); + private Semaphore o = new Semaphore(0); public H2O() { - semaphoreH = new Semaphore(2); - semaphoreO = new Semaphore(0); + } public void hydrogen(Runnable releaseHydrogen) throws InterruptedException { - semaphoreH.acquire(); + h.acquire(); // releaseHydrogen.run() outputs "H". Do not change or remove this line. releaseHydrogen.run(); - semaphoreO.release(); + o.release(); } public void oxygen(Runnable releaseOxygen) throws InterruptedException { - semaphoreO.acquire(2); + o.acquire(2); // releaseOxygen.run() outputs "O". Do not change or remove this line. releaseOxygen.run(); - semaphoreH.release(2); + h.release(2); + } + + public static void main(String[] args) { + Runnable h = () -> System.out.println("h"); + Runnable o = () -> System.out.println("o"); + int n = 3; + H2O h2O = new H2O(); + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < n * 2; i++) { + h2O.hydrogen(h); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); + new Thread(new Runnable() { + @Override + public void run() { + try { + for (int i = 0; i < n; i++) { + h2O.oxygen(o); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }).start(); } } From 65e553aa99db07f17c66fd78ba1bb2d833f6f711 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月11日 19:08:30 +0800 Subject: [PATCH 053/115] =?UTF-8?q?1226.=20=E5=93=B2=E5=AD=A6=E5=AE=B6?= =?UTF-8?q?=E8=BF=9B=E9=A4=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode1226/DiningPhilosophers.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/main/java/thread/leetcode1226/DiningPhilosophers.java diff --git a/src/main/java/thread/leetcode1226/DiningPhilosophers.java b/src/main/java/thread/leetcode1226/DiningPhilosophers.java new file mode 100644 index 0000000..6c7f5e6 --- /dev/null +++ b/src/main/java/thread/leetcode1226/DiningPhilosophers.java @@ -0,0 +1,34 @@ +package thread.leetcode1226; + +import java.util.concurrent.Semaphore; + +/** + * 哲学家进餐 + * LeetCode 1226 https://leetcode.cn/problems/the-dining-philosophers/ + * + * @author yangyi 2022年08月11日18:59:39 + */ +public class DiningPhilosophers { + + private Semaphore s = new Semaphore(1); + + public DiningPhilosophers() { + + } + + // call the run() method of any runnable to execute its code + public void wantsToEat(int philosopher, + Runnable pickLeftFork, + Runnable pickRightFork, + Runnable eat, + Runnable putLeftFork, + Runnable putRightFork) throws InterruptedException { + s.acquire(); + pickLeftFork.run(); + pickRightFork.run(); + eat.run(); + putLeftFork.run(); + putRightFork.run(); + s.release(); + } +} From a45996e794a8253b231b08808ee425338677973d Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月11日 21:04:44 +0800 Subject: [PATCH 054/115] =?UTF-8?q?175.=20=E7=BB=84=E5=90=88=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ src/main/java/database/leetcode175/Solution.sql | 6 ++++++ 2 files changed, 10 insertions(+) create mode 100644 src/main/java/database/leetcode175/Solution.sql diff --git a/README.md b/README.md index 8679688..c79feb5 100644 --- a/README.md +++ b/README.md @@ -310,6 +310,10 @@ - [1117.H2O生成](/src/main/java/thread/leetcode1117/H2O.java) - [1195.交替打印字符串](/src/main/java/thread/leetcode1195/FizzBuzz.java) +## SQL + +- [175.组合两个表](/src/main/java/database/leetcode175/Solution.sql) + ## PAT 1. [1001. 害死人不偿命的(3n+1)猜想(15分)](/src/main/java/pat/pat1001/Main.java) diff --git a/src/main/java/database/leetcode175/Solution.sql b/src/main/java/database/leetcode175/Solution.sql new file mode 100644 index 0000000..357de70 --- /dev/null +++ b/src/main/java/database/leetcode175/Solution.sql @@ -0,0 +1,6 @@ +-- 175. 组合两个表 +-- https://leetcode.cn/problems/combine-two-tables/ +-- @author yangyi 2022年08月12日10:58:23 +select Person.FirstName, Person.LastName, Address.City, Address.State +from Person left join Address on +Person.PersonId = Address.PersonId \ No newline at end of file From f9f65734d5cdee6dbc12ea29d80afd488dee9197 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 11:16:31 +0800 Subject: [PATCH 055/115] =?UTF-8?q?176.=20=E7=AC=AC=E4=BA=8C=E9=AB=98?= =?UTF-8?q?=E7=9A=84=E8=96=AA=E6=B0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/database/leetcode176/Solution.sql | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/main/java/database/leetcode176/Solution.sql diff --git a/src/main/java/database/leetcode176/Solution.sql b/src/main/java/database/leetcode176/Solution.sql new file mode 100644 index 0000000..9051455 --- /dev/null +++ b/src/main/java/database/leetcode176/Solution.sql @@ -0,0 +1,4 @@ +-- 176. 第二高的薪水 +-- https://leetcode.cn/problems/second-highest-salary/ +-- @author yangyi 2022年08月12日11:05:46s +select (select distinct salary from Employee order by salary DESC limit 1 offset 1) as SecondHighestSalary \ No newline at end of file From e768a00e32a4a29a71ae415a7191b980957d3a23 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 11:23:17 +0800 Subject: [PATCH 056/115] =?UTF-8?q?177.=20=E7=AC=ACN=E9=AB=98=E7=9A=84?= =?UTF-8?q?=E8=96=AA=E6=B0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/main/java/database/leetcode177/Solution.sql | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/main/java/database/leetcode177/Solution.sql diff --git a/README.md b/README.md index c79feb5..d20a43f 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,8 @@ ## SQL - [175.组合两个表](/src/main/java/database/leetcode175/Solution.sql) +- [176.第二高的薪水](/src/main/java/database/leetcode176/Solution.sql) +- [177.第N高的薪水](/src/main/java/database/leetcode177/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode177/Solution.sql b/src/main/java/database/leetcode177/Solution.sql new file mode 100644 index 0000000..ef94982 --- /dev/null +++ b/src/main/java/database/leetcode177/Solution.sql @@ -0,0 +1,10 @@ +-- 177. 第N高的薪水 +-- https://leetcode.cn/problems/nth-highest-salary/ +-- yangyi 2022年08月12日11:21:59 +CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT +BEGIN + set n = n - 1; + RETURN ( + select (select distinct salary from Employee order by salary DESC limit 1 offset n) + ); +END \ No newline at end of file From f0d2794c15a743271a6a2e52f4f440f30c76bc19 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 11:39:41 +0800 Subject: [PATCH 057/115] =?UTF-8?q?178.=20=E5=88=86=E6=95=B0=E6=8E=92?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode178/Solution.sql | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 src/main/java/database/leetcode178/Solution.sql diff --git a/README.md b/README.md index d20a43f..4fca705 100644 --- a/README.md +++ b/README.md @@ -315,6 +315,7 @@ - [175.组合两个表](/src/main/java/database/leetcode175/Solution.sql) - [176.第二高的薪水](/src/main/java/database/leetcode176/Solution.sql) - [177.第N高的薪水](/src/main/java/database/leetcode177/Solution.sql) +- [178.分数排名](/src/main/java/database/leetcode178/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode178/Solution.sql b/src/main/java/database/leetcode178/Solution.sql new file mode 100644 index 0000000..190437d --- /dev/null +++ b/src/main/java/database/leetcode178/Solution.sql @@ -0,0 +1,4 @@ +-- 178. 分数排名 +-- https://leetcode.cn/problems/rank-scores/ +-- yangyi 2022年08月12日11:38:32 +select A.score, (select count(distinct B.score) from Scores as B where B.score>= A.score) as 'rank' from Scores as A order by A.score DESC \ No newline at end of file From 73dfd0cd755c2ac6bf9cff2ad14c25f8ebe9dff5 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 11:58:40 +0800 Subject: [PATCH 058/115] =?UTF-8?q?180.=20=E8=BF=9E=E7=BB=AD=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E7=9A=84=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode180/Solution.sql | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 src/main/java/database/leetcode180/Solution.sql diff --git a/README.md b/README.md index 4fca705..cfecbf8 100644 --- a/README.md +++ b/README.md @@ -316,6 +316,7 @@ - [176.第二高的薪水](/src/main/java/database/leetcode176/Solution.sql) - [177.第N高的薪水](/src/main/java/database/leetcode177/Solution.sql) - [178.分数排名](/src/main/java/database/leetcode178/Solution.sql) +- [180.连续出现的数字](/src/main/java/database/leetcode180/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode180/Solution.sql b/src/main/java/database/leetcode180/Solution.sql new file mode 100644 index 0000000..a2e3e47 --- /dev/null +++ b/src/main/java/database/leetcode180/Solution.sql @@ -0,0 +1,4 @@ +-- 180. 连续出现的数字 +-- https://leetcode.cn/problems/consecutive-numbers/ +-- yangyi 2022年08月12日11:57:57 +select distinct a.num as ConsecutiveNums from Logs a, Logs as b, Logs as c where a.id = b.id - 1 and b.id = c.id - 1 and a.num = b.num and b.num = c.num \ No newline at end of file From dc72483e668ab2306ec9be4d3fdf0879bdfa1ac4 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 12:16:33 +0800 Subject: [PATCH 059/115] =?UTF-8?q?181.=20=E8=B6=85=E8=BF=87=E7=BB=8F?= =?UTF-8?q?=E7=90=86=E6=94=B6=E5=85=A5=E7=9A=84=E5=91=98=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode181/Solution.sql | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 src/main/java/database/leetcode181/Solution.sql diff --git a/README.md b/README.md index cfecbf8..8edd0b1 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,7 @@ - [177.第N高的薪水](/src/main/java/database/leetcode177/Solution.sql) - [178.分数排名](/src/main/java/database/leetcode178/Solution.sql) - [180.连续出现的数字](/src/main/java/database/leetcode180/Solution.sql) +- [181.超过经理收入的员工](/src/main/java/database/leetcode181/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode181/Solution.sql b/src/main/java/database/leetcode181/Solution.sql new file mode 100644 index 0000000..3606294 --- /dev/null +++ b/src/main/java/database/leetcode181/Solution.sql @@ -0,0 +1,4 @@ +-- 181. 超过经理收入的员工 +-- https://leetcode.cn/problems/employees-earning-more-than-their-managers/ +-- yangyi 2022年08月12日12:15:42 +select a.name as 'Employee' from Employee as a, Employee as b where a.managerId = b.id and a.salary> b.salary \ No newline at end of file From f163a6d6423dd189c11f88766028224ff0438b34 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 14:24:44 +0800 Subject: [PATCH 060/115] =?UTF-8?q?182.=20=E6=9F=A5=E6=89=BE=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E7=94=B5=E5=AD=90=E9=82=AE=E7=AE=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode182/Solution.sql | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 src/main/java/database/leetcode182/Solution.sql diff --git a/README.md b/README.md index 8edd0b1..97b7d54 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,7 @@ - [178.分数排名](/src/main/java/database/leetcode178/Solution.sql) - [180.连续出现的数字](/src/main/java/database/leetcode180/Solution.sql) - [181.超过经理收入的员工](/src/main/java/database/leetcode181/Solution.sql) +- [182.查找重复的电子邮箱](/src/main/java/database/leetcode182/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode182/Solution.sql b/src/main/java/database/leetcode182/Solution.sql new file mode 100644 index 0000000..0c10144 --- /dev/null +++ b/src/main/java/database/leetcode182/Solution.sql @@ -0,0 +1,4 @@ +-- 182. 查找重复的电子邮箱 +-- https://leetcode.cn/problems/duplicate-emails/ +-- yangyi 2022年08月12日14:18:41 +select Email from Person group by Email having count(Email)> 1 \ No newline at end of file From 69d4ef8d3daf1c1b24d8e6d5391fcdf6b0fd7b2d Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 14:41:05 +0800 Subject: [PATCH 061/115] =?UTF-8?q?183.=20=E4=BB=8E=E4=B8=8D=E8=AE=A2?= =?UTF-8?q?=E8=B4=AD=E7=9A=84=E5=AE=A2=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode183/Solution.sql | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 src/main/java/database/leetcode183/Solution.sql diff --git a/README.md b/README.md index 97b7d54..e8a2fe5 100644 --- a/README.md +++ b/README.md @@ -319,6 +319,7 @@ - [180.连续出现的数字](/src/main/java/database/leetcode180/Solution.sql) - [181.超过经理收入的员工](/src/main/java/database/leetcode181/Solution.sql) - [182.查找重复的电子邮箱](/src/main/java/database/leetcode182/Solution.sql) +- [183.从不订购的客户](/src/main/java/database/leetcode183/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode183/Solution.sql b/src/main/java/database/leetcode183/Solution.sql new file mode 100644 index 0000000..7984454 --- /dev/null +++ b/src/main/java/database/leetcode183/Solution.sql @@ -0,0 +1,4 @@ +-- 183. 从不订购的客户 +-- https://leetcode.cn/problems/customers-who-never-order/ +-- yangyi 2022年08月12日14:39:46 +select Customers.name as 'Customers' from Customers left join Orders on Customers.id = Orders.CustomerId where Orders.CustomerId is null \ No newline at end of file From 95fc638f59886d0667f093db0ce8b536b585d166 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 18:17:02 +0800 Subject: [PATCH 062/115] =?UTF-8?q?196.=20=E5=88=A0=E9=99=A4=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E7=9A=84=E7=94=B5=E5=AD=90=E9=82=AE=E7=AE=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode196/Solution.sql | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/database/leetcode196/Solution.sql diff --git a/README.md b/README.md index e8a2fe5..5ef48c6 100644 --- a/README.md +++ b/README.md @@ -320,6 +320,7 @@ - [181.超过经理收入的员工](/src/main/java/database/leetcode181/Solution.sql) - [182.查找重复的电子邮箱](/src/main/java/database/leetcode182/Solution.sql) - [183.从不订购的客户](/src/main/java/database/leetcode183/Solution.sql) +- [196.删除重复的电子邮箱](/src/main/java/database/leetcode196/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode196/Solution.sql b/src/main/java/database/leetcode196/Solution.sql new file mode 100644 index 0000000..5fc49c9 --- /dev/null +++ b/src/main/java/database/leetcode196/Solution.sql @@ -0,0 +1,7 @@ +-- 196. 删除重复的电子邮箱 +-- https://leetcode.cn/problems/delete-duplicate-emails/ +-- yangyi 2022年08月12日18:16:40 +--# Please write a DELETE statement and DO NOT write a SELECT statement. +delete p1 from Person as p1, Person as p2 +where p1.email = p2.email +and p1.id> p2.id \ No newline at end of file From 83bca82f578bed3d623a2e920b2a8e572bd3e270 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 18:31:02 +0800 Subject: [PATCH 063/115] =?UTF-8?q?197.=20=E4=B8=8A=E5=8D=87=E7=9A=84?= =?UTF-8?q?=E6=B8=A9=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode197/Solution.sql | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 src/main/java/database/leetcode197/Solution.sql diff --git a/README.md b/README.md index 5ef48c6..d7a76ae 100644 --- a/README.md +++ b/README.md @@ -321,6 +321,7 @@ - [182.查找重复的电子邮箱](/src/main/java/database/leetcode182/Solution.sql) - [183.从不订购的客户](/src/main/java/database/leetcode183/Solution.sql) - [196.删除重复的电子邮箱](/src/main/java/database/leetcode196/Solution.sql) +- [197.上升的温度](/src/main/java/database/leetcode197/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode197/Solution.sql b/src/main/java/database/leetcode197/Solution.sql new file mode 100644 index 0000000..1ca8c3a --- /dev/null +++ b/src/main/java/database/leetcode197/Solution.sql @@ -0,0 +1,6 @@ +-- 197. 上升的温度 +-- https://leetcode.cn/problems/rising-temperature/ +-- yangyi 2022年08月12日18:30:44 +select w1.id from Weather as w1, Weather as w2 +where datediff(w1.recordDate, w2.recordDate) = 1 +and w1.temperature> w2.temperature \ No newline at end of file From 7444fba5a9367fd1bb426ef440770f15a0b45bca Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月12日 18:40:17 +0800 Subject: [PATCH 064/115] =?UTF-8?q?511.=20=E6=B8=B8=E6=88=8F=E7=8E=A9?= =?UTF-8?q?=E6=B3=95=E5=88=86=E6=9E=90I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/main/java/database/leetcode511/Solution.sql | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 src/main/java/database/leetcode511/Solution.sql diff --git a/README.md b/README.md index d7a76ae..65ad759 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,8 @@ - [183.从不订购的客户](/src/main/java/database/leetcode183/Solution.sql) - [196.删除重复的电子邮箱](/src/main/java/database/leetcode196/Solution.sql) - [197.上升的温度](/src/main/java/database/leetcode197/Solution.sql) +- [197.上升的温度](/src/main/java/database/leetcode197/Solution.sql) +- [511.游戏玩法分析I](/src/main/java/database/leetcode511/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode511/Solution.sql b/src/main/java/database/leetcode511/Solution.sql new file mode 100644 index 0000000..ee8d806 --- /dev/null +++ b/src/main/java/database/leetcode511/Solution.sql @@ -0,0 +1,5 @@ +-- 511. 游戏玩法分析 I +-- https://leetcode.cn/problems/game-play-analysis-i/ +-- yangyi 2022年08月12日18:39:29 +select player_id, min(event_date) as first_login from Activity +group by player_id \ No newline at end of file From 68df6011fed53a8be8b361b3f5493f96cdf6f9d8 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 11:28:51 +0800 Subject: [PATCH 065/115] =?UTF-8?q?584.=20=E5=AF=BB=E6=89=BE=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=8E=A8=E8=8D=90=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/database/leetcode584/Solution.sql | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 src/main/java/database/leetcode584/Solution.sql diff --git a/README.md b/README.md index 65ad759..3667b58 100644 --- a/README.md +++ b/README.md @@ -322,8 +322,8 @@ - [183.从不订购的客户](/src/main/java/database/leetcode183/Solution.sql) - [196.删除重复的电子邮箱](/src/main/java/database/leetcode196/Solution.sql) - [197.上升的温度](/src/main/java/database/leetcode197/Solution.sql) -- [197.上升的温度](/src/main/java/database/leetcode197/Solution.sql) - [511.游戏玩法分析I](/src/main/java/database/leetcode511/Solution.sql) +- [584.寻找用户推荐人](/src/main/java/database/leetcode584/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode584/Solution.sql b/src/main/java/database/leetcode584/Solution.sql new file mode 100644 index 0000000..088ac74 --- /dev/null +++ b/src/main/java/database/leetcode584/Solution.sql @@ -0,0 +1,6 @@ +-- 584. 寻找用户推荐人 +-- https://leetcode.cn/problems/find-customer-referee/ +-- yangyi 2022年08月15日11:28:30 +select name from customer +where referee_id 2 +or referee_id is null \ No newline at end of file From 0a0f5f46758fe8daabaaa276e92940181c42edff Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 11:33:56 +0800 Subject: [PATCH 066/115] =?UTF-8?q?595.=20=E5=A4=A7=E7=9A=84=E5=9B=BD?= =?UTF-8?q?=E5=AE=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode595/Solution.sql | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 src/main/java/database/leetcode595/Solution.sql diff --git a/README.md b/README.md index 3667b58..904ca1c 100644 --- a/README.md +++ b/README.md @@ -324,6 +324,7 @@ - [197.上升的温度](/src/main/java/database/leetcode197/Solution.sql) - [511.游戏玩法分析I](/src/main/java/database/leetcode511/Solution.sql) - [584.寻找用户推荐人](/src/main/java/database/leetcode584/Solution.sql) +- [595.大的国家](/src/main/java/database/leetcode595/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode595/Solution.sql b/src/main/java/database/leetcode595/Solution.sql new file mode 100644 index 0000000..aa418ee --- /dev/null +++ b/src/main/java/database/leetcode595/Solution.sql @@ -0,0 +1,6 @@ +-- 595. 大的国家 +-- https://leetcode.cn/problems/big-countries/ +-- yangyi 2022年08月15日11:33:36 +select name, population, area from World +where area>= 3000000 +or population>= 25000000 \ No newline at end of file From 30ee3c16cd021708e07dfd7abc0d47059cc222d2 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 11:38:05 +0800 Subject: [PATCH 067/115] =?UTF-8?q?1757.=20=E5=8F=AF=E5=9B=9E=E6=94=B6?= =?UTF-8?q?=E4=B8=94=E4=BD=8E=E8=84=82=E7=9A=84=E4=BA=A7=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1757/Solution.sql | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 src/main/java/database/leetcode1757/Solution.sql diff --git a/README.md b/README.md index 904ca1c..e2eaee0 100644 --- a/README.md +++ b/README.md @@ -325,6 +325,7 @@ - [511.游戏玩法分析I](/src/main/java/database/leetcode511/Solution.sql) - [584.寻找用户推荐人](/src/main/java/database/leetcode584/Solution.sql) - [595.大的国家](/src/main/java/database/leetcode595/Solution.sql) +- [1757.可回收且低脂的产品](/src/main/java/database/leetcode1757/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1757/Solution.sql b/src/main/java/database/leetcode1757/Solution.sql new file mode 100644 index 0000000..5436906 --- /dev/null +++ b/src/main/java/database/leetcode1757/Solution.sql @@ -0,0 +1,6 @@ +-- 1757. 可回收且低脂的产品 +-- https://leetcode.cn/problems/recyclable-and-low-fat-products/ +-- yangyi 2022年08月15日11:37:47 +select product_id from Products +where low_fats = 'Y' +and recyclable = 'Y' \ No newline at end of file From 437027ab8f206a47156a8420c3f313279f65a15d Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 16:10:13 +0800 Subject: [PATCH 068/115] =?UTF-8?q?1873.=20=E8=AE=A1=E7=AE=97=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E5=A5=96=E9=87=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 ++ src/main/java/database/leetcode1873/Solution.sql | 11 +++++++++++ 2 files changed, 13 insertions(+) create mode 100644 src/main/java/database/leetcode1873/Solution.sql diff --git a/README.md b/README.md index e2eaee0..e7f9254 100644 --- a/README.md +++ b/README.md @@ -309,6 +309,7 @@ - [1116.打印零与奇偶数](/src/main/java/thread/leetcode1116/ZeroEvenOdd.java) - [1117.H2O生成](/src/main/java/thread/leetcode1117/H2O.java) - [1195.交替打印字符串](/src/main/java/thread/leetcode1195/FizzBuzz.java) +- [1226.哲学家进餐](/src/main/java/thread/leetcode1226/DiningPhilosophers.java) ## SQL @@ -326,6 +327,7 @@ - [584.寻找用户推荐人](/src/main/java/database/leetcode584/Solution.sql) - [595.大的国家](/src/main/java/database/leetcode595/Solution.sql) - [1757.可回收且低脂的产品](/src/main/java/database/leetcode1757/Solution.sql) +- [1873.计算特殊奖金](/src/main/java/database/leetcode1873/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1873/Solution.sql b/src/main/java/database/leetcode1873/Solution.sql new file mode 100644 index 0000000..8cb6581 --- /dev/null +++ b/src/main/java/database/leetcode1873/Solution.sql @@ -0,0 +1,11 @@ +-- 1873. 计算特殊奖金 +-- https://leetcode.cn/problems/calculate-special-bonus/ +-- yangyi 2022年08月15日16:08:03 +select +employee_id, +case +when mod(employee_id, 2) 0 and left(name, 1) 'M' then salary +else 0 +end as bonus +from Employees +order by employee_id \ No newline at end of file From 8b7d808b583b4120bae067c6027b3a1bffac80b7 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 16:30:05 +0800 Subject: [PATCH 069/115] =?UTF-8?q?627.=20=E5=8F=98=E6=9B=B4=E6=80=A7?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode627/Solution.sql | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 src/main/java/database/leetcode627/Solution.sql diff --git a/README.md b/README.md index e7f9254..1a3fb18 100644 --- a/README.md +++ b/README.md @@ -328,6 +328,7 @@ - [595.大的国家](/src/main/java/database/leetcode595/Solution.sql) - [1757.可回收且低脂的产品](/src/main/java/database/leetcode1757/Solution.sql) - [1873.计算特殊奖金](/src/main/java/database/leetcode1873/Solution.sql) +- [627.变更性别](/src/main/java/database/leetcode627/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode627/Solution.sql b/src/main/java/database/leetcode627/Solution.sql new file mode 100644 index 0000000..a224188 --- /dev/null +++ b/src/main/java/database/leetcode627/Solution.sql @@ -0,0 +1,9 @@ +-- 627. 变更性别 +-- https://leetcode.cn/problems/swap-salary/ +-- yangyi 2022年08月15日16:29:47 +update Salary +set sex = +case +when sex = 'm' then 'f' +else 'm' +end \ No newline at end of file From 4718722435171b3f657d243e9bbd447ac97beb26 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 16:55:00 +0800 Subject: [PATCH 070/115] =?UTF-8?q?586.=20=E8=AE=A2=E5=8D=95=E6=9C=80?= =?UTF-8?q?=E5=A4=9A=E7=9A=84=E5=AE=A2=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode586/Solution.sql | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 src/main/java/database/leetcode586/Solution.sql diff --git a/README.md b/README.md index 1a3fb18..0321280 100644 --- a/README.md +++ b/README.md @@ -329,6 +329,7 @@ - [1757.可回收且低脂的产品](/src/main/java/database/leetcode1757/Solution.sql) - [1873.计算特殊奖金](/src/main/java/database/leetcode1873/Solution.sql) - [627.变更性别](/src/main/java/database/leetcode627/Solution.sql) +- [586.订单最多的客户](/src/main/java/database/leetcode586/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode586/Solution.sql b/src/main/java/database/leetcode586/Solution.sql new file mode 100644 index 0000000..e41e60c --- /dev/null +++ b/src/main/java/database/leetcode586/Solution.sql @@ -0,0 +1,8 @@ +-- 586. 订单最多的客户 +-- https://leetcode.cn/problems/customer-placing-the-largest-number-of-orders/ +-- yangyi 2022年08月15日16:54:27 +select customer_number +from Orders +group by customer_number +order by count(customer_number) DESC +limit 1 \ No newline at end of file From 49fc88cb72bd48dd99ef997e7ea1161446000ed1 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 17:08:56 +0800 Subject: [PATCH 071/115] =?UTF-8?q?620.=20=E6=9C=89=E8=B6=A3=E7=9A=84?= =?UTF-8?q?=E7=94=B5=E5=BD=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode620/Solution.sql | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 src/main/java/database/leetcode620/Solution.sql diff --git a/README.md b/README.md index 0321280..d8efc21 100644 --- a/README.md +++ b/README.md @@ -330,6 +330,7 @@ - [1873.计算特殊奖金](/src/main/java/database/leetcode1873/Solution.sql) - [627.变更性别](/src/main/java/database/leetcode627/Solution.sql) - [586.订单最多的客户](/src/main/java/database/leetcode586/Solution.sql) +- [620.有趣的电影](/src/main/java/database/leetcode620/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode620/Solution.sql b/src/main/java/database/leetcode620/Solution.sql new file mode 100644 index 0000000..9f85a5c --- /dev/null +++ b/src/main/java/database/leetcode620/Solution.sql @@ -0,0 +1,8 @@ +-- 620. 有趣的电影 +-- https://leetcode.cn/problems/not-boring-movies/ +-- yangyi 2022年08月15日17:08:21 +select * +from cinema +where description 'boring' +and mod(id, 2) 0 +order by rating DESC \ No newline at end of file From 6228b026f40cde1205e30093f7196880f20622aa Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 17:30:30 +0800 Subject: [PATCH 072/115] =?UTF-8?q?596.=20=E8=B6=85=E8=BF=875=E5=90=8D?= =?UTF-8?q?=E5=AD=A6=E7=94=9F=E7=9A=84=E8=AF=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode596/Solution.sql | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/database/leetcode596/Solution.sql diff --git a/README.md b/README.md index d8efc21..92b7a32 100644 --- a/README.md +++ b/README.md @@ -331,6 +331,7 @@ - [627.变更性别](/src/main/java/database/leetcode627/Solution.sql) - [586.订单最多的客户](/src/main/java/database/leetcode586/Solution.sql) - [620.有趣的电影](/src/main/java/database/leetcode620/Solution.sql) +- [596.超过5名学生的课](/src/main/java/database/leetcode596/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode596/Solution.sql b/src/main/java/database/leetcode596/Solution.sql new file mode 100644 index 0000000..5ea08b8 --- /dev/null +++ b/src/main/java/database/leetcode596/Solution.sql @@ -0,0 +1,7 @@ +-- 596. 超过5名学生的课 +-- https://leetcode.cn/problems/classes-more-than-5-students/ +-- yangyi 2022年08月15日17:30:01 +select class +from Courses +group by class +having count(class)>= 5 \ No newline at end of file From 4367482c71f8c8544044b8be4e1acdf85bafbf5c Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月15日 23:31:35 +0800 Subject: [PATCH 073/115] =?UTF-8?q?34.=20=E5=9C=A8=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E4=B8=AD=E6=9F=A5=E6=89=BE=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E7=9A=84=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=92=8C=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E4=B8=AA=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/ds/binary/leetcode34/Solution.java | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/ds/binary/leetcode34/Solution.java diff --git a/README.md b/README.md index 101c7bf..96d7658 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ - [查找最后一个小于等于给定值的元素](/src/main/java/ds/binary/BSEndLess.java) - [x的平方根](/src/main/java/ds/binary/MySqrt.java) - [35.搜索插入位置](/src/main/java/ds/binary/leetcode35/Solution.java) + - [34.在排序数组中查找元素的第一个和最后一个位置](/src/main/java/ds/binary/leetcode34/Solution.java) 5. 双指针 - [141.环形链表](/src/main/java/ds/pointer/leetcode141/Solution.java) diff --git a/src/main/java/ds/binary/leetcode34/Solution.java b/src/main/java/ds/binary/leetcode34/Solution.java new file mode 100644 index 0000000..a72bfd0 --- /dev/null +++ b/src/main/java/ds/binary/leetcode34/Solution.java @@ -0,0 +1,51 @@ +package ds.binary.leetcode34; + +import java.util.Arrays; + +/** + * 在排序数组中查找元素的第一个和最后一个位置 + * LeetCode 34. https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/ + * + * @author yangyi 2022年07月12日18:50:04 + */ +public class Solution { + + public int[] searchRange(int[] nums, int target) { + if (nums == null || nums.length == 0) { + return new int[]{-1, -1}; + } + int left = 0, right = nums.length - 1; + while (left + 1 < right) { + int mid = left + (right - left) / 2; + if (nums[mid] == target) { + if (nums[left] == nums[right]) { + return new int[]{left, right}; + } else if (nums[left] != target) { + left++; + } else if (nums[right] != target) { + right--; + } + } else if (nums[mid] < target) { + left = mid; + } else { + right = mid; + } + } + if (nums[left] == target && nums[right] == target) { + return new int[]{left, right}; + } + if (nums[right] == target) { + return new int[]{right, right}; + } + if (nums[left] == target) { + return new int[]{left, left}; + } + return new int[]{-1, -1}; + } + + public static void main(String[] args) { + System.out.println(Arrays.toString(new Solution().searchRange(new int[]{5, 7, 7, 8, 8, 10}, 8))); + System.out.println(Arrays.toString(new Solution().searchRange(new int[]{5, 7, 7, 8, 8, 10}, 6))); + System.out.println(Arrays.toString(new Solution().searchRange(new int[]{}, 0))); + } +} From 9d484e1edf94b2f59096659cfd78963a741a79bc Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 00:35:40 +0800 Subject: [PATCH 074/115] =?UTF-8?q?1114.=20=E6=8C=89=E5=BA=8F=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/thread/leetcode1114/Foo2.java | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/main/java/thread/leetcode1114/Foo2.java diff --git a/src/main/java/thread/leetcode1114/Foo2.java b/src/main/java/thread/leetcode1114/Foo2.java new file mode 100644 index 0000000..8ca9b82 --- /dev/null +++ b/src/main/java/thread/leetcode1114/Foo2.java @@ -0,0 +1,63 @@ +package thread.leetcode1114; + +import java.util.concurrent.Semaphore; + +/** + * 按序打印 + * LeetCode 1114 https://leetcode-cn.com/problems/print-in-order/ + * + * @author yangyi 2022年08月11日15:52:55 + */ +public class Foo2 { + + private Semaphore firstFinish = new Semaphore(0); + private Semaphore secondFinish = new Semaphore(0); + + public Foo2() { + + } + + public void first(Runnable printFirst) throws InterruptedException { + printFirst.run(); + firstFinish.release(); + } + + public void second(Runnable printSecond) throws InterruptedException { + firstFinish.acquire(); + printSecond.run(); + secondFinish.release(); + } + + public void third(Runnable printThird) throws InterruptedException { + secondFinish.acquire(); + printThird.run(); + } + + public static void main(String[] args) throws InterruptedException { + Foo2 foo2 = new Foo2(); + Runnable printFirst = () -> System.out.println("first"); + Runnable printSecond = () -> System.out.println("second"); + Runnable printThird = () -> System.out.println("third"); + new Thread(() -> { + try { + foo2.third(printThird); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + new Thread(() -> { + try { + foo2.first(printFirst); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + new Thread(() -> { + try { + foo2.second(printSecond); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + } +} From 4ddcae5351ee7b327c16035f65943698451da0ba Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 11:09:53 +0800 Subject: [PATCH 075/115] =?UTF-8?q?1667.=20=E4=BF=AE=E5=A4=8D=E8=A1=A8?= =?UTF-8?q?=E4=B8=AD=E7=9A=84=E5=90=8D=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1667/Solution.sql | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/database/leetcode1667/Solution.sql diff --git a/README.md b/README.md index 92b7a32..e829748 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,7 @@ - [586.订单最多的客户](/src/main/java/database/leetcode586/Solution.sql) - [620.有趣的电影](/src/main/java/database/leetcode620/Solution.sql) - [596.超过5名学生的课](/src/main/java/database/leetcode596/Solution.sql) +- [1667.修复表中的名字](/src/main/java/database/leetcode1667/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1667/Solution.sql b/src/main/java/database/leetcode1667/Solution.sql new file mode 100644 index 0000000..ee2597b --- /dev/null +++ b/src/main/java/database/leetcode1667/Solution.sql @@ -0,0 +1,7 @@ +-- 1667. 修复表中的名字 +-- https://leetcode.cn/problems/fix-names-in-a-table/ +-- yangyi 2022年08月16日11:09:30 +select user_id, +concat(upper(left(name, 1)), lower(substr(name, 2))) as name +from Users +order by user_id \ No newline at end of file From cafd615216590dfb62843f05a059b710977555b0 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 11:40:28 +0800 Subject: [PATCH 076/115] =?UTF-8?q?1484.=20=E6=8C=89=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E5=88=86=E7=BB=84=E9=94=80=E5=94=AE=E4=BA=A7=E5=93=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1484/Solution.sql | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 src/main/java/database/leetcode1484/Solution.sql diff --git a/README.md b/README.md index e829748..83253ed 100644 --- a/README.md +++ b/README.md @@ -333,6 +333,7 @@ - [620.有趣的电影](/src/main/java/database/leetcode620/Solution.sql) - [596.超过5名学生的课](/src/main/java/database/leetcode596/Solution.sql) - [1667.修复表中的名字](/src/main/java/database/leetcode1667/Solution.sql) +- [1484.按日期分组销售产品](/src/main/java/database/leetcode1484/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1484/Solution.sql b/src/main/java/database/leetcode1484/Solution.sql new file mode 100644 index 0000000..7dcd1c0 --- /dev/null +++ b/src/main/java/database/leetcode1484/Solution.sql @@ -0,0 +1,6 @@ +-- 1484. 按日期分组销售产品 +-- https://leetcode.cn/problems/group-sold-products-by-the-date/ +-- yanyi 2022年08月16日11:38:09 +select sell_date, count(distinct product) as num_sold, group_concat(distinct product order by product) as products +from Activities +group by sell_date \ No newline at end of file From 480ebd1f88c69e3e00451cd51563b28d43bc89f3 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 12:02:41 +0800 Subject: [PATCH 077/115] =?UTF-8?q?1527.=20=E6=82=A3=E6=9F=90=E7=A7=8D?= =?UTF-8?q?=E7=96=BE=E7=97=85=E7=9A=84=E6=82=A3=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1527/Solution.sql | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 src/main/java/database/leetcode1527/Solution.sql diff --git a/README.md b/README.md index 83253ed..263c244 100644 --- a/README.md +++ b/README.md @@ -334,6 +334,7 @@ - [596.超过5名学生的课](/src/main/java/database/leetcode596/Solution.sql) - [1667.修复表中的名字](/src/main/java/database/leetcode1667/Solution.sql) - [1484.按日期分组销售产品](/src/main/java/database/leetcode1484/Solution.sql) +- [1527.患某种疾病的患者](/src/main/java/database/leetcode1527/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1527/Solution.sql b/src/main/java/database/leetcode1527/Solution.sql new file mode 100644 index 0000000..30eaa79 --- /dev/null +++ b/src/main/java/database/leetcode1527/Solution.sql @@ -0,0 +1,6 @@ +-- 1527. 患某种疾病的患者 +-- https://leetcode.cn/problems/patients-with-a-condition/ +-- yangyi 2022年08月16日12:02:21 +select * +from Patients +where conditions like 'DIAB1%' or conditions like '% DIAB1%' \ No newline at end of file From f4ea33c3fdab825004fc56cff7c7553f8bd5fb64 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 14:46:47 +0800 Subject: [PATCH 078/115] =?UTF-8?q?607.=20=E9=94=80=E5=94=AE=E5=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode607/Solution.sql | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 src/main/java/database/leetcode607/Solution.sql diff --git a/README.md b/README.md index 263c244..0140990 100644 --- a/README.md +++ b/README.md @@ -335,6 +335,7 @@ - [1667.修复表中的名字](/src/main/java/database/leetcode1667/Solution.sql) - [1484.按日期分组销售产品](/src/main/java/database/leetcode1484/Solution.sql) - [1527.患某种疾病的患者](/src/main/java/database/leetcode1527/Solution.sql) +- [607.销售员](/src/main/java/database/leetcode607/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode607/Solution.sql b/src/main/java/database/leetcode607/Solution.sql new file mode 100644 index 0000000..cd6e7ce --- /dev/null +++ b/src/main/java/database/leetcode607/Solution.sql @@ -0,0 +1,8 @@ +-- 607. 销售员 +-- https://leetcode.cn/problems/sales-person/ +-- yangyi 2022年08月16日14:46:18 +select SalesPerson.name +from SalesPerson +where sales_id not in (select sales_id +from Orders as o +left join Company as c on o.com_id = c.com_id where c.name = 'RED') \ No newline at end of file From 6206923265f9d39ce7bfc53a0721270a11945a45 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 15:04:35 +0800 Subject: [PATCH 079/115] =?UTF-8?q?608.=20=E6=A0=91=E8=8A=82=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode608/Solution.sql | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/main/java/database/leetcode608/Solution.sql diff --git a/README.md b/README.md index 0140990..9bfcbeb 100644 --- a/README.md +++ b/README.md @@ -336,6 +336,7 @@ - [1484.按日期分组销售产品](/src/main/java/database/leetcode1484/Solution.sql) - [1527.患某种疾病的患者](/src/main/java/database/leetcode1527/Solution.sql) - [607.销售员](/src/main/java/database/leetcode607/Solution.sql) +- [608.树节点](/src/main/java/database/leetcode608/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode608/Solution.sql b/src/main/java/database/leetcode608/Solution.sql new file mode 100644 index 0000000..31504fd --- /dev/null +++ b/src/main/java/database/leetcode608/Solution.sql @@ -0,0 +1,11 @@ +-- 608. 树节点 +-- https://leetcode.cn/problems/tree-node/ +-- yangyi 2022年08月16日15:04:09 +select id, +case +when id = (select id from tree where p_id is null) then 'Root' +when id in (select p_id from tree) then 'Inner' +else 'Leaf' +end as Type +from tree +order by id \ No newline at end of file From ba9120e16e0c41186d9c6f02d347f5568941e1fb Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 15:27:28 +0800 Subject: [PATCH 080/115] =?UTF-8?q?1084.=20=E9=94=80=E5=94=AE=E5=88=86?= =?UTF-8?q?=E6=9E=90III?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1084/Solution.sql | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 src/main/java/database/leetcode1084/Solution.sql diff --git a/README.md b/README.md index 9bfcbeb..f819818 100644 --- a/README.md +++ b/README.md @@ -337,6 +337,7 @@ - [1527.患某种疾病的患者](/src/main/java/database/leetcode1527/Solution.sql) - [607.销售员](/src/main/java/database/leetcode607/Solution.sql) - [608.树节点](/src/main/java/database/leetcode608/Solution.sql) +- [1084.销售分析III](/src/main/java/database/leetcode1084/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1084/Solution.sql b/src/main/java/database/leetcode1084/Solution.sql new file mode 100644 index 0000000..8d81ad7 --- /dev/null +++ b/src/main/java/database/leetcode1084/Solution.sql @@ -0,0 +1,8 @@ +-- 1084. 销售分析III +-- https://leetcode.cn/problems/sales-analysis-iii/ +-- yangyi 2022年08月16日15:27:00 +select p.product_id, p.product_name +from Product as p +left join Sales as s on p.product_id = s.product_id +group by s.product_id +having min(sale_date)>= '2019-01-01' and max(sale_date) <= '2019-03-31' \ No newline at end of file From 6850f2041468f41f81c266217178853597726bfb Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 16:56:29 +0800 Subject: [PATCH 081/115] =?UTF-8?q?626.=20=E6=8D=A2=E5=BA=A7=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode626/Solution.sql | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/main/java/database/leetcode626/Solution.sql diff --git a/README.md b/README.md index f819818..e1766e6 100644 --- a/README.md +++ b/README.md @@ -338,6 +338,7 @@ - [607.销售员](/src/main/java/database/leetcode607/Solution.sql) - [608.树节点](/src/main/java/database/leetcode608/Solution.sql) - [1084.销售分析III](/src/main/java/database/leetcode1084/Solution.sql) +- [626.换座位](/src/main/java/database/leetcode626/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode626/Solution.sql b/src/main/java/database/leetcode626/Solution.sql new file mode 100644 index 0000000..c34f74b --- /dev/null +++ b/src/main/java/database/leetcode626/Solution.sql @@ -0,0 +1,13 @@ +-- 626. 换座位 +-- https://leetcode.cn/problems/exchange-seats/ +-- yangyi 2022年08月16日15:36:25 +# Write your MySQL query statement below +select +case +when mod(id, 2) 0 and counts id then id + 1 +when mod(id, 2) 0 and counts = id then id +when mod(id, 2) = 0 then id - 1 +end as id, student +from Seat, +(select count(id) as counts from Seat) as Seat_counts +order by id \ No newline at end of file From f374138aade44f91582b7cc40e0b30c1ab36e45b Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 17:06:33 +0800 Subject: [PATCH 082/115] =?UTF-8?q?1050.=20=E5=90=88=E4=BD=9C=E8=BF=87?= =?UTF-8?q?=E8=87=B3=E5=B0=91=E4=B8=89=E6=AC=A1=E7=9A=84=E6=BC=94=E5=91=98?= =?UTF-8?q?=E5=92=8C=E5=AF=BC=E6=BC=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1050/Solution.sql | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/database/leetcode1050/Solution.sql diff --git a/README.md b/README.md index e1766e6..29b3b2a 100644 --- a/README.md +++ b/README.md @@ -339,6 +339,7 @@ - [608.树节点](/src/main/java/database/leetcode608/Solution.sql) - [1084.销售分析III](/src/main/java/database/leetcode1084/Solution.sql) - [626.换座位](/src/main/java/database/leetcode626/Solution.sql) +- [1050.合作过至少三次的演员和导演](/src/main/java/database/leetcode1050/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1050/Solution.sql b/src/main/java/database/leetcode1050/Solution.sql new file mode 100644 index 0000000..0dfa27f --- /dev/null +++ b/src/main/java/database/leetcode1050/Solution.sql @@ -0,0 +1,7 @@ +-- 1050. 合作过至少三次的演员和导演 +-- https://leetcode.cn/problems/actors-and-directors-who-cooperated-at-least-three-times/ +-- yangyi 2022年08月16日17:06:19 +select actor_id, director_id +from ActorDirector +group by actor_id, director_id +having count(timestamp)>= 3 \ No newline at end of file From 07a888b9abb90703b30f8b17f7171c3081dd14bf Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 17:20:51 +0800 Subject: [PATCH 083/115] =?UTF-8?q?1148.=20=E6=96=87=E7=AB=A0=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=20I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1148/Solution.sql | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/database/leetcode1148/Solution.sql diff --git a/README.md b/README.md index 29b3b2a..d2dfe5e 100644 --- a/README.md +++ b/README.md @@ -340,6 +340,7 @@ - [1084.销售分析III](/src/main/java/database/leetcode1084/Solution.sql) - [626.换座位](/src/main/java/database/leetcode626/Solution.sql) - [1050.合作过至少三次的演员和导演](/src/main/java/database/leetcode1050/Solution.sql) +- [1148.文章浏览I](/src/main/java/database/leetcode1148/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1148/Solution.sql b/src/main/java/database/leetcode1148/Solution.sql new file mode 100644 index 0000000..0a6a7d1 --- /dev/null +++ b/src/main/java/database/leetcode1148/Solution.sql @@ -0,0 +1,7 @@ +-- 1148. 文章浏览 I +-- https://leetcode.cn/problems/article-views-i/ +-- yangyi 2022年08月16日17:20:22 +select distinct author_id as id +from Views +where author_id = viewer_id +order by author_id \ No newline at end of file From fcea77a7cc00624f19f5cf5a9f2a10035880af3e Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 17:46:58 +0800 Subject: [PATCH 084/115] =?UTF-8?q?1179.=20=E9=87=8D=E6=96=B0=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E9=83=A8=E9=97=A8=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + .../java/database/leetcode1179/Solution.sql | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 src/main/java/database/leetcode1179/Solution.sql diff --git a/README.md b/README.md index d2dfe5e..872feca 100644 --- a/README.md +++ b/README.md @@ -341,6 +341,7 @@ - [626.换座位](/src/main/java/database/leetcode626/Solution.sql) - [1050.合作过至少三次的演员和导演](/src/main/java/database/leetcode1050/Solution.sql) - [1148.文章浏览I](/src/main/java/database/leetcode1148/Solution.sql) +- [1179.重新格式化部门表](/src/main/java/database/leetcode1179/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1179/Solution.sql b/src/main/java/database/leetcode1179/Solution.sql new file mode 100644 index 0000000..ad7b4f3 --- /dev/null +++ b/src/main/java/database/leetcode1179/Solution.sql @@ -0,0 +1,18 @@ +-- 1179. 重新格式化部门表 +-- https://leetcode.cn/problems/reformat-department-table/ +-- yangyi 2022年08月16日17:40:55 +select id, +sum(case when month = 'Jan' then revenue end) as Jan_Revenue, +sum(case when month = 'Feb' then revenue end) as Feb_Revenue, +sum(case when month = 'Mar' then revenue end) as Mar_Revenue, +sum(case when month = 'Apr' then revenue end) as Apr_Revenue, +sum(case when month = 'May' then revenue end) as May_Revenue, +sum(case when month = 'Jun' then revenue end) as Jun_Revenue, +sum(case when month = 'Jul' then revenue end) as Jul_Revenue, +sum(case when month = 'Aug' then revenue end) as Aug_Revenue, +sum(case when month = 'Sep' then revenue end) as Sep_Revenue, +sum(case when month = 'Oct' then revenue end) as Oct_Revenue, +sum(case when month = 'Nov' then revenue end) as Nov_Revenue, +sum(case when month = 'Dec' then revenue end) as Dec_Revenue +from Department +group by id \ No newline at end of file From 5dce592519899534d844d2529fd918667858e6fb Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 18:01:21 +0800 Subject: [PATCH 085/115] =?UTF-8?q?1407.=20=E6=8E=92=E5=90=8D=E9=9D=A0?= =?UTF-8?q?=E5=89=8D=E7=9A=84=E6=97=85=E8=A1=8C=E8=80=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1407/Solution.sql | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 src/main/java/database/leetcode1407/Solution.sql diff --git a/README.md b/README.md index 872feca..9c5829f 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,7 @@ - [1050.合作过至少三次的演员和导演](/src/main/java/database/leetcode1050/Solution.sql) - [1148.文章浏览I](/src/main/java/database/leetcode1148/Solution.sql) - [1179.重新格式化部门表](/src/main/java/database/leetcode1179/Solution.sql) +- [1407.排名靠前的旅行者](/src/main/java/database/leetcode1407/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1407/Solution.sql b/src/main/java/database/leetcode1407/Solution.sql new file mode 100644 index 0000000..db38f13 --- /dev/null +++ b/src/main/java/database/leetcode1407/Solution.sql @@ -0,0 +1,12 @@ +-- 1407. 排名靠前的旅行者 +-- https://leetcode.cn/problems/top-travellers/ +-- yangyi 2022年08月16日18:00:42 +select u.name, +case +when sum(r.distance) is null then 0 +else sum(r.distance) +end as travelled_distance +from Users as u +left join Rides as r on u.id = r.user_id +group by r.user_id +order by travelled_distance DESC, u.name \ No newline at end of file From 32f241f3528b09e4188d5773e7b4ce1b5608f13f Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月16日 19:25:19 +0800 Subject: [PATCH 086/115] =?UTF-8?q?1581.=20=E8=BF=9B=E5=BA=97=E5=8D=B4?= =?UTF-8?q?=E6=9C=AA=E8=BF=9B=E8=A1=8C=E8=BF=87=E4=BA=A4=E6=98=93=E7=9A=84?= =?UTF-8?q?=E9=A1=BE=E5=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/database/leetcode1581/Solution.sql | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 src/main/java/database/leetcode1581/Solution.sql diff --git a/README.md b/README.md index 9c5829f..4c90d5e 100644 --- a/README.md +++ b/README.md @@ -343,6 +343,7 @@ - [1148.文章浏览I](/src/main/java/database/leetcode1148/Solution.sql) - [1179.重新格式化部门表](/src/main/java/database/leetcode1179/Solution.sql) - [1407.排名靠前的旅行者](/src/main/java/database/leetcode1407/Solution.sql) +- [1581.进店却未进行过交易的顾客](/src/main/java/database/leetcode1581/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1581/Solution.sql b/src/main/java/database/leetcode1581/Solution.sql new file mode 100644 index 0000000..317a3a7 --- /dev/null +++ b/src/main/java/database/leetcode1581/Solution.sql @@ -0,0 +1,8 @@ +-- 1581. 进店却未进行过交易的顾客 +-- https://leetcode.cn/problems/customer-who-visited-but-did-not-make-any-transactions/ +-- yangyi 2022年08月16日19:15:39 +select v.customer_id, count(customer_id) as count_no_trans +from Visits as v +left join Transactions as t on v.visit_id = t.visit_id +where t.amount is null +group by customer_id \ No newline at end of file From 1e3d131ca24c35147beecdb06caf8a0de635d609 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月17日 00:30:21 +0800 Subject: [PATCH 087/115] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=A2=98=E7=9B=AE=E6=89=80=E5=9C=A8=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 62 +++++++++---------- .../leetcode1050/Solution.sql | 0 .../leetcode1084/Solution.sql | 0 .../leetcode1148/Solution.sql | 0 .../leetcode1179/Solution.sql | 0 .../leetcode1407/Solution.sql | 0 .../leetcode1484/Solution.sql | 0 .../leetcode1527/Solution.sql | 0 .../leetcode1581/Solution.sql | 0 .../leetcode1667/Solution.sql | 0 .../{database => db}/leetcode175/Solution.sql | 0 .../leetcode1757/Solution.sql | 0 .../{database => db}/leetcode176/Solution.sql | 0 .../{database => db}/leetcode177/Solution.sql | 0 .../{database => db}/leetcode178/Solution.sql | 0 .../{database => db}/leetcode180/Solution.sql | 0 .../{database => db}/leetcode181/Solution.sql | 0 .../{database => db}/leetcode182/Solution.sql | 0 .../{database => db}/leetcode183/Solution.sql | 0 .../leetcode1873/Solution.sql | 0 .../{database => db}/leetcode196/Solution.sql | 0 .../{database => db}/leetcode197/Solution.sql | 0 .../{database => db}/leetcode511/Solution.sql | 0 .../{database => db}/leetcode584/Solution.sql | 0 .../{database => db}/leetcode586/Solution.sql | 0 .../{database => db}/leetcode595/Solution.sql | 0 .../{database => db}/leetcode596/Solution.sql | 0 .../{database => db}/leetcode607/Solution.sql | 0 .../{database => db}/leetcode608/Solution.sql | 0 .../{database => db}/leetcode620/Solution.sql | 0 .../{database => db}/leetcode626/Solution.sql | 0 .../{database => db}/leetcode627/Solution.sql | 0 32 files changed, 31 insertions(+), 31 deletions(-) rename src/main/java/{database => db}/leetcode1050/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1084/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1148/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1179/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1407/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1484/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1527/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1581/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1667/Solution.sql (100%) rename src/main/java/{database => db}/leetcode175/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1757/Solution.sql (100%) rename src/main/java/{database => db}/leetcode176/Solution.sql (100%) rename src/main/java/{database => db}/leetcode177/Solution.sql (100%) rename src/main/java/{database => db}/leetcode178/Solution.sql (100%) rename src/main/java/{database => db}/leetcode180/Solution.sql (100%) rename src/main/java/{database => db}/leetcode181/Solution.sql (100%) rename src/main/java/{database => db}/leetcode182/Solution.sql (100%) rename src/main/java/{database => db}/leetcode183/Solution.sql (100%) rename src/main/java/{database => db}/leetcode1873/Solution.sql (100%) rename src/main/java/{database => db}/leetcode196/Solution.sql (100%) rename src/main/java/{database => db}/leetcode197/Solution.sql (100%) rename src/main/java/{database => db}/leetcode511/Solution.sql (100%) rename src/main/java/{database => db}/leetcode584/Solution.sql (100%) rename src/main/java/{database => db}/leetcode586/Solution.sql (100%) rename src/main/java/{database => db}/leetcode595/Solution.sql (100%) rename src/main/java/{database => db}/leetcode596/Solution.sql (100%) rename src/main/java/{database => db}/leetcode607/Solution.sql (100%) rename src/main/java/{database => db}/leetcode608/Solution.sql (100%) rename src/main/java/{database => db}/leetcode620/Solution.sql (100%) rename src/main/java/{database => db}/leetcode626/Solution.sql (100%) rename src/main/java/{database => db}/leetcode627/Solution.sql (100%) diff --git a/README.md b/README.md index 4c90d5e..753d8f3 100644 --- a/README.md +++ b/README.md @@ -313,37 +313,37 @@ ## SQL -- [175.组合两个表](/src/main/java/database/leetcode175/Solution.sql) -- [176.第二高的薪水](/src/main/java/database/leetcode176/Solution.sql) -- [177.第N高的薪水](/src/main/java/database/leetcode177/Solution.sql) -- [178.分数排名](/src/main/java/database/leetcode178/Solution.sql) -- [180.连续出现的数字](/src/main/java/database/leetcode180/Solution.sql) -- [181.超过经理收入的员工](/src/main/java/database/leetcode181/Solution.sql) -- [182.查找重复的电子邮箱](/src/main/java/database/leetcode182/Solution.sql) -- [183.从不订购的客户](/src/main/java/database/leetcode183/Solution.sql) -- [196.删除重复的电子邮箱](/src/main/java/database/leetcode196/Solution.sql) -- [197.上升的温度](/src/main/java/database/leetcode197/Solution.sql) -- [511.游戏玩法分析I](/src/main/java/database/leetcode511/Solution.sql) -- [584.寻找用户推荐人](/src/main/java/database/leetcode584/Solution.sql) -- [595.大的国家](/src/main/java/database/leetcode595/Solution.sql) -- [1757.可回收且低脂的产品](/src/main/java/database/leetcode1757/Solution.sql) -- [1873.计算特殊奖金](/src/main/java/database/leetcode1873/Solution.sql) -- [627.变更性别](/src/main/java/database/leetcode627/Solution.sql) -- [586.订单最多的客户](/src/main/java/database/leetcode586/Solution.sql) -- [620.有趣的电影](/src/main/java/database/leetcode620/Solution.sql) -- [596.超过5名学生的课](/src/main/java/database/leetcode596/Solution.sql) -- [1667.修复表中的名字](/src/main/java/database/leetcode1667/Solution.sql) -- [1484.按日期分组销售产品](/src/main/java/database/leetcode1484/Solution.sql) -- [1527.患某种疾病的患者](/src/main/java/database/leetcode1527/Solution.sql) -- [607.销售员](/src/main/java/database/leetcode607/Solution.sql) -- [608.树节点](/src/main/java/database/leetcode608/Solution.sql) -- [1084.销售分析III](/src/main/java/database/leetcode1084/Solution.sql) -- [626.换座位](/src/main/java/database/leetcode626/Solution.sql) -- [1050.合作过至少三次的演员和导演](/src/main/java/database/leetcode1050/Solution.sql) -- [1148.文章浏览I](/src/main/java/database/leetcode1148/Solution.sql) -- [1179.重新格式化部门表](/src/main/java/database/leetcode1179/Solution.sql) -- [1407.排名靠前的旅行者](/src/main/java/database/leetcode1407/Solution.sql) -- [1581.进店却未进行过交易的顾客](/src/main/java/database/leetcode1581/Solution.sql) +- [175.组合两个表](/src/main/java/db/leetcode175/Solution.sql) +- [176.第二高的薪水](/src/main/java/db/leetcode176/Solution.sql) +- [177.第N高的薪水](/src/main/java/db/leetcode177/Solution.sql) +- [178.分数排名](/src/main/java/db/leetcode178/Solution.sql) +- [180.连续出现的数字](/src/main/java/db/leetcode180/Solution.sql) +- [181.超过经理收入的员工](/src/main/java/db/leetcode181/Solution.sql) +- [182.查找重复的电子邮箱](/src/main/java/db/leetcode182/Solution.sql) +- [183.从不订购的客户](/src/main/java/db/leetcode183/Solution.sql) +- [196.删除重复的电子邮箱](/src/main/java/db/leetcode196/Solution.sql) +- [197.上升的温度](/src/main/java/db/leetcode197/Solution.sql) +- [511.游戏玩法分析I](/src/main/java/db/leetcode511/Solution.sql) +- [584.寻找用户推荐人](/src/main/java/db/leetcode584/Solution.sql) +- [595.大的国家](/src/main/java/db/leetcode595/Solution.sql) +- [1757.可回收且低脂的产品](/src/main/java/db/leetcode1757/Solution.sql) +- [1873.计算特殊奖金](/src/main/java/db/leetcode1873/Solution.sql) +- [627.变更性别](/src/main/java/db/leetcode627/Solution.sql) +- [586.订单最多的客户](/src/main/java/db/leetcode586/Solution.sql) +- [620.有趣的电影](/src/main/java/db/leetcode620/Solution.sql) +- [596.超过5名学生的课](/src/main/java/db/leetcode596/Solution.sql) +- [1667.修复表中的名字](/src/main/java/db/leetcode1667/Solution.sql) +- [1484.按日期分组销售产品](/src/main/java/db/leetcode1484/Solution.sql) +- [1527.患某种疾病的患者](/src/main/java/db/leetcode1527/Solution.sql) +- [607.销售员](/src/main/java/db/leetcode607/Solution.sql) +- [608.树节点](/src/main/java/db/leetcode608/Solution.sql) +- [1084.销售分析III](/src/main/java/db/leetcode1084/Solution.sql) +- [626.换座位](/src/main/java/db/leetcode626/Solution.sql) +- [1050.合作过至少三次的演员和导演](/src/main/java/db/leetcode1050/Solution.sql) +- [1148.文章浏览I](/src/main/java/db/leetcode1148/Solution.sql) +- [1179.重新格式化部门表](/src/main/java/db/leetcode1179/Solution.sql) +- [1407.排名靠前的旅行者](/src/main/java/db/leetcode1407/Solution.sql) +- [1581.进店却未进行过交易的顾客](/src/main/java/db/leetcode1581/Solution.sql) ## PAT diff --git a/src/main/java/database/leetcode1050/Solution.sql b/src/main/java/db/leetcode1050/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1050/Solution.sql rename to src/main/java/db/leetcode1050/Solution.sql diff --git a/src/main/java/database/leetcode1084/Solution.sql b/src/main/java/db/leetcode1084/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1084/Solution.sql rename to src/main/java/db/leetcode1084/Solution.sql diff --git a/src/main/java/database/leetcode1148/Solution.sql b/src/main/java/db/leetcode1148/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1148/Solution.sql rename to src/main/java/db/leetcode1148/Solution.sql diff --git a/src/main/java/database/leetcode1179/Solution.sql b/src/main/java/db/leetcode1179/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1179/Solution.sql rename to src/main/java/db/leetcode1179/Solution.sql diff --git a/src/main/java/database/leetcode1407/Solution.sql b/src/main/java/db/leetcode1407/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1407/Solution.sql rename to src/main/java/db/leetcode1407/Solution.sql diff --git a/src/main/java/database/leetcode1484/Solution.sql b/src/main/java/db/leetcode1484/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1484/Solution.sql rename to src/main/java/db/leetcode1484/Solution.sql diff --git a/src/main/java/database/leetcode1527/Solution.sql b/src/main/java/db/leetcode1527/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1527/Solution.sql rename to src/main/java/db/leetcode1527/Solution.sql diff --git a/src/main/java/database/leetcode1581/Solution.sql b/src/main/java/db/leetcode1581/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1581/Solution.sql rename to src/main/java/db/leetcode1581/Solution.sql diff --git a/src/main/java/database/leetcode1667/Solution.sql b/src/main/java/db/leetcode1667/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1667/Solution.sql rename to src/main/java/db/leetcode1667/Solution.sql diff --git a/src/main/java/database/leetcode175/Solution.sql b/src/main/java/db/leetcode175/Solution.sql similarity index 100% rename from src/main/java/database/leetcode175/Solution.sql rename to src/main/java/db/leetcode175/Solution.sql diff --git a/src/main/java/database/leetcode1757/Solution.sql b/src/main/java/db/leetcode1757/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1757/Solution.sql rename to src/main/java/db/leetcode1757/Solution.sql diff --git a/src/main/java/database/leetcode176/Solution.sql b/src/main/java/db/leetcode176/Solution.sql similarity index 100% rename from src/main/java/database/leetcode176/Solution.sql rename to src/main/java/db/leetcode176/Solution.sql diff --git a/src/main/java/database/leetcode177/Solution.sql b/src/main/java/db/leetcode177/Solution.sql similarity index 100% rename from src/main/java/database/leetcode177/Solution.sql rename to src/main/java/db/leetcode177/Solution.sql diff --git a/src/main/java/database/leetcode178/Solution.sql b/src/main/java/db/leetcode178/Solution.sql similarity index 100% rename from src/main/java/database/leetcode178/Solution.sql rename to src/main/java/db/leetcode178/Solution.sql diff --git a/src/main/java/database/leetcode180/Solution.sql b/src/main/java/db/leetcode180/Solution.sql similarity index 100% rename from src/main/java/database/leetcode180/Solution.sql rename to src/main/java/db/leetcode180/Solution.sql diff --git a/src/main/java/database/leetcode181/Solution.sql b/src/main/java/db/leetcode181/Solution.sql similarity index 100% rename from src/main/java/database/leetcode181/Solution.sql rename to src/main/java/db/leetcode181/Solution.sql diff --git a/src/main/java/database/leetcode182/Solution.sql b/src/main/java/db/leetcode182/Solution.sql similarity index 100% rename from src/main/java/database/leetcode182/Solution.sql rename to src/main/java/db/leetcode182/Solution.sql diff --git a/src/main/java/database/leetcode183/Solution.sql b/src/main/java/db/leetcode183/Solution.sql similarity index 100% rename from src/main/java/database/leetcode183/Solution.sql rename to src/main/java/db/leetcode183/Solution.sql diff --git a/src/main/java/database/leetcode1873/Solution.sql b/src/main/java/db/leetcode1873/Solution.sql similarity index 100% rename from src/main/java/database/leetcode1873/Solution.sql rename to src/main/java/db/leetcode1873/Solution.sql diff --git a/src/main/java/database/leetcode196/Solution.sql b/src/main/java/db/leetcode196/Solution.sql similarity index 100% rename from src/main/java/database/leetcode196/Solution.sql rename to src/main/java/db/leetcode196/Solution.sql diff --git a/src/main/java/database/leetcode197/Solution.sql b/src/main/java/db/leetcode197/Solution.sql similarity index 100% rename from src/main/java/database/leetcode197/Solution.sql rename to src/main/java/db/leetcode197/Solution.sql diff --git a/src/main/java/database/leetcode511/Solution.sql b/src/main/java/db/leetcode511/Solution.sql similarity index 100% rename from src/main/java/database/leetcode511/Solution.sql rename to src/main/java/db/leetcode511/Solution.sql diff --git a/src/main/java/database/leetcode584/Solution.sql b/src/main/java/db/leetcode584/Solution.sql similarity index 100% rename from src/main/java/database/leetcode584/Solution.sql rename to src/main/java/db/leetcode584/Solution.sql diff --git a/src/main/java/database/leetcode586/Solution.sql b/src/main/java/db/leetcode586/Solution.sql similarity index 100% rename from src/main/java/database/leetcode586/Solution.sql rename to src/main/java/db/leetcode586/Solution.sql diff --git a/src/main/java/database/leetcode595/Solution.sql b/src/main/java/db/leetcode595/Solution.sql similarity index 100% rename from src/main/java/database/leetcode595/Solution.sql rename to src/main/java/db/leetcode595/Solution.sql diff --git a/src/main/java/database/leetcode596/Solution.sql b/src/main/java/db/leetcode596/Solution.sql similarity index 100% rename from src/main/java/database/leetcode596/Solution.sql rename to src/main/java/db/leetcode596/Solution.sql diff --git a/src/main/java/database/leetcode607/Solution.sql b/src/main/java/db/leetcode607/Solution.sql similarity index 100% rename from src/main/java/database/leetcode607/Solution.sql rename to src/main/java/db/leetcode607/Solution.sql diff --git a/src/main/java/database/leetcode608/Solution.sql b/src/main/java/db/leetcode608/Solution.sql similarity index 100% rename from src/main/java/database/leetcode608/Solution.sql rename to src/main/java/db/leetcode608/Solution.sql diff --git a/src/main/java/database/leetcode620/Solution.sql b/src/main/java/db/leetcode620/Solution.sql similarity index 100% rename from src/main/java/database/leetcode620/Solution.sql rename to src/main/java/db/leetcode620/Solution.sql diff --git a/src/main/java/database/leetcode626/Solution.sql b/src/main/java/db/leetcode626/Solution.sql similarity index 100% rename from src/main/java/database/leetcode626/Solution.sql rename to src/main/java/db/leetcode626/Solution.sql diff --git a/src/main/java/database/leetcode627/Solution.sql b/src/main/java/db/leetcode627/Solution.sql similarity index 100% rename from src/main/java/database/leetcode627/Solution.sql rename to src/main/java/db/leetcode627/Solution.sql From 3ec04e81071551524d8c6d6a0fd0cf4742b22ebc Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月17日 17:05:27 +0800 Subject: [PATCH 088/115] =?UTF-8?q?1795.=20=E6=AF=8F=E4=B8=AA=E4=BA=A7?= =?UTF-8?q?=E5=93=81=E5=9C=A8=E4=B8=8D=E5=90=8C=E5=95=86=E5=BA=97=E7=9A=84?= =?UTF-8?q?=E4=BB=B7=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1795/Solution.sql | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/main/java/db/leetcode1795/Solution.sql diff --git a/README.md b/README.md index 753d8f3..d47cc47 100644 --- a/README.md +++ b/README.md @@ -344,6 +344,7 @@ - [1179.重新格式化部门表](/src/main/java/db/leetcode1179/Solution.sql) - [1407.排名靠前的旅行者](/src/main/java/db/leetcode1407/Solution.sql) - [1581.进店却未进行过交易的顾客](/src/main/java/db/leetcode1581/Solution.sql) +- [1795.每个产品在不同商店的价格](/src/main/java/db/leetcode1795/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1795/Solution.sql b/src/main/java/db/leetcode1795/Solution.sql new file mode 100644 index 0000000..9736fb7 --- /dev/null +++ b/src/main/java/db/leetcode1795/Solution.sql @@ -0,0 +1,11 @@ +-- 1795. 每个产品在不同商店的价格 +-- https://leetcode.cn/problems/rearrange-products-table/ +-- yangyi 2022年08月17日17:04:54 +select product_id, 'store1' as store, store1 as price +from Products where store1 is not null +union +select product_id, 'store2' as store, store2 as price +from Products where store2 is not null +union +select product_id, 'store3' as store, store3 as price +from Products where store3 is not null \ No newline at end of file From a3469db0d16b78284a828433a5cda46c9da1a8b8 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月17日 17:25:10 +0800 Subject: [PATCH 089/115] =?UTF-8?q?1965.=20=E4=B8=A2=E5=A4=B1=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E9=9B=87=E5=91=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1965/Solution.sql | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 src/main/java/db/leetcode1965/Solution.sql diff --git a/README.md b/README.md index d47cc47..eb72dfe 100644 --- a/README.md +++ b/README.md @@ -345,6 +345,7 @@ - [1407.排名靠前的旅行者](/src/main/java/db/leetcode1407/Solution.sql) - [1581.进店却未进行过交易的顾客](/src/main/java/db/leetcode1581/Solution.sql) - [1795.每个产品在不同商店的价格](/src/main/java/db/leetcode1795/Solution.sql) +- [1965.丢失信息的雇员](/src/main/java/db/leetcode1965/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1965/Solution.sql b/src/main/java/db/leetcode1965/Solution.sql new file mode 100644 index 0000000..02734c6 --- /dev/null +++ b/src/main/java/db/leetcode1965/Solution.sql @@ -0,0 +1,13 @@ +-- 1965. 丢失信息的雇员 +-- https://leetcode.cn/problems/employees-with-missing-information/ +-- yangyi 2022年08月17日17:24:49 +select e.employee_id as employee_id +from Employees as e +left join Salaries as s on e.employee_id = s.employee_id +where s.salary is null +union +select s.employee_id as employee_id +from Salaries as s +left join Employees as e on s.employee_id = e.employee_id +where e.name is null +order by employee_id \ No newline at end of file From b1f025bacc5b0adbe439b716ecca441bc046b5d7 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月18日 01:30:55 +0800 Subject: [PATCH 090/115] =?UTF-8?q?1729.=E6=B1=82=E5=85=B3=E6=B3=A8?= =?UTF-8?q?=E8=80=85=E7=9A=84=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1729/Solution.sql | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/db/leetcode1729/Solution.sql diff --git a/README.md b/README.md index eb72dfe..90178e2 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,7 @@ - [1581.进店却未进行过交易的顾客](/src/main/java/db/leetcode1581/Solution.sql) - [1795.每个产品在不同商店的价格](/src/main/java/db/leetcode1795/Solution.sql) - [1965.丢失信息的雇员](/src/main/java/db/leetcode1965/Solution.sql) +- [1729.求关注者的数量](/src/main/java/db/leetcode1729/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1729/Solution.sql b/src/main/java/db/leetcode1729/Solution.sql new file mode 100644 index 0000000..0a7a703 --- /dev/null +++ b/src/main/java/db/leetcode1729/Solution.sql @@ -0,0 +1,7 @@ +-- 1729. 求关注者的数量 +-- https://leetcode.cn/problems/find-followers-count/ +-- yangyi 2022年08月17日00:37:21 +select user_id, count(follower_id) as followers_count +from Followers +group by user_id +order by user_id \ No newline at end of file From 7916176226674891624a772898c7875b8fb30fd6 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月18日 01:30:55 +0800 Subject: [PATCH 091/115] =?UTF-8?q?1729.=E6=B1=82=E5=85=B3=E6=B3=A8?= =?UTF-8?q?=E8=80=85=E7=9A=84=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/db/leetcode1890/Solution.sql | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/main/java/db/leetcode1890/Solution.sql diff --git a/src/main/java/db/leetcode1890/Solution.sql b/src/main/java/db/leetcode1890/Solution.sql new file mode 100644 index 0000000..ffbef35 --- /dev/null +++ b/src/main/java/db/leetcode1890/Solution.sql @@ -0,0 +1,7 @@ +-- 1890. 2020年最后一次登录 +-- https://leetcode.cn/problems/the-latest-login-in-2020/ +-- yangyi 2022年08月19日15:04:26 +select user_id, max(time_stamp) as last_stamp +from Logins +where left(time_stamp, 4) = '2020' +group by user_id \ No newline at end of file From 355bba2fc4d7cb09b30bd5ba619ba66d713c8fc6 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月19日 15:08:28 +0800 Subject: [PATCH 092/115] =?UTF-8?q?1890.=202020=E5=B9=B4=E6=9C=80=E5=90=8E?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 90178e2..4da9264 100644 --- a/README.md +++ b/README.md @@ -347,6 +347,7 @@ - [1795.每个产品在不同商店的价格](/src/main/java/db/leetcode1795/Solution.sql) - [1965.丢失信息的雇员](/src/main/java/db/leetcode1965/Solution.sql) - [1729.求关注者的数量](/src/main/java/db/leetcode1729/Solution.sql) +- [1890.2020年最后一次登录](/src/main/java/db/leetcode1890/Solution.sql) ## PAT From 9391bd620a6a8e2d4c9fb84418bbe996bcfd8378 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月19日 15:36:47 +0800 Subject: [PATCH 093/115] =?UTF-8?q?1393.=20=E8=82=A1=E7=A5=A8=E7=9A=84?= =?UTF-8?q?=E8=B5=84=E6=9C=AC=E6=8D=9F=E7=9B=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1393/Solution.sql | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 src/main/java/db/leetcode1393/Solution.sql diff --git a/README.md b/README.md index 4da9264..b9e9ccb 100644 --- a/README.md +++ b/README.md @@ -348,6 +348,7 @@ - [1965.丢失信息的雇员](/src/main/java/db/leetcode1965/Solution.sql) - [1729.求关注者的数量](/src/main/java/db/leetcode1729/Solution.sql) - [1890.2020年最后一次登录](/src/main/java/db/leetcode1890/Solution.sql) +- [1393.股票的资本损益](/src/main/java/db/leetcode1393/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1393/Solution.sql b/src/main/java/db/leetcode1393/Solution.sql new file mode 100644 index 0000000..acefa99 --- /dev/null +++ b/src/main/java/db/leetcode1393/Solution.sql @@ -0,0 +1,9 @@ +-- 1393. 股票的资本损益 +-- https://leetcode.cn/problems/capital-gainloss/ +-- yangyi 2022年08月19日15:36:10 +select stock_name, sum( +case when operation = 'Buy' then -price + when operation = 'Sell' then price +end) as capital_gain_loss +from Stocks +group by stock_name \ No newline at end of file From 95b611b2dc464043177f4d2a9d32e820905c9861 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月19日 16:03:26 +0800 Subject: [PATCH 094/115] =?UTF-8?q?1141.=20=E6=9F=A5=E8=AF=A2=E8=BF=9130?= =?UTF-8?q?=E5=A4=A9=E6=B4=BB=E8=B7=83=E7=94=A8=E6=88=B7=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1141/Solution.sql | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 src/main/java/db/leetcode1141/Solution.sql diff --git a/README.md b/README.md index b9e9ccb..cac4fdc 100644 --- a/README.md +++ b/README.md @@ -349,6 +349,7 @@ - [1729.求关注者的数量](/src/main/java/db/leetcode1729/Solution.sql) - [1890.2020年最后一次登录](/src/main/java/db/leetcode1890/Solution.sql) - [1393.股票的资本损益](/src/main/java/db/leetcode1393/Solution.sql) +- [1141.查询近30天活跃用户数](/src/main/java/db/leetcode1141/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1141/Solution.sql b/src/main/java/db/leetcode1141/Solution.sql new file mode 100644 index 0000000..27cba2c --- /dev/null +++ b/src/main/java/db/leetcode1141/Solution.sql @@ -0,0 +1,7 @@ +-- 1141. 查询近30天活跃用户数 +-- https://leetcode.cn/problems/user-activity-for-the-past-30-days-i/ +-- yangyi 2022年08月19日16:02:56 +select activity_date as day, count(distinct user_id) as active_users +from Activity +where datediff('2019-07-27', activity_date)> 0 and datediff('2019-07-27', activity_date) < 30 +group by activity_date \ No newline at end of file From 9339fdcf35cd4644cfbff1ac8b9e98cd5c98cc5d Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月19日 18:30:51 +0800 Subject: [PATCH 095/115] =?UTF-8?q?184.=20=E9=83=A8=E9=97=A8=E5=B7=A5?= =?UTF-8?q?=E8=B5=84=E6=9C=80=E9=AB=98=E7=9A=84=E5=91=98=E5=B7=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode184/Solution.sql | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 src/main/java/db/leetcode184/Solution.sql diff --git a/README.md b/README.md index cac4fdc..6d09af3 100644 --- a/README.md +++ b/README.md @@ -350,6 +350,7 @@ - [1890.2020年最后一次登录](/src/main/java/db/leetcode1890/Solution.sql) - [1393.股票的资本损益](/src/main/java/db/leetcode1393/Solution.sql) - [1141.查询近30天活跃用户数](/src/main/java/db/leetcode1141/Solution.sql) +- [184.部门工资最高的员工](/src/main/java/db/leetcode184/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode184/Solution.sql b/src/main/java/db/leetcode184/Solution.sql new file mode 100644 index 0000000..e9e4b85 --- /dev/null +++ b/src/main/java/db/leetcode184/Solution.sql @@ -0,0 +1,11 @@ +-- 184. 部门工资最高的员工 +-- https://leetcode.cn/problems/department-highest-salary/ +-- yangyi 2022年08月19日18:30:24 +select d.name as 'Department', e.name as 'Employee', e.salary as Salary +from Employee as e +left join Department as d on e.departmentId = d.id +where (e.departmentId, e.salary) in ( +select departmentId, max(salary) +from Employee +group by departmentId +) \ No newline at end of file From e3ebb492ac51ca90e7364dd40c9e62202616215d Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月22日 14:38:16 +0800 Subject: [PATCH 096/115] =?UTF-8?q?1741.=20=E6=9F=A5=E6=89=BE=E6=AF=8F?= =?UTF-8?q?=E4=B8=AA=E5=91=98=E5=B7=A5=E8=8A=B1=E8=B4=B9=E7=9A=84=E6=80=BB?= =?UTF-8?q?=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1741/Solution.sql | 6 ++++++ 2 files changed, 7 insertions(+) create mode 100644 src/main/java/db/leetcode1741/Solution.sql diff --git a/README.md b/README.md index 6d09af3..d9eee09 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,7 @@ - [1393.股票的资本损益](/src/main/java/db/leetcode1393/Solution.sql) - [1141.查询近30天活跃用户数](/src/main/java/db/leetcode1141/Solution.sql) - [184.部门工资最高的员工](/src/main/java/db/leetcode184/Solution.sql) +- [1741.查找每个员工花费的总时间](/src/main/java/db/leetcode1741/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1741/Solution.sql b/src/main/java/db/leetcode1741/Solution.sql new file mode 100644 index 0000000..ca71e59 --- /dev/null +++ b/src/main/java/db/leetcode1741/Solution.sql @@ -0,0 +1,6 @@ +-- 1741. 查找每个员工花费的总时间 +-- https://leetcode.cn/problems/find-total-time-spent-by-each-employee/ +-- yangyi 2022年08月22日14:37:39 +select event_day as day, emp_id, sum(out_time - in_time) as total_time +from Employees +group by event_day, emp_id \ No newline at end of file From 5c17b464b84d90847f92fa96f5746d4acdcbd8e3 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月23日 17:00:22 +0800 Subject: [PATCH 097/115] =?UTF-8?q?1050.=20=E5=90=88=E4=BD=9C=E8=BF=87?= =?UTF-8?q?=E8=87=B3=E5=B0=91=E4=B8=89=E6=AC=A1=E7=9A=84=E6=BC=94=E5=91=98?= =?UTF-8?q?=E5=92=8C=E5=AF=BC=E6=BC=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/db/leetcode1050/Solution.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/db/leetcode1050/Solution.sql b/src/main/java/db/leetcode1050/Solution.sql index 0dfa27f..c13e835 100644 --- a/src/main/java/db/leetcode1050/Solution.sql +++ b/src/main/java/db/leetcode1050/Solution.sql @@ -4,4 +4,4 @@ select actor_id, director_id from ActorDirector group by actor_id, director_id -having count(timestamp)>= 3 \ No newline at end of file +having count(actor_id)>= 3 and count(director_id)>= 3 \ No newline at end of file From e62d91c82c6d419e8d852d328d039634fa30c344 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月23日 17:07:40 +0800 Subject: [PATCH 098/115] =?UTF-8?q?1587.=20=E9=93=B6=E8=A1=8C=E8=B4=A6?= =?UTF-8?q?=E6=88=B7=E6=A6=82=E8=A6=81=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1587/Solution.sql | 8 ++++++++ 2 files changed, 9 insertions(+) create mode 100644 src/main/java/db/leetcode1587/Solution.sql diff --git a/README.md b/README.md index d9eee09..638d4f5 100644 --- a/README.md +++ b/README.md @@ -352,6 +352,7 @@ - [1141.查询近30天活跃用户数](/src/main/java/db/leetcode1141/Solution.sql) - [184.部门工资最高的员工](/src/main/java/db/leetcode184/Solution.sql) - [1741.查找每个员工花费的总时间](/src/main/java/db/leetcode1741/Solution.sql) +- [1587.银行账户概要II](/src/main/java/db/leetcode1587/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1587/Solution.sql b/src/main/java/db/leetcode1587/Solution.sql new file mode 100644 index 0000000..857f75c --- /dev/null +++ b/src/main/java/db/leetcode1587/Solution.sql @@ -0,0 +1,8 @@ +-- 1587. 银行账户概要 II +-- https://leetcode.cn/problems/bank-account-summary-ii/ +-- yangyi 2022年08月23日17:06:51 +select u.name, sum(t.amount) as balance +from Users as u +left join Transactions as t on t.account = u.account +group by t.account +having sum(t.amount)> 10000 \ No newline at end of file From 6bf4e1ee218386365fd75e313a9a224a1c7838b3 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2022年8月23日 17:47:43 +0800 Subject: [PATCH 099/115] =?UTF-8?q?1158.=20=E5=B8=82=E5=9C=BA=E5=88=86?= =?UTF-8?q?=E6=9E=90=20I?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/db/leetcode1158/Solution.sql | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 src/main/java/db/leetcode1158/Solution.sql diff --git a/README.md b/README.md index 638d4f5..0846f1b 100644 --- a/README.md +++ b/README.md @@ -353,6 +353,7 @@ - [184.部门工资最高的员工](/src/main/java/db/leetcode184/Solution.sql) - [1741.查找每个员工花费的总时间](/src/main/java/db/leetcode1741/Solution.sql) - [1587.银行账户概要II](/src/main/java/db/leetcode1587/Solution.sql) +- [1158.市场分析I](/src/main/java/db/leetcode1158/Solution.sql) ## PAT diff --git a/src/main/java/db/leetcode1158/Solution.sql b/src/main/java/db/leetcode1158/Solution.sql new file mode 100644 index 0000000..7aa57b8 --- /dev/null +++ b/src/main/java/db/leetcode1158/Solution.sql @@ -0,0 +1,12 @@ +-- 1158. 市场分析 I +-- https://leetcode.cn/problems/market-analysis-i/ +-- yangyi 2022年08月23日17:47:06 +select Users.user_id as buyer_id, join_date, ifnull(UserBuy.cnt, 0) as orders_in_2019 +from Users +left join ( + select buyer_id, count(order_id) cnt + from Orders + where order_date between '2019-01-01' and '2019-12-31' + group by buyer_id +) UserBuy +on Users.user_id = UserBuy.buyer_id \ No newline at end of file From e42a09fa0dd4aa4496a105b9a804fd4cedaebb8f Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 12:56:33 +0800 Subject: [PATCH 100/115] =?UTF-8?q?HJ1.=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E5=8D=95=E8=AF=8D=E7=9A=84?= =?UTF-8?q?=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++++ .../java/ds/greedy/leetcode53/Solution.java | 2 -- src/main/java/od/HJ1/Main.java | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 src/main/java/od/HJ1/Main.java diff --git a/README.md b/README.md index 0846f1b..59a9d55 100644 --- a/README.md +++ b/README.md @@ -355,6 +355,10 @@ - [1587.银行账户概要II](/src/main/java/db/leetcode1587/Solution.sql) - [1158.市场分析I](/src/main/java/db/leetcode1158/Solution.sql) +## 华为OD + +- [HJ1.字符串最后一个单词的长度](/src/main/java/od/HJ1/Main.java) + ## PAT 1. [1001. 害死人不偿命的(3n+1)猜想(15分)](/src/main/java/pat/pat1001/Main.java) diff --git a/src/main/java/ds/greedy/leetcode53/Solution.java b/src/main/java/ds/greedy/leetcode53/Solution.java index bd75eb4..cad8549 100644 --- a/src/main/java/ds/greedy/leetcode53/Solution.java +++ b/src/main/java/ds/greedy/leetcode53/Solution.java @@ -1,7 +1,5 @@ package ds.greedy.leetcode53; -import org.omg.CORBA.INTERNAL; - /** * 最大子序和 * LeetCode 53 https://leetcode-cn.com/problems/maximum-subarray/ diff --git a/src/main/java/od/HJ1/Main.java b/src/main/java/od/HJ1/Main.java new file mode 100644 index 0000000..8b2c63d --- /dev/null +++ b/src/main/java/od/HJ1/Main.java @@ -0,0 +1,24 @@ +package od.HJ1; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日12:55:18 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String str = in.nextLine(); + char[] chars = str.toCharArray(); + int count = 0; + for (int i = chars.length - 1; i>= 0; i--) { + if (chars[i] != ' ') { + count++; + } else { + break; + } + } + System.out.println(count); + } +} From 601efba995191bbc36faca25b02bb909abbee0f1 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 13:09:35 +0800 Subject: [PATCH 101/115] =?UTF-8?q?HJ2.=E8=AE=A1=E7=AE=97=E6=9F=90?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E5=87=BA=E7=8E=B0=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ2/Main.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/main/java/od/HJ2/Main.java diff --git a/README.md b/README.md index 59a9d55..ded4023 100644 --- a/README.md +++ b/README.md @@ -358,6 +358,7 @@ ## 华为OD - [HJ1.字符串最后一个单词的长度](/src/main/java/od/HJ1/Main.java) +- [HJ2.计算某字符出现次数](/src/main/java/od/HJ2/Main.java) ## PAT diff --git a/src/main/java/od/HJ2/Main.java b/src/main/java/od/HJ2/Main.java new file mode 100644 index 0000000..6b66e0e --- /dev/null +++ b/src/main/java/od/HJ2/Main.java @@ -0,0 +1,15 @@ +package od.HJ2; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日13:16:17 + */ +class Main { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String a = in.nextLine(); + String b = in.nextLine(); + System.out.println(a.length() - a.toLowerCase().replaceAll(b.toLowerCase(), "").length()); + } +} From d99cb47a714f35d5f18300df566b149df0f53ec6 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 13:25:26 +0800 Subject: [PATCH 102/115] =?UTF-8?q?HJ3.=E6=98=8E=E6=98=8E=E7=9A=84?= =?UTF-8?q?=E9=9A=8F=E6=9C=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ3/Main.java | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/main/java/od/HJ3/Main.java diff --git a/README.md b/README.md index ded4023..81098ff 100644 --- a/README.md +++ b/README.md @@ -359,6 +359,7 @@ - [HJ1.字符串最后一个单词的长度](/src/main/java/od/HJ1/Main.java) - [HJ2.计算某字符出现次数](/src/main/java/od/HJ2/Main.java) +- [HJ3.明明的随机数](/src/main/java/od/HJ3/Main.java) ## PAT diff --git a/src/main/java/od/HJ3/Main.java b/src/main/java/od/HJ3/Main.java new file mode 100644 index 0000000..e4ae8e2 --- /dev/null +++ b/src/main/java/od/HJ3/Main.java @@ -0,0 +1,23 @@ +package od.HJ3; + +import java.util.Scanner; +import java.util.TreeSet; + +/** + * @author yangyi 2023年08月29日13:25:04 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n = in.nextInt(); + TreeSet result = new TreeSet(); + for (int i = 0; i < n; i++) { + result.add(in.nextInt()); + } + for (Integer integer : result) { + System.out.println(integer); + } + } + +} From 94cf8d2380c8bc4b7d7bc1c5830db6fc127765ad Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 13:44:58 +0800 Subject: [PATCH 103/115] =?UTF-8?q?HJ4.=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E5=88=86=E9=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ4/Main.java | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 src/main/java/od/HJ4/Main.java diff --git a/README.md b/README.md index 81098ff..2ac3659 100644 --- a/README.md +++ b/README.md @@ -360,6 +360,7 @@ - [HJ1.字符串最后一个单词的长度](/src/main/java/od/HJ1/Main.java) - [HJ2.计算某字符出现次数](/src/main/java/od/HJ2/Main.java) - [HJ3.明明的随机数](/src/main/java/od/HJ3/Main.java) +- [HJ4.字符串分隔](/src/main/java/od/HJ4/Main.java) ## PAT diff --git a/src/main/java/od/HJ4/Main.java b/src/main/java/od/HJ4/Main.java new file mode 100644 index 0000000..a36a8bd --- /dev/null +++ b/src/main/java/od/HJ4/Main.java @@ -0,0 +1,22 @@ +package od.HJ4; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日13:44:01 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String str = in.nextLine(); + while (str.length()>= 8) { + System.out.println(str.substring(0, 8)); + str = str.substring(8); + } + if (str.length() < 8 && str.length()> 0) { + str += "00000000"; + System.out.println(str.substring(0, 8)); + } + } +} From 581f6a778652fbb6f571919c5735ba98bf92faab Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 13:51:40 +0800 Subject: [PATCH 104/115] =?UTF-8?q?HJ5.=E8=BF=9B=E5=88=B6=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ5/Main.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/main/java/od/HJ5/Main.java diff --git a/README.md b/README.md index 2ac3659..03890e6 100644 --- a/README.md +++ b/README.md @@ -361,6 +361,7 @@ - [HJ2.计算某字符出现次数](/src/main/java/od/HJ2/Main.java) - [HJ3.明明的随机数](/src/main/java/od/HJ3/Main.java) - [HJ4.字符串分隔](/src/main/java/od/HJ4/Main.java) +- [HJ5.进制转换](/src/main/java/od/HJ5/Main.java) ## PAT diff --git a/src/main/java/od/HJ5/Main.java b/src/main/java/od/HJ5/Main.java new file mode 100644 index 0000000..c75faa3 --- /dev/null +++ b/src/main/java/od/HJ5/Main.java @@ -0,0 +1,15 @@ +package od.HJ5; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日13:51:10 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String str = in.nextLine(); + System.out.println(Integer.parseInt(str.substring(2), 16)); + } +} From 9740be4f3e41a02504145a3a774e95c6481ff44a Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 16:01:45 +0800 Subject: [PATCH 105/115] =?UTF-8?q?HJ6.=E8=B4=A8=E6=95=B0=E5=9B=A0?= =?UTF-8?q?=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ6/Main.java | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/main/java/od/HJ6/Main.java diff --git a/README.md b/README.md index 03890e6..1b9919c 100644 --- a/README.md +++ b/README.md @@ -362,6 +362,7 @@ - [HJ3.明明的随机数](/src/main/java/od/HJ3/Main.java) - [HJ4.字符串分隔](/src/main/java/od/HJ4/Main.java) - [HJ5.进制转换](/src/main/java/od/HJ5/Main.java) +- [**HJ6.质数因子**](/src/main/java/od/HJ6/Main.java) ## PAT diff --git a/src/main/java/od/HJ6/Main.java b/src/main/java/od/HJ6/Main.java new file mode 100644 index 0000000..f523d40 --- /dev/null +++ b/src/main/java/od/HJ6/Main.java @@ -0,0 +1,24 @@ +package od.HJ6; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日15:33:05 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + long n = in.nextLong(); + long sqrt = (long) Math.sqrt(n); + for (int i = 2; i <= sqrt; i++) { + while (n % 2 == 0) { + n /= i; + System.out.println(i + " "); + } + } + if (n != 1) { + System.out.println(n + " "); + } + } +} From afae7f9bfbfa9d3548c619948b72ad792e23f5b6 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 16:06:58 +0800 Subject: [PATCH 106/115] =?UTF-8?q?HJ7.=E5=8F=96=E8=BF=91=E4=BC=BC?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ7/Main.java | 15 +++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/main/java/od/HJ7/Main.java diff --git a/README.md b/README.md index 1b9919c..52ee681 100644 --- a/README.md +++ b/README.md @@ -363,6 +363,7 @@ - [HJ4.字符串分隔](/src/main/java/od/HJ4/Main.java) - [HJ5.进制转换](/src/main/java/od/HJ5/Main.java) - [**HJ6.质数因子**](/src/main/java/od/HJ6/Main.java) +- [HJ7.取近似值](/src/main/java/od/HJ7/Main.java) ## PAT diff --git a/src/main/java/od/HJ7/Main.java b/src/main/java/od/HJ7/Main.java new file mode 100644 index 0000000..0aab7e7 --- /dev/null +++ b/src/main/java/od/HJ7/Main.java @@ -0,0 +1,15 @@ +package od.HJ7; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日16:02:37 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + double n = in.nextDouble(); + System.out.println(Math.round(n)); + } +} From 628e2d11ae433a1e1716f0938ec5d2dab253ed0c Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 16:17:35 +0800 Subject: [PATCH 107/115] =?UTF-8?q?HJ8.=E5=90=88=E5=B9=B6=E8=A1=A8?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ8/Main.java | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/main/java/od/HJ8/Main.java diff --git a/README.md b/README.md index 52ee681..e3fd269 100644 --- a/README.md +++ b/README.md @@ -364,6 +364,7 @@ - [HJ5.进制转换](/src/main/java/od/HJ5/Main.java) - [**HJ6.质数因子**](/src/main/java/od/HJ6/Main.java) - [HJ7.取近似值](/src/main/java/od/HJ7/Main.java) +- [HJ8.合并表记录](/src/main/java/od/HJ8/Main.java) ## PAT diff --git a/src/main/java/od/HJ8/Main.java b/src/main/java/od/HJ8/Main.java new file mode 100644 index 0000000..526c071 --- /dev/null +++ b/src/main/java/od/HJ8/Main.java @@ -0,0 +1,29 @@ +package od.HJ8; + +import java.util.Map; +import java.util.Scanner; +import java.util.TreeMap; + +/** + * @author yangyi 2023年08月29日16:09:39 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n = in.nextInt(); + TreeMap tree = new TreeMap(); + for (int i = 0; i < n; i++) { + int key = in.nextInt(); + int value = in.nextInt(); + if (tree.containsKey(key)) { + tree.put(key, tree.getOrDefault(key, 0) + value); + } else { + tree.put(key, value); + } + } + for (Map.Entry integerIntegerEntry : tree.entrySet()) { + System.out.println(integerIntegerEntry.getKey() + " " + integerIntegerEntry.getValue()); + } + } +} From 38bd6f1fb0892be0fefd410feea6edf1c333ff09 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 16:43:34 +0800 Subject: [PATCH 108/115] =?UTF-8?q?HJ9.=E6=8F=90=E5=8F=96=E4=B8=8D?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E7=9A=84=E6=95=B4=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ9/Main.java | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/main/java/od/HJ9/Main.java diff --git a/README.md b/README.md index e3fd269..6d5365e 100644 --- a/README.md +++ b/README.md @@ -365,6 +365,7 @@ - [**HJ6.质数因子**](/src/main/java/od/HJ6/Main.java) - [HJ7.取近似值](/src/main/java/od/HJ7/Main.java) - [HJ8.合并表记录](/src/main/java/od/HJ8/Main.java) +- [HJ9.提取不重复的整数](/src/main/java/od/HJ9/Main.java) ## PAT diff --git a/src/main/java/od/HJ9/Main.java b/src/main/java/od/HJ9/Main.java new file mode 100644 index 0000000..a558479 --- /dev/null +++ b/src/main/java/od/HJ9/Main.java @@ -0,0 +1,24 @@ +package od.HJ9; + +import java.util.HashSet; +import java.util.Scanner; +import java.util.Set; + +/** + * @author yangyi 2023年08月29日16:18:51 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String n = in.nextLine(); + char[] chars = n.toCharArray(); + Set set = new HashSet(); + for (int i = chars.length - 1; i>= 0; i--) { + if (!set.contains(chars[i])) { + set.add(chars[i]); + System.out.print(chars[i]); + } + } + } +} From 21d2f954acc6afb175e0f0bc8b6b3bbdd957815b Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 16:54:39 +0800 Subject: [PATCH 109/115] =?UTF-8?q?HJ10.=E5=AD=97=E7=AC=A6=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ10/Main.java | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/java/od/HJ10/Main.java diff --git a/README.md b/README.md index 6d5365e..f838546 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,7 @@ - [HJ7.取近似值](/src/main/java/od/HJ7/Main.java) - [HJ8.合并表记录](/src/main/java/od/HJ8/Main.java) - [HJ9.提取不重复的整数](/src/main/java/od/HJ9/Main.java) +- [HJ10.字符个数统计](/src/main/java/od/HJ10/Main.java) ## PAT diff --git a/src/main/java/od/HJ10/Main.java b/src/main/java/od/HJ10/Main.java new file mode 100644 index 0000000..a0b1e3e --- /dev/null +++ b/src/main/java/od/HJ10/Main.java @@ -0,0 +1,25 @@ +package od.HJ10; + +import java.util.HashSet; +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日16:46:50 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String str = in.nextLine(); + char[] chars = str.toCharArray(); + HashSet set = new HashSet(); + for (char aChar : chars) { + if (aChar> 0 && aChar < 127) { + if (!set.contains(aChar)) { + set.add(aChar); + } + } + } + System.out.println(set.size()); + } +} From 04fd3cbc1b0fe39fa671516d29c94597e98f9348 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 16:59:40 +0800 Subject: [PATCH 110/115] =?UTF-8?q?HJ11.=E6=95=B0=E5=AD=97=E9=A2=A0?= =?UTF-8?q?=E5=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ11/Main.java | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/main/java/od/HJ11/Main.java diff --git a/README.md b/README.md index f838546..d1165e9 100644 --- a/README.md +++ b/README.md @@ -367,6 +367,7 @@ - [HJ8.合并表记录](/src/main/java/od/HJ8/Main.java) - [HJ9.提取不重复的整数](/src/main/java/od/HJ9/Main.java) - [HJ10.字符个数统计](/src/main/java/od/HJ10/Main.java) +- [HJ11.数字颠倒](/src/main/java/od/HJ11/Main.java) ## PAT diff --git a/src/main/java/od/HJ11/Main.java b/src/main/java/od/HJ11/Main.java new file mode 100644 index 0000000..180049e --- /dev/null +++ b/src/main/java/od/HJ11/Main.java @@ -0,0 +1,27 @@ +package od.HJ11; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日16:58:35 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String str = in.nextLine(); + char[] chars = str.toCharArray(); + int start = 0; + int end = chars.length - 1; + while (start < end) { + char temp = chars[start]; + chars[start] = chars[end]; + chars[end] = temp; + start++; + end--; + } + for (int i = 0; i < chars.length; i++) { + System.out.print(chars[i]); + } + } +} From 3114583fae146b16233fff6ac3d576b22bafc9b7 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 17:01:06 +0800 Subject: [PATCH 111/115] =?UTF-8?q?HJ12.=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E5=8F=8D=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ12/Main.java | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/main/java/od/HJ12/Main.java diff --git a/README.md b/README.md index d1165e9..207f3b6 100644 --- a/README.md +++ b/README.md @@ -368,6 +368,7 @@ - [HJ9.提取不重复的整数](/src/main/java/od/HJ9/Main.java) - [HJ10.字符个数统计](/src/main/java/od/HJ10/Main.java) - [HJ11.数字颠倒](/src/main/java/od/HJ11/Main.java) +- [HJ12.字符串反转](/src/main/java/od/HJ12/Main.java) ## PAT diff --git a/src/main/java/od/HJ12/Main.java b/src/main/java/od/HJ12/Main.java new file mode 100644 index 0000000..971784e --- /dev/null +++ b/src/main/java/od/HJ12/Main.java @@ -0,0 +1,27 @@ +package od.HJ12; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日16:58:35 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String str = in.nextLine(); + char[] chars = str.toCharArray(); + int start = 0; + int end = chars.length - 1; + while (start < end) { + char temp = chars[start]; + chars[start] = chars[end]; + chars[end] = temp; + start++; + end--; + } + for (int i = 0; i < chars.length; i++) { + System.out.print(chars[i]); + } + } +} From d1858e27519e7e15fa7e2b799a02a54208c70ae1 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 17:10:25 +0800 Subject: [PATCH 112/115] =?UTF-8?q?HJ13.=E5=8F=A5=E5=AD=90=E9=80=86?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ13/Main.java | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/java/od/HJ13/Main.java diff --git a/README.md b/README.md index 207f3b6..36858fc 100644 --- a/README.md +++ b/README.md @@ -369,6 +369,7 @@ - [HJ10.字符个数统计](/src/main/java/od/HJ10/Main.java) - [HJ11.数字颠倒](/src/main/java/od/HJ11/Main.java) - [HJ12.字符串反转](/src/main/java/od/HJ12/Main.java) +- [HJ13.句子逆序](/src/main/java/od/HJ13/Main.java) ## PAT diff --git a/src/main/java/od/HJ13/Main.java b/src/main/java/od/HJ13/Main.java new file mode 100644 index 0000000..548c8f8 --- /dev/null +++ b/src/main/java/od/HJ13/Main.java @@ -0,0 +1,25 @@ +package od.HJ13; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日17:03:34 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String str = in.nextLine(); + char[] chars = str.toCharArray(); + if (str.contains(" ")) { + String[] strs = str.split(" "); + for (int i = strs.length - 1; i>= 0; i--) { + System.out.print(strs[i] + " "); + } + } else { + for (char aChar : chars) { + System.out.print(aChar); + } + } + } +} From a074218db0a1732da5e7814ea8e21e14862be589 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 17:53:56 +0800 Subject: [PATCH 113/115] =?UTF-8?q?HJ14.=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ14/Main.java | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/java/od/HJ14/Main.java diff --git a/README.md b/README.md index 36858fc..473464a 100644 --- a/README.md +++ b/README.md @@ -370,6 +370,7 @@ - [HJ11.数字颠倒](/src/main/java/od/HJ11/Main.java) - [HJ12.字符串反转](/src/main/java/od/HJ12/Main.java) - [HJ13.句子逆序](/src/main/java/od/HJ13/Main.java) +- [HJ14.字符串排序](/src/main/java/od/HJ14/Main.java) ## PAT diff --git a/src/main/java/od/HJ14/Main.java b/src/main/java/od/HJ14/Main.java new file mode 100644 index 0000000..ea2fbe6 --- /dev/null +++ b/src/main/java/od/HJ14/Main.java @@ -0,0 +1,25 @@ +package od.HJ14; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日17:12:31 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n = in.nextInt(); + ArrayList list = new ArrayList(); + for (int i = 0; i < n; i++) { + String str = in.next(); + list.add(str); + } + list.sort(Comparator.naturalOrder()); + for (String s : list) { + System.out.println(s); + } + } +} From 0548047d6aedff4ef197293781129355c9016b3f Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 18:09:06 +0800 Subject: [PATCH 114/115] =?UTF-8?q?HJ15.=E6=B1=82int=E5=9E=8B=E6=AD=A3?= =?UTF-8?q?=E6=95=B4=E6=95=B0=E5=9C=A8=E5=86=85=E5=AD=98=E4=B8=AD=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E6=97=B61=E7=9A=84=E4=B8=AA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ15/Main.java | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 src/main/java/od/HJ15/Main.java diff --git a/README.md b/README.md index 473464a..fc29836 100644 --- a/README.md +++ b/README.md @@ -371,6 +371,7 @@ - [HJ12.字符串反转](/src/main/java/od/HJ12/Main.java) - [HJ13.句子逆序](/src/main/java/od/HJ13/Main.java) - [HJ14.字符串排序](/src/main/java/od/HJ14/Main.java) +- [**HJ15.求int型正整数在内存中存储时1的个数**](/src/main/java/od/HJ15/Main.java) ## PAT diff --git a/src/main/java/od/HJ15/Main.java b/src/main/java/od/HJ15/Main.java new file mode 100644 index 0000000..d8672af --- /dev/null +++ b/src/main/java/od/HJ15/Main.java @@ -0,0 +1,20 @@ +package od.HJ15; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日17:55:29 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int n = in.nextInt(); + int count = 0; + while (n != 0) { + n = n & (n-1); + count++; + } + System.out.println(count); + } +} From b7520d0c4894dc3084e00c681e7833e89467cd61 Mon Sep 17 00:00:00 2001 From: yangyi Date: 2023年8月29日 18:41:14 +0800 Subject: [PATCH 115/115] =?UTF-8?q?HJ106.=E5=AD=97=E7=AC=A6=E9=80=86?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + src/main/java/od/HJ106/Main.java | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/main/java/od/HJ106/Main.java diff --git a/README.md b/README.md index fc29836..880ae49 100644 --- a/README.md +++ b/README.md @@ -372,6 +372,7 @@ - [HJ13.句子逆序](/src/main/java/od/HJ13/Main.java) - [HJ14.字符串排序](/src/main/java/od/HJ14/Main.java) - [**HJ15.求int型正整数在内存中存储时1的个数**](/src/main/java/od/HJ15/Main.java) +- [HJ106.字符逆序](/src/main/java/od/HJ106/Main.java) ## PAT diff --git a/src/main/java/od/HJ106/Main.java b/src/main/java/od/HJ106/Main.java new file mode 100644 index 0000000..51bbfa3 --- /dev/null +++ b/src/main/java/od/HJ106/Main.java @@ -0,0 +1,27 @@ +package od.HJ106; + +import java.util.Scanner; + +/** + * @author yangyi 2023年08月29日18:40:12 + */ +class Main { + + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + String n = in.nextLine(); + int start = 0; + int end = n.length() - 1; + char[] chars = n.toCharArray(); + while (start < end) { + char temp = chars[start]; + chars[start] = chars[end]; + chars[end] = temp; + start++; + end--; + } + for (char aChar : chars) { + System.out.print(aChar); + } + } +}

AltStyle によって変換されたページ (->オリジナル) /