From ac71ac43bcc9437839d0da2fc4615eca6b34a31e Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 5 Sep 2021 08:32:17 -0400 Subject: [PATCH 1/5] =?UTF-8?q?Update=200042.=E6=8E=A5=E9=9B=A8=E6=B0=B4.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Java 单调栈实现 --- problems/0042.接雨水.md | 47 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 522b10f9..17c671ef 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -391,6 +391,7 @@ class Solution { } } ``` + 动态规划法 ```java class Solution { @@ -418,6 +419,52 @@ class Solution { } } ``` + +单调栈法 +```java + public int trapMonoStack(int[] height){ + int size = height.length; + + if (size <= 2) return 0; + + // in the stack, we push the index of array + // using height[] to access the real height + Stack stack = new Stack(); + stack.push(0); + + int sum = 0; + for (int index = 1; index < size; index++){ + int stackTop = stack.peek(); + if (height[index] < height[stackTop]){ + stack.push(index); + }else if (height[index] == height[stackTop]){ + // 因为相等的相邻墙,左边一个是不可能存放雨水的,所以pop左边的index, push当前的index + stack.pop(); + stack.push(index); + }else{ + //pop up all lower value + int heightAtIdx = height[index]; + while (!stack.isEmpty() && (heightAtIdx > height[stackTop])){ + int mid = stack.pop(); + + if (!stack.isEmpty()){ + int left = stack.peek(); + + int h = Math.min(height[left], height[index]) - height[mid]; + int w = index - left - 1; + int hold = h * w; + if (hold > 0) sum += hold; + stackTop = stack.peek(); + } + } + stack.push(index); + } + } + + return sum; + } +``` + Python: 双指针法 From 8d1c4d65ffaf633ac3ffe17430bee6d7e5759829 Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 5 Sep 2021 09:17:11 -0400 Subject: [PATCH 2/5] =?UTF-8?q?Update=200084.=E6=9F=B1=E7=8A=B6=E5=9B=BE?= =?UTF-8?q?=E4=B8=AD=E6=9C=80=E5=A4=A7=E7=9A=84=E7=9F=A9=E5=BD=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加java 单调栈实现 --- problems/0084.柱状图中最大的矩形.md | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0084.柱状图中最大的矩形.md b/problems/0084.柱状图中最大的矩形.md index a0f06e8f..57df4161 100644 --- a/problems/0084.柱状图中最大的矩形.md +++ b/problems/0084.柱状图中最大的矩形.md @@ -228,6 +228,50 @@ class Solution { } ``` +单调栈 +```java +class Solution { + int largestRectangleArea(int[] heights) { + Stack st = new Stack(); + + // 数组扩容,在头和尾各加入一个元素 + int [] newHeights = new int[heights.length + 2]; + newHeights[0] = 0; + newHeights[newHeights.length - 1] = 0; + for (int index = 0; index < heights.length; index++){ + newHeights[index + 1] = heights[index]; + } + + heights = newHeights; + + st.push(0); + int result = 0; + // 第一个元素已经入栈,从下表1开始 + for (int i = 1; i < heights.length; i++) { + // 注意heights[i] 是和heights[st.top()] 比较 ,st.top()是下表 + if (heights[i] > heights[st.peek()]) { + st.push(i); + } else if (heights[i] == heights[st.peek()]) { + st.pop(); // 这个可以加,可以不加,效果一样,思路不同 + st.push(i); + } else { + while (heights[i] < heights[st.peek()]) { // 注意是while + int mid = st.peek(); + st.pop(); + int left = st.peek(); + int right = i; + int w = right - left - 1; + int h = heights[mid]; + result = Math.max(result, w * h); + } + st.push(i); + } + } + return result; + } +} +``` + Python: 动态规划 From 69441475c6f5d037d1eff9a4fd46c4b1913f1ff9 Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 5 Sep 2021 09:18:34 -0400 Subject: [PATCH 3/5] =?UTF-8?q?Update=200042.=E6=8E=A5=E9=9B=A8=E6=B0=B4.m?= =?UTF-8?q?d?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改代码格式 --- problems/0042.接雨水.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/problems/0042.接雨水.md b/problems/0042.接雨水.md index 17c671ef..4383a0b8 100644 --- a/problems/0042.接雨水.md +++ b/problems/0042.接雨水.md @@ -422,7 +422,8 @@ class Solution { 单调栈法 ```java - public int trapMonoStack(int[] height){ +class Solution { + public int trap(int[] height){ int size = height.length; if (size <= 2) return 0; @@ -463,6 +464,7 @@ class Solution { return sum; } +} ``` Python: From adafd3993154d5371edebd2406c880afd88d3b3b Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 5 Sep 2021 09:44:53 -0400 Subject: [PATCH 4/5] =?UTF-8?q?Update=201356.=E6=A0=B9=E6=8D=AE=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E4=BA=8C=E8=BF=9B=E5=88=B6=E4=B8=8B1=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=9B=AE=E6=8E=92=E5=BA=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加Java实现 --- ...据数字二进制下1的数目排序.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md index 660434a2..40a49b9c 100644 --- a/problems/1356.根据数字二进制下1的数目排序.md +++ b/problems/1356.根据数字二进制下1的数目排序.md @@ -123,6 +123,32 @@ public: ## Java ```java +class Solution { + private int cntInt(int val){ + int count = 0; + while(val > 0) { + val = val & (val - 1); + count ++; + } + + return count; + } + + public int[] sortByBits(int[] arr) { + return Arrays.stream(arr).boxed() + .sorted(new Comparator(){ + @Override + public int compare(Integer o1, Integer o2) { + // TODO Auto-generated method stub + int cnt1 = cntInt(o1); + int cnt2 = cntInt(o2); + return (cnt1 == cnt2) ? Integer.compare(o1, o2) : Integer.compare(cnt1, cnt2); + } + }) + .mapToInt(Integer::intValue) + .toArray(); + } +} ``` From d6f5a785871fc8918da32f1727e92021f9ccba23 Mon Sep 17 00:00:00 2001 From: hailincai Date: Sun, 5 Sep 2021 09:45:39 -0400 Subject: [PATCH 5/5] =?UTF-8?q?Update=201356.=E6=A0=B9=E6=8D=AE=E6=95=B0?= =?UTF-8?q?=E5=AD=97=E4=BA=8C=E8=BF=9B=E5=88=B6=E4=B8=8B1=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E7=9B=AE=E6=8E=92=E5=BA=8F.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit remove TODO for comparator --- problems/1356.根据数字二进制下1的数目排序.md | 1 - 1 file changed, 1 deletion(-) diff --git a/problems/1356.根据数字二进制下1的数目排序.md b/problems/1356.根据数字二进制下1的数目排序.md index 40a49b9c..06c29500 100644 --- a/problems/1356.根据数字二进制下1的数目排序.md +++ b/problems/1356.根据数字二进制下1的数目排序.md @@ -139,7 +139,6 @@ class Solution { .sorted(new Comparator(){ @Override public int compare(Integer o1, Integer o2) { - // TODO Auto-generated method stub int cnt1 = cntInt(o1); int cnt2 = cntInt(o2); return (cnt1 == cnt2) ? Integer.compare(o1, o2) : Integer.compare(cnt1, cnt2);