力扣:面试题30:包含min函数的栈
This commit is contained in:
parent
ea952085f8
commit
e3ddb8c3e7
@ -0,0 +1,85 @@
|
||||
//定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 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)
|
||||
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
<p>定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>示例:</strong></p>
|
||||
|
||||
<pre>MinStack minStack = new MinStack();
|
||||
minStack.push(-2);
|
||||
minStack.push(0);
|
||||
minStack.push(-3);
|
||||
minStack.min(); --> 返回 -3.
|
||||
minStack.pop();
|
||||
minStack.top(); --> 返回 0.
|
||||
minStack.min(); --> 返回 -2.
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ol>
|
||||
<li>各函数的调用总次数不超过 20000 次</li>
|
||||
</ol>
|
||||
|
||||
<p> </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>
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
@ -0,0 +1,60 @@
|
||||

|
||||
|
||||
---
|
||||
|
||||
### 🧠 解题思路
|
||||
|
||||
我们解决这道题的关键在于,需要知道哪些是需要去除的外层括号,为了找到这些需要去除的外层括号,我们可以使用到计数器。
|
||||
|
||||
**规则:** 遇到左括号,我们的计数器 *+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* ,则属于有效的右括号。
|
||||
|
||||
---
|
||||
|
||||
### 🎨 图解演示
|
||||
|
||||
        
|
||||
|
||||
---
|
||||
|
||||
### 🍭 示例代码
|
||||
|
||||
```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 📑 多多益善~
|
||||
|
||||
⛲⛲⛲ 期待下次再见~
|
@ -0,0 +1,48 @@
|
||||

|
||||
|
||||
---
|
||||
|
||||
### 🧠 解题思路
|
||||
|
||||
分析题意之后,可以得出以下结论:
|
||||
|
||||
1. 字符要做比较,所以之前的字符应该被存储下来,这里我们会用到栈。
|
||||
2. 遍历字符,若栈顶和当前字符正好大小写都具备,则弹出栈顶抵消,否则当前字符入栈。
|
||||
|
||||
---
|
||||
|
||||
### 🎨 图解演示
|
||||
|
||||
     
|
||||
|
||||
---
|
||||
|
||||
### 🍭 示例代码
|
||||
|
||||
```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
Loading…
Reference in New Issue
Block a user