diff --git a/Week 01/id_436/Leetcode_26_436.java b/Week 01/id_436/Leetcode_26_436.java new file mode 100644 index 000000000..a14498af7 --- /dev/null +++ b/Week 01/id_436/Leetcode_26_436.java @@ -0,0 +1,24 @@ + +/** + * 在这里给出对类 Leetcode_26_436 的描述。 + * + * @作者(你的名字) + * @版本(一个版本号或者一个日期) + */ +public class Leetcode_26_436 +{ + public int removeDuplicates(int[] nums) { + if(nums.length == 0) + return 0; + int result = 1; + int j = 0;// previous pointer; + for(int i = 0; i < nums.length; i++){ + if(nums[j] != nums[i]){ + result++; + j++; + nums[j] = nums[i]; + } + } + return result; + } +} diff --git a/Week 01/id_436/Leetcode_42_436.java b/Week 01/id_436/Leetcode_42_436.java new file mode 100644 index 000000000..76309d0d1 --- /dev/null +++ b/Week 01/id_436/Leetcode_42_436.java @@ -0,0 +1,28 @@ +/** + * 在这里给出对类 Leetcode_42_436 的描述。 + * + * @作者(你的名字) + * @版本(一个版本号或者一个日期) + */ +import java.util.*; +public class Leetcode_42_436 +{ + public int trap(int[] height){ + int n = height.length; + Deque stack = new ArrayDeque<>(); + int result = 0; + int index = 0; + while(index < n){ + while(!stack.isEmpty() && height[index] > height[stack.peek()]){ + int top = stack.pop(); + if(stack.isEmpty()) break; + int h = Math.min(height[stack.peek()], height[index]) - height[top]; + int dist = index - stack.peek() - 1; + result += dist * h; + } + stack.push(index); + index++; + } + return result; + } +}