From ac40cc8be29fdbf22b2b6a5b889315de74f602c3 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Wed, 23 Jun 2021 10:24:40 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix(1047):=20=E6=96=B0=E5=A2=9E=E5=8F=8C?= =?UTF-8?q?=E6=8C=87=E9=92=88java=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...除字符串中的所有相邻重复项.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 305a287d..4f13cda3 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -171,6 +171,29 @@ class Solution { } ``` +拓展:双指针 +```java +class Solution { + public String removeDuplicates(String s) { + char[] ch = s.toCharArray(); + int fast = 0; + int slow = 0; + while(fast < s.length()){ + // 直接用fast指针覆盖slow指针的值 + ch[slow] = ch[fast]; + // 遇到前后相同值的,就跳过,即slow指针后退一步,下次循环就可以直接被覆盖掉了 + if(slow > 0 && ch[slow] == ch[slow - 1]){ + slow--; + }else{ + slow++; + } + fast++; + } + return new String(ch,0,slow); + } +} +``` + Python: ```python3 class Solution: From 625558094da614c5f9a2b358f3291bf102f7e1a7 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Wed, 23 Jun 2021 10:31:45 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix(1047):=20=E4=BD=BF=E7=94=A8ArrayDeque?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/1047.删除字符串中的所有相邻重复项.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 4f13cda3..760a0946 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -127,7 +127,9 @@ Java: ```Java class Solution { public String removeDuplicates(String S) { - Deque deque = new LinkedList<>(); + //ArrayDeque会比LinkedList在除了删除元素这一点外会快一点 + //参考:https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist + ArrayDeque deque = new ArrayDeque<>(); char ch; for (int i = 0; i < S.length(); i++) { ch = S.charAt(i); From a150c61f9db97521b2b758affe64e202d6a821d4 Mon Sep 17 00:00:00 2001 From: KailokFung Date: Wed, 23 Jun 2021 18:01:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix(0239):=20=E6=96=B0=E5=A2=9E=E4=B8=80?= =?UTF-8?q?=E7=A7=8Djava=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0239.滑动窗口最大值.md | 35 ++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md index d108335b..ad54a940 100644 --- a/problems/0239.滑动窗口最大值.md +++ b/problems/0239.滑动窗口最大值.md @@ -208,6 +208,7 @@ public: Java: ```Java +//解法一 //自定义数组 class MyQueue { Deque deque = new LinkedList<>(); @@ -260,6 +261,40 @@ class Solution { return res; } } + +//解法二 +//利用双端队列手动实现单调队列 +/** + * 用一个单调队列来存储对应的下标,每当窗口滑动的时候,直接取队列的头部指针对应的值放入结果集即可 + * 单调队列类似 (tail -->) 3 --> 2 --> 1 --> 0 (--> head) (右边为头结点,元素存的是下标) + */ +class Solution { + public int[] maxSlidingWindow(int[] nums, int k) { + ArrayDeque deque = new ArrayDeque<>(); + int n = nums.length; + int[] res = new int[n - k + 1]; + int idx = 0; + for(int i = 0; i < n; i++) { + // 根据题意,i为nums下标,是要在[i - k + 1, k] 中选到最大值,只需要保证两点 + // 1.队列头结点需要在[i - k + 1, k]范围内,不符合则要弹出 + while(!deque.isEmpty() && deque.peek() < i - k + 1){ + deque.poll(); + } + // 2.既然是单调,就要保证每次放进去的数字要比末尾的都大,否则也弹出 + while(!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) { + deque.pollLast(); + } + + deque.offer(i); + + // 因为单调,当i增长到符合第一个k范围的时候,每滑动一步都将队列头节点放入结果就行了 + if(i >= k - 1){ + res[idx++] = nums[deque.peek()]; + } + } + return res; + } +} ``` Python: