From f26b63e3069be512729377b5f77624c02a1aefc1 Mon Sep 17 00:00:00 2001 From: h2linlin Date: Thu, 13 May 2021 20:05:10 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200027.=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E5=85=83=E7=B4=A0.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0027.移除元素.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 959474fc..55c1a1dc 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -123,6 +123,22 @@ public: Java: +```java +class Solution { + public int removeElement(int[] nums, int val) { + int p2 = 0; + + for (int p1 = 0; p1 < nums.length; p1++) { + if (nums[p1] != val) { + nums[p2] = nums[p1]; + p2++; + } + } + + return p2; + } +} +``` Python: @@ -172,4 +188,4 @@ var removeElement = (nums, val) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
From 137d509180933a5f8cfa50ee20276bc06794e4b2 Mon Sep 17 00:00:00 2001 From: h2linlin Date: Thu, 13 May 2021 20:21:03 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200142.=E7=8E=AF?= =?UTF-8?q?=E5=BD=A2=E9=93=BE=E8=A1=A8II.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0142.环形链表II.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index 9622affc..cd6347ef 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -186,7 +186,35 @@ public: Java: +```java +public class Solution { + public ListNode detectCycle(ListNode head) { + // 1.寻找相遇点 + ListNode fast = head; + ListNode slow = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + if (fast != slow) { + continue; + } + ListNode meet = fast; + + // 2.寻找入口点 + slow = head; + while (slow != fast) { + slow = slow.next; + fast = fast.next; + } + + return fast; + } + + return null; + } +} +``` Python: From 4a08e044706f91d2eb268c05ee43d26298cd24e7 Mon Sep 17 00:00:00 2001 From: h2linlin Date: Thu, 13 May 2021 20:44:47 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200151.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=87=8C=E7=9A=84=E5=8D=95?= =?UTF-8?q?=E8=AF=8D.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0151.翻转字符串里的单词.md | 71 ++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index d81c139d..13938fd9 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -212,6 +212,77 @@ public: Java: +```java +class Solution { + public String reverseWords(String s) { + char[] chars = s.toCharArray(); + // 1.双指针去除空格 + // 去除头尾空格 + int head = 0; + while (chars[head] == ' ') { + head ++; + } + int tail = chars.length - 1; + while (chars[tail] == ' ') { + tail --; + } + + // 去除中间空格 + int p1 = head; + int p2 = head + 1; + while (p2 <= tail) { + if (chars[p2 -1] == ' ' && chars[p2] == ' ') { + p2 ++; + continue; + } + chars[p1 + 1] = chars[p2]; + p1 ++; + p2 ++; + } + + tail = p1; + + // 2.双指针翻转整个字符串 + chars = reverse(chars, head, tail); + + // 3.双指针翻转每个单词 + p1 = head; + p2 = head; + + for (int i = head; i <= tail;) { + int add = 0; + while (i + add <= tail && chars[i + add] != ' ') { + add ++; + } + + if (i + add != tail) { + reverse(chars, i, i + add - 1); + i += add + 1; + } else { + reverse(chars, i, tail); + i = tail + 1; + } + } + + return new String(chars, head, tail - head + 1); + } + + char[] reverse(char[] chars, int begin, int end) { + int p1 = begin; + int p2 = end; + + while (p1 < p2) { + chars[p1] ^= chars[p2]; + chars[p2] ^= chars[p1]; + chars[p1] ^= chars[p2]; + + p1 ++; + p2 --; + } + return chars; + } +} +``` Python: From 7bda59c0623ff7f37f0bb4d67db44eede5b05543 Mon Sep 17 00:00:00 2001 From: h2linlin Date: Thu, 13 May 2021 20:52:01 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200101.=E5=AF=B9?= =?UTF-8?q?=E7=A7=B0=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Java=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0101.对称二叉树.md | 89 +++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 535300af..05a67623 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -254,7 +254,94 @@ public: ## 其他语言版本 Java: +```java +public class N0101 { + /** + * 解法1:DFS,递归。 + */ + public boolean isSymmetric2(TreeNode root) { + if (root == null) { + return false; + } + return compare(root.left, root.right); + } + + private boolean compare(TreeNode left, TreeNode right) { + if (left == null && right == null) { + return true; + } + if (left != null && right == null) { + return false; + } + if (left == null && right != null) { + return false; + } + + if (left.val == right.val) { + return compare(left.left, right.right) && compare(left.right, right.left); + } + + return false; + } + + /** + * 解法2:DFS,迭代 + */ + public boolean isSymmetric3(TreeNode root) { + if (root == null) { + return false; + } + + if (!equal(root.left, root.right)) { + return false; + } + + Deque st = new LinkedList<>(); + + st.push(root.right); + st.push(root.left); + + TreeNode curR = root.right; + TreeNode curL = root.left; + + while (!st.isEmpty()) { + curL = st.pop(); + curR = st.pop(); + + // 前序,处理 + if (!equal(curL, curR)) { + return false; + } + + if (curR != null && curL != null) { + st.push(curL.right); + st.push(curR.left); + st.push(curR.right); + st.push(curL.left); + } + } + + return true; + } + + private boolean equal(TreeNode l, TreeNode r) { + if (l == null && r == null) { + return true; + } + if (l != null && r == null) { + return false; + } + if (l == null && r != null) { + return false; + } + if (l.val == r.val) { + return true; + } + return false; + } +} +``` Python: @@ -283,4 +370,4 @@ const check = (leftPtr, rightPtr) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file +
From fddb778dbee09ea7fad6e204ce52d326be8f4d98 Mon Sep 17 00:00:00 2001 From: h2linlin Date: Thu, 13 May 2021 21:25:03 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200102.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E5=B1=82=E5=BA=8F=E9=81=8D=E5=8E=86?= =?UTF-8?q?.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0102.二叉树的层序遍历.md | 228 +++++++++++++++++++--- 1 file changed, 205 insertions(+), 23 deletions(-) diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 0781e01d..9b5f9ed5 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -420,54 +420,236 @@ public: Java: ```Java +// 102.二叉树的层序遍历 class Solution { - public List> resList=new ArrayList>(); + public List> resList = new ArrayList>(); + public List> levelOrder(TreeNode root) { //checkFun01(root,0); checkFun02(root); - + return resList; } - //DFS--递归方式 - public void checkFun01(TreeNode node,Integer deep){ - if(node==null) return; + //DFS--递归方式 + public void checkFun01(TreeNode node, Integer deep) { + if (node == null) return; deep++; - if(resList.size() item=new ArrayList(); + List item = new ArrayList(); resList.add(item); } - resList.get(deep-1).add(node.val); - - checkFun01(node.left,deep); - checkFun01(node.right,deep); + resList.get(deep - 1).add(node.val); + + checkFun01(node.left, deep); + checkFun01(node.right, deep); } - + //BFS--迭代方式--借助队列 - public void checkFun02(TreeNode node){ - if(node==null) return; - Queue que=new LinkedList(); + public void checkFun02(TreeNode node) { + if (node == null) return; + Queue que = new LinkedList(); que.offer(node); - while(!que.isEmpty()){ - List itemList=new ArrayList(); - int len=que.size(); + while (!que.isEmpty()) { + List itemList = new ArrayList(); + int len = que.size(); - while(len>0){ - TreeNode tmpNode=que.poll(); + while (len > 0) { + TreeNode tmpNode = que.poll(); itemList.add(tmpNode.val); - if(tmpNode.left!=null) que.offer(tmpNode.left); - if(tmpNode.right!=null) que.offer(tmpNode.right); + if (tmpNode.left != null) que.offer(tmpNode.left); + if (tmpNode.right != null) que.offer(tmpNode.right); len--; } - + resList.add(itemList); } } +} + + +// 107. 二叉树的层序遍历 II +public class N0107 { + + /** + * 解法:队列,迭代。 + * 层序遍历,再翻转数组即可。 + */ + public List> solution1(TreeNode root) { + List> list = new ArrayList<>(); + Deque que = new LinkedList<>(); + + if (root == null) { + return list; + } + + que.offerLast(root); + while (!que.isEmpty()) { + List levelList = new ArrayList<>(); + + int levelSize = que.size(); + for (int i = 0; i < levelSize; i++) { + TreeNode peek = que.peekFirst(); + levelList.add(que.pollFirst().val); + + if (peek.left != null) { + que.offerLast(peek.left); + } + if (peek.right != null) { + que.offerLast(peek.right); + } + } + list.add(levelList); + } + + List> result = new ArrayList<>(); + for (int i = list.size() - 1; i >= 0; i-- ) { + result.add(list.get(i)); + } + + return result; + } +} + +// 199.二叉树的右视图 +public class N0199 { + /** + * 解法:队列,迭代。 + * 每次返回每层的最后一个字段即可。 + * + * 小优化:每层右孩子先入队。代码略。 + */ + public List rightSideView(TreeNode root) { + List list = new ArrayList<>(); + Deque que = new LinkedList<>(); + + if (root == null) { + return list; + } + + que.offerLast(root); + while (!que.isEmpty()) { + int levelSize = que.size(); + + for (int i = 0; i < levelSize; i++) { + TreeNode poll = que.pollFirst(); + + if (poll.left != null) { + que.addLast(poll.left); + } + if (poll.right != null) { + que.addLast(poll.right); + } + + if (i == levelSize - 1) { + list.add(poll.val); + } + } + } + + return list; + } +} + +// 637. 二叉树的层平均值 +public class N0637 { + + /** + * 解法:队列,迭代。 + * 每次返回每层的最后一个字段即可。 + */ + public List averageOfLevels(TreeNode root) { + List list = new ArrayList<>(); + Deque que = new LinkedList<>(); + + if (root == null) { + return list; + } + + que.offerLast(root); + while (!que.isEmpty()) { + TreeNode peek = que.peekFirst(); + + int levelSize = que.size(); + double levelSum = 0.0; + for (int i = 0; i < levelSize; i++) { + TreeNode poll = que.pollFirst(); + + levelSum += poll.val; + + if (poll.left != null) { + que.addLast(poll.left); + } + if (poll.right != null) { + que.addLast(poll.right); + } + } + list.add(levelSum / levelSize); + } + return list; + } +} + +// 429. N 叉树的层序遍历 +public class N0429 { + /** + * 解法1:队列,迭代。 + */ + public List> levelOrder(Node root) { + List> list = new ArrayList<>(); + Deque que = new LinkedList<>(); + + if (root == null) { + return list; + } + + que.offerLast(root); + while (!que.isEmpty()) { + int levelSize = que.size(); + List levelList = new ArrayList<>(); + + for (int i = 0; i < levelSize; i++) { + Node poll = que.pollFirst(); + + levelList.add(poll.val); + + List children = poll.children; + if (children == null || children.size() == 0) { + continue; + } + for (Node child : children) { + if (child != null) { + que.offerLast(child); + } + } + } + list.add(levelList); + } + + return list; + } + + class Node { + public int val; + public List children; + + public Node() {} + + public Node(int _val) { + val = _val; + } + + public Node(int _val, List _children) { + val = _val; + children = _children; + } + } +} ``` From f6b32b55b37657654221cae8626dfaaeb99d1389 Mon Sep 17 00:00:00 2001 From: h2linlin Date: Thu, 13 May 2021 21:59:23 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200347.=E5=89=8D=20K=20?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.md=20Java?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0347.前K个高频元素.md | 52 ++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/problems/0347.前K个高频元素.md b/problems/0347.前K个高频元素.md index 1902bd68..9ec601a7 100644 --- a/problems/0347.前K个高频元素.md +++ b/problems/0347.前K个高频元素.md @@ -133,7 +133,59 @@ public: Java: +```java +public class N0347 { + /** + * 解法: + * 1.统计频次。O(n) + * 2.排序。O(nlogn) + * 3.取前K个元素 + * + * 注意到,排序的时候需要用小顶堆,而不是大顶堆。因为每次需要把最小的堆顶弹出去,最后才剩下最大的k个。 + * 时间复杂度:O(n + nlogk) = O(nlogk) + */ + public int[] topKFrequent(int[] nums, int k) { + HashMap map = statistic(nums); + PriorityQueue pq = new PriorityQueue<>(new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return map.get(o1) - map.get(o2); + } + }); + + for (Integer key : map.keySet()) { + if (pq.size() < k) { + pq.offer(key); + } else if (map.get(pq.peek()) < map.get(key)){ + pq.poll(); + pq.offer(key); + } + } + + int[] result = new int[k]; + for (int i = 0; i < k; i++) { + result[i] = pq.poll(); + } + return result; + } + + + // key: 值,value:次数 + private HashMap statistic(int[] nums) { + HashMap map = new HashMap<>(); + for (int num : nums) { + if (map.get(num) == null) { + map.put(num, 1); + } else { + map.put(num, map.get(num) + 1); + } + } + + return map; + } +} +``` Python: