diff --git a/Algorithm/StackAndQueue/20_valid-parentheses/247.java b/Algorithm/StackAndQueue/20_valid-parentheses/247.java new file mode 100644 index 0000000..f0a6898 --- /dev/null +++ b/Algorithm/StackAndQueue/20_valid-parentheses/247.java @@ -0,0 +1,59 @@ +/** + * 第一种写法利用字符串的替换,把括号都去掉,如果最后不为空字符串,证明字符串不匹配 + * 时间复杂度O(n) + * 空间复杂度:因为replace的api里调用了tostring,所以每次都会new一个string对象,算O(n)吧 + */ +class Solution { + public boolean isValid(String s) { + if (s == null) + return true; + else { + while (s.length() > 0 && (s.indexOf("()") >= 0 || s.indexOf("[]") >= 0 || s.indexOf("{}") >= 0)) { + s = s.replace("()", ""); + s = s.replace("[]", ""); + s = s.replace("{}", ""); + } + if (s.length() > 0) + return false; + else + return true; + } + + + } + +} + +/** + * 第二种就是常规思路,利用栈,左括号入栈,如果右括号就看栈顶的是不是对应的左括号,不是的话立刻就可以判false。一直处理完string,判断栈是否为空。 + * 时间复杂度 O(N) + * 空间复杂度 O(N) + */ +class Solution { + public boolean isValid(String s) { + char[] array = s.toCharArray(); + Stack stack = new Stack<>(); + for (int i = 0; i < array.length; i++) { + if (array[i] == '{' || array[i] == '[' || array[i] == '(') { + stack.push(array[i]); + + } + if (array[i] == '}') { + if (stack.isEmpty() || stack.peek() != '{') + return false; + stack.pop(); + } else if (array[i] == ')') { + if (stack.isEmpty() || stack.peek() != '(') + return false; + stack.pop(); + } else if (array[i] == ']') { + if (stack.isEmpty() || stack.peek() != '[') + return false; + stack.pop(); + } + } + return stack.isEmpty(); + + } +} + diff --git a/Algorithm/StackAndQueue/225_implement-stack-using-queues/247.java b/Algorithm/StackAndQueue/225_implement-stack-using-queues/247.java new file mode 100644 index 0000000..9c4d8c7 --- /dev/null +++ b/Algorithm/StackAndQueue/225_implement-stack-using-queues/247.java @@ -0,0 +1,58 @@ +import java.util.LinkedList; +import java.util.Queue; + +/** + * 队列a是暂时的操作队列,队列b是排好逆序的队列。 + * 时间复杂度,入栈o(n),其他操作O(1) + * 空间复杂度o(n),两个队列 + */ +class MyStack { + private Queue queuea; + private Queue queueb; + + /** Initialize your data structure here. */ + public MyStack() { + queuea = new LinkedList<>(); + queueb = new LinkedList<>(); + + } + + /** Push element x onto stack. */ + public void push(int x) { + queuea.add(x); + while (!queueb.isEmpty()) + queuea.add(queueb.poll()); + + Queue temp = queuea; + queuea = queueb; + queueb = temp; + } + + public int pop() { + return queueb.poll(); + } + + /** + * Get the top element. + */ + public int top() { + return queueb.peek(); + + } + + /** + * Returns whether the stack is empty. + */ + public boolean empty() { + return queueb.isEmpty(); + } +} + +/** + * Your MyStack object will be instantiated and called as such: + * MyStack obj = new MyStack(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.top(); + * boolean param_4 = obj.empty(); + */ \ No newline at end of file diff --git a/Algorithm/StackAndQueue/232_implement-queue-using-stacks/247.java b/Algorithm/StackAndQueue/232_implement-queue-using-stacks/247.java new file mode 100644 index 0000000..de9d221 --- /dev/null +++ b/Algorithm/StackAndQueue/232_implement-queue-using-stacks/247.java @@ -0,0 +1,78 @@ +/** + * s1 输入栈,s2输出栈 + * 时间复杂度,push,empty O(1),pop peek o(n) + * 空间复杂度 o(n) + */ +class MyQueue { + + Stack s1 ; + Stack s2 ; + + + /** + * Initialize your data structure here. + */ + public MyQueue() { + s1 = new Stack<>(); + s2 = new Stack<>(); + } + + /** + * Push element x to the back of queue. + */ + public void push(int x) { + s1.push(x); + + } + + /** + * Removes the element from in front of queue and returns that element. + */ + public int pop() { + while (!s1.isEmpty()) { + s2.push(s1.pop()); + } + int res = s2.peek(); + s2.pop(); + while (!s2.isEmpty()) { + s1.push(s2.pop()); + + } + return res; + + } + + /** + * Get the front element. + */ + public int peek() { + while (!s1.isEmpty()) { + s2.push(s1.pop()); + } + int res = s2.peek(); + while (!s2.isEmpty()) { + s1.push(s2.pop()); + + } + return res; + } + + + /** + * Returns whether the queue is empty. + */ + public boolean empty() { + + return s1.isEmpty(); + + } +} + +/** + * Your MyQueue object will be instantiated and called as such: + * MyQueue obj = new MyQueue(); + * obj.push(x); + * int param_2 = obj.pop(); + * int param_3 = obj.peek(); + * boolean param_4 = obj.empty(); + */ \ No newline at end of file diff --git a/Algorithm/StackAndQueue/703_kth-largest-element-in-a-stream/247.java b/Algorithm/StackAndQueue/703_kth-largest-element-in-a-stream/247.java new file mode 100644 index 0000000..7c6bf89 --- /dev/null +++ b/Algorithm/StackAndQueue/703_kth-largest-element-in-a-stream/247.java @@ -0,0 +1,39 @@ +/** + * 用最小堆。堆顶元素是最小的,堆底的都比他大。所以第k大,就维护一个k大小的最小堆。 + * 时间复杂度 o(logk)因为是树 + * 空间复杂度 o(k) + */ +class KthLargest { + private PriorityQueue queue; + private Integer n; + + public KthLargest(int k, int[] nums) { + n = k; + queue = new PriorityQueue<>(k); + + for (int i = 0; i < nums.length; i++) { + add(nums[i]); + } + + } + + public int add(int val) { + if (queue.size() < n) { + + queue.add(val); + } + else if (val > queue.peek()){ + queue.poll(); + queue.add(val); + } + + + return queue.peek(); + } +} + +/** + * Your KthLargest object will be instantiated and called as such: + * KthLargest obj = new KthLargest(k, nums); + * int param_1 = obj.add(val); + */ \ No newline at end of file