力扣:面试题30:包含min函数的栈

This commit is contained in:
huangge1199 2021-04-01 14:59:09 +08:00
parent ea952085f8
commit e3ddb8c3e7
8 changed files with 227 additions and 6 deletions

View File

@ -0,0 +1,85 @@
//定义栈的数据结构请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中调用 minpush pop 的时间复杂度都是 O(1)
//
//
//
// 示例:
//
// MinStack minStack = new MinStack();
//minStack.push(-2);
//minStack.push(0);
//minStack.push(-3);
//minStack.min(); --> 返回 -3.
//minStack.pop();
//minStack.top(); --> 返回 0.
//minStack.min(); --> 返回 -2.
//
//
//
//
// 提示
//
//
// 各函数的调用总次数不超过 20000
//
//
//
//
// 注意本题与主站 155 题相同https://leetcode-cn.com/problems/min-stack/
// Related Topics 设计
// 👍 114 👎 0
package leetcode.editor.cn;
import java.util.Stack;
//面试题30:包含min函数的栈
public class BaoHanMinhanShuDeZhanLcof{
public static void main(String[] args) {
//测试代码
// Solution solution = new BaoHanMinhanShuDeZhanLcof().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MinStack {
Stack<Integer> stack;
Stack<Integer> min;
public MinStack() {
stack = new Stack<>();
min = new Stack<>();
}
public void push(int val) {
if (stack.isEmpty()) {
min.push(val);
} else {
min.push(Math.min(min.peek(), val));
}
stack.push(val);
}
public void pop() {
stack.pop();
min.pop();
}
public int top() {
return stack.peek();
}
public int getMin() {
return min.peek();
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.min();
*/
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,28 @@
<p>定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。</p>
<p>&nbsp;</p>
<p><strong>示例:</strong></p>
<pre>MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --&gt; 返回 -3.
minStack.pop();
minStack.top(); --&gt; 返回 0.
minStack.min(); --&gt; 返回 -2.
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ol>
<li>各函数的调用总次数不超过 20000 次</li>
</ol>
<p>&nbsp;</p>
<p>注意:本题与主站 155 题相同:<a href="https://leetcode-cn.com/problems/min-stack/">https://leetcode-cn.com/problems/min-stack/</a></p>
<div><div>Related Topics</div><div><li></li><li>设计</li></div></div>\n<div><li>👍 114</li><li>👎 0</li></div>

View File

@ -68,7 +68,7 @@ public class FuZaLianBiaoDeFuZhiLcof {
List<Integer> var = Arrays.asList(7, 13, 11, 10, 1);
List<Integer> random = Arrays.asList(null, 0, 4, 2, 0);
Node head = new Node(0);
head = head.setHead(var, random);
// head = head.setHead(var, random);
solution.copyRandomList(head);
}

View File

@ -47,13 +47,13 @@ import java.util.Stack;
public class MinStack {
public static void main(String[] args) {
//测试代码
// Solution solution = new MinStack().new Solution();
// Solution solution = new MinStack1().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class MinStack {
class MinStack1 {
// Stack<Integer> stack;
// Stack<Integer> min;
// public MinStack() {
@ -101,7 +101,7 @@ public class MinStack {
}
Stack<Data> stack;
public MinStack() {
public MinStack1() {
stack = new Stack<>();
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,60 @@
![图解每日一练.jpg](https://pic.leetcode-cn.com/1615817903-fzmpwZ-%E5%9B%BE%E8%A7%A3%E6%AF%8F%E6%97%A5%E4%B8%80%E7%BB%83.jpg)
---
### 🧠 解题思路
我们解决这道题的关键在于,需要知道哪些是需要去除的外层括号,为了找到这些需要去除的外层括号,我们可以使用到计数器。
**规则:** 遇到左括号,我们的计数器 *+1*,遇到右括号,我们的计数器 *-1*
这样的话,一组连续且有效的括号,将不会对计数器的值产生变化。
```js
// 示例一
当前的计数值: 0 1 0 1
( ) ( )
遍历后计数值: 1 0 1 0
// 示例二
当前的计数值: 0 1 2 1 2 1 0 1
( ( ) ( ) ) ( )
遍历后计数值: 1 2 1 2 1 0 1 0
```
根据上述两个示例,我们可以很快的找出规律:
1. 遇到左括号,当前计数值大于 *0* ,则属于有效的左括号。
2. 遇到右括号,当前计数值大于 *1* ,则属于有效的右括号。
---
### 🎨 图解演示
![1.jpg](https://pic.leetcode-cn.com/1615909098-eOohaJ-1.jpg) ![2.jpg](https://pic.leetcode-cn.com/1615908912-aGpYJn-2.jpg) ![3.jpg](https://pic.leetcode-cn.com/1615908914-oGzkUH-3.jpg) ![4.jpg](https://pic.leetcode-cn.com/1615908917-mGekYh-4.jpg) ![5.jpg](https://pic.leetcode-cn.com/1615908919-mQrLwp-5.jpg) ![6.jpg](https://pic.leetcode-cn.com/1615908921-ZUDUic-6.jpg) ![7.jpg](https://pic.leetcode-cn.com/1615908923-AKfYyO-7.jpg) ![8.jpg](https://pic.leetcode-cn.com/1615908926-zzQCRy-8.jpg) ![9.jpg](https://pic.leetcode-cn.com/1615908928-KccJnw-9.jpg)
---
### 🍭 示例代码
```Javascript []
var removeOuterParentheses = function(S) {
let count = 0, ans = '';
for (let i = 0; i < S.length; i++) {
if(S[i] === '(' && count++ > 0) ans += '('
if(S[i] === ')' && count-- > 1) ans += ')';
}
return ans;
};
```
---
### 转身挥手
嘿,少年,做图不易,留下个赞或评论再走吧!谢啦~ 💐
差点忘了,祝你牛年大吉 🐮 AC 和 Offer 📑 多多益善~
⛲⛲⛲ 期待下次再见~

View File

@ -0,0 +1,48 @@
![图解每日一练.jpg](https://pic.leetcode-cn.com/1615817903-fzmpwZ-%E5%9B%BE%E8%A7%A3%E6%AF%8F%E6%97%A5%E4%B8%80%E7%BB%83.jpg)
---
### 🧠 解题思路
分析题意之后,可以得出以下结论:
1. 字符要做比较,所以之前的字符应该被存储下来,这里我们会用到栈。
2. 遍历字符,若栈顶和当前字符正好大小写都具备,则弹出栈顶抵消,否则当前字符入栈。
---
### 🎨 图解演示
![1.jpg](https://pic.leetcode-cn.com/1616513770-cbkAnG-1.jpg) ![2.jpg](https://pic.leetcode-cn.com/1616513772-ffshlQ-2.jpg) ![3.jpg](https://pic.leetcode-cn.com/1616513774-oJkAFT-3.jpg) ![4.jpg](https://pic.leetcode-cn.com/1616513777-jpPCEP-4.jpg) ![5.jpg](https://pic.leetcode-cn.com/1616513779-LcycBt-5.jpg) ![6.jpg](https://pic.leetcode-cn.com/1616513781-XeIFCV-6.jpg)
---
### 🍭 示例代码
```Javascript []
var makeGood = function(s) {
let res = [];
for(let i of s){
if(
res.length &&
res[res.length - 1] !== i &&
res[res.length - 1].toUpperCase() === i.toUpperCase()
){
res.pop();
}else{
res.push(i);
}
}
return res.join("");
};
```
---
### 转身挥手
嘿,少年,做图不易,留下个赞或评论再走吧!谢啦~ 💐
差点忘了,祝你牛年大吉 🐮 AC 和 Offer 📑 多多益善~
⛲⛲⛲ 期待下次再见~

File diff suppressed because one or more lines are too long