当前位置:网站首页>Sword finger offer 09.30 Stack
Sword finger offer 09.30 Stack
2022-06-26 14:13:00 【hedgehog:】
subject 1:
20. Valid parenthesis
https://leetcode-cn.com/problems/valid-parentheses/

Code :
class Solution {
public boolean isValid(String s) {
char[] ch = s.toCharArray();// Convert to character array
Stack<Character> st=new Stack<Character>();
boolean flag=false;
for(int i=0;i<ch.length;i++){
if(st.empty()){
// If the stack is empty The stack
st.push(ch[i]);
}else{
// Determine whether the brackets match
flag=isMatching(st.peek(),ch[i]);
if(flag){
// Parentheses matching
st.pop();
}else{
// Bracket mismatch
st.push(ch[i]);
}
}
}
if(st.empty())
return true;
else
return false;
}
boolean isMatching(char a,char b){
if(a+1==b||a+2==b)
return true;
else
return false;
}
}result :

subject 2:


Code :
class CQueue {
Stack<Integer> st1;
Stack<Integer> st2;
public CQueue() {
st1=new Stack<Integer>();
st2=new Stack<Integer>();
}
public void appendTail(int value) {
st1.push(value);
}
public int deleteHead() {
if(st2.empty()){
// If st2 It's empty. Will s1 Some of them pour in
while(!st1.empty())
st2.push(st1.pop());
}
if(!st2.empty())// If it's over 2 Non empty be pop
return st2.pop();
else// If it's over 2 Or empty? Then return to -1
return -1;
}
}
/**
* Your CQueue object will be instantiated and called as such:
* CQueue obj = new CQueue();
* obj.appendTail(value);
* int param_2 = obj.deleteHead();
*/result :

subject 3:

Code 1 :
class MinStack {
Stack<Integer> st1;
Stack<Integer> st2;
int min=(int)-Math.pow(2,31)-1;
/** initialize your data structure here. */
public MinStack() {
st1=new Stack<Integer>();
st2=new Stack<Integer>();
}
public void push(int x) {
// use st2 Synchronously maintain the minimum stack
min=min<x?min:x;
st1.push(x);
st2.push(min);
}
public void pop() {
st1.pop();
st2.pop();
// Remember to update the minimum value synchronously when bouncing the stack
if(!st2.empty())
min=st2.peek();
else
min=(int)-Math.pow(2,31)-1;
}
public int top() {
return st1.peek();
}
public int min() {
return st2.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();
*/Code 2 : Reference others Not through min Update minimum
class MinStack {
Stack<Integer> A, B;
public MinStack() {
A = new Stack<>();
B = new Stack<>();
}
public void push(int x) {
A.push(x);
if(B.empty() || B.peek() >= x)
B.push(x);
}
public void pop() {
if(A.pop().equals(B.peek()))
B.pop();
}
public int top() {
return A.peek();
}
public int min() {
return B.peek();
}
}result :

边栏推荐
- [scoi2016] lucky numbers
- 9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
- Svn commit error after deleting files locally
- 嵌入式virlog代码运行流程
- Comparison of disk partition modes (MBR and GPT)
- On insect classes and objects
- Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
- [proteus simulation] Arduino uno key start / stop + PWM speed control DC motor speed
- Is it safe to open a securities account? Is there any danger
- C | analysis of malloc implementation
猜你喜欢

Related knowledge of libsvm support vector machine

Stream常用操作以及原理探索

微信小程序注册指引

Correlation analysis related knowledge

9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》

Freefilesync folder comparison and synchronization software

VTK 圆柱体的生成与渲染

Installation and uninstallation of MySQL software for windows

Included angle of 3D vector

Gartner 2022年顶级战略技术趋势报告
随机推荐
ThreadLocal巨坑!内存泄露只是小儿科...
虫子 运算符重载的一个好玩的
It is better and safer to choose which securities company to open an account for flush stock
证券开户安全吗,有没有什么危险啊
Zero basics of C language lesson 7: break & continue
mysql配置提高数据插入效率
VTK 圆柱体的生成与渲染
Logical operation
CloudCompare——泊松重建
Create your own cross domain proxy server
Obtain information about hard disk and volume or partition (capacity, ID, volume label name, etc.)
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
免费的机器学习数据集网站(6300+数据集)
d的is表达式
嵌入式virlog代码运行流程
Build your own PE manually from winpe of ADK
Luogu p4513 xiaobaiguang Park
虫子 类和对象 中
Network remote access using raspberry pie
Introduction to 26 papers related to CVPR 2022 document image analysis and recognition