当前位置:网站首页>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 :
边栏推荐
- Exercises under insect STL string
- 嵌入式virlog代码运行流程
- Go language - pipeline channel
- D check type is pointer
- Bug memory management
- Reprint - easy to use wechat applet UI component library
- MySQL configuration improves data insertion efficiency
- How to check if a text field is empty or not in swift
- CloudCompare——泊松重建
- C language ---getchar() and putchar()
猜你喜欢
Global variable vs local variable
Wechat applet -picker component is repackaged and the disabled attribute is added -- below
2021-10-18 character array
windows版MySQL软件的安装与卸载
Build your own PE manually from winpe of ADK
What is the use of index aliases in ES
New specification of risc-v chip architecture
Select tag - uses the default text as a placeholder prompt but is not considered a valid value
Free machine learning dataset website (6300+ dataset)
Calculate the distance between two points (2D, 3D)
随机推荐
Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
AGCO AI frontier promotion (6.26)
Common operation and Principle Exploration of stream
Assert and constd13
9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
虫子 STL string 下 练习题
Gartner 2022 Top Strategic Technology Trends Report
Exercises under insect STL string
Mongodb series window environment deployment configuration
嵌入式virlog代码运行流程
Applicable and inapplicable scenarios of mongodb series
First k large XOR value problem
证券开户安全吗,有没有什么危险啊
基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
array
Codeforces Global Round 21A~D
虫子 内存管理 下 内存注意点
On insect classes and objects
Es common grammar I