- Blog home page Xiao Wu_ Xiao Wu has an idea_ CSDN blog - leetcode, notes, java domain Blogger
- Welcome to pay attentiongive the thumbs-upCollection and message
- Heaven rewards diligence. Diligence can make up for weakness. Come on with Xiao Wu
- 17-year-old freshman, limited level, please give advice, thank you very much!
- Reference book: algorithm 4, learning video: Dark horse programmer Java data structure and Java algorithm, the most complete data structure + algorithm tutorial of the whole network, 154 Java data structure diagrams_ Beep beep beep_ bilibili
catalogue
Inverse Polish expression evaluation problem
Stack
Stack is a data structure based on first in and last out. It is a special linear table that can only be inserted and deleted at one end. It stores data according to the principle of first in and last out. The first entered data is pressed into the bottom of the stack, and the last data is at the top of the stack. When it needs to be read, the data pops up from the top of the stack (the last data is read out first)
code implementation
import java.util.Iterator; public class Stack<T> implements Iterable <T>{ //Record first node private Node head; //Record the number of elements in the stack private int N; private class Node{ public T item; public Node next; public Node(T item,Node next) { this.item=item; this.next=next; } } public Stack() { this.head=new Node(null,null); this.N=0; } //Judge whether the number of elements in the current stack is 0 public boolean isEmpty() { return N==0; } //Get the number of elements in the stack public int size() { return N; } //Pushing elements into the stack is almost the same as inserting nodes in the linked list, except that they are inserted from the beginning public void push(T t) { //Find the first node that the first node points to Node oldFirst=head.next; //Create a new node Node newNode =new Node(t,null); //Let the first node point to the new node head.next=newNode; //Let the new node point to the original first node newNode.next=oldFirst; N++;//The elements are pressed in, and there must be more elements } //The pop-up element is actually that the first node crosses the first node and connects with the second node (pointing to the next node of the original first node) public T pop() { //Find the first node pointed to by the first node Node oldFirst=head.next;//It may be an empty linked list, so it should be handled safely if(oldFirst==null) { return null; } //Let the first node point to the next node of the original first node head.next=oldFirst.next; //Pop up elements. The number of elements must be reduced N--; return oldFirst.item; } public Iterator<T> iterator() { return new Literator(); } private class Literator implements Iterator{ private Node n; public Literator() { this.n=head; } public boolean hasNext() { return n.next!=null; } public Object next() { n=n.next; return n.item; } } } public class test { public static void main(String[] args) { //Create stack object Stack<String>stack=new Stack<>(); //Test stack stack.push("a"); stack.push("b"); stack.push("c"); stack.push("d"); for(String i:stack) { System.out.println(i); } System.out.println("------------------------"); //Test cartridge stack String result=stack.pop(); System.out.println("The pop-up element is"+result); System.out.println("The number of remaining elements is:"+stack.size()); } }
Bracket matching problem
Problem Description: given a string, which may contain "()" parentheses and other characters, please write a program to check whether the parentheses in the string appear in pairs.
For example:
"(small none) (small North)": correct
"Small none ((small North))": correct
"Xiao Wu (Xiao Bei (Xiao Ming) (Xiao Nan) Xiao Li)": correct
"((small North) small none": error
Note: this bracket appears in pairs, not referring to the number of "()", but making the left bracket (and the right bracket) correspond one by one
The main skill to deal with this problem is that when we traverse the string, if we encounter the left bracket, push it into the stack, and if we encounter the right bracket, pop up the elements of the stack. If there is no corresponding left bracket, the pop-up elements are null, which can be judged.
When we finally reach the end of the string, we have to judge whether there are left parentheses in the stack. If there are some, it means that there are few right parentheses and returns false. If there is no, it meets the meaning of the question and returns true.
code implementation
public class test { public static void main(String[] args) { String str="(Small none(Xiaobei)())"; boolean isf=isf(str); System.out.println(str+"Do the brackets in match:"+isf); } public static boolean isf(String str) { //Create a stack object to store the left parenthesis Stack<String> chars=new Stack<>(); //Traverses the string from left to right for(int i=0;i<str.length();i++) { //If the character type is not convenient to return, it is defined as a string type String currchar=str.charAt(i)+""; //Judge whether it is an open bracket. If so, press it into the stack if(currchar.equals("(")) { chars.push(currchar); } else if(currchar.equals(")")) {//Judge whether it is a right parenthesis. If so, pop up the element (left parenthesis) from the stack String p= chars.pop(); if(p==null) { return false; } } } //The above traversal has completed the operation of whether all the right parentheses correspond to the left parenthesis //Finally, we check whether there are left parentheses in the stack if(chars.size()==0) { return true; } else { return false; } } }
import java.util.Iterator; public class Stack<T> implements Iterable <T>{ //Record first node private Node head; //Record the number of elements in the stack private int N; private class Node{ public T item; public Node next; public Node(T item,Node next) { this.item=item; this.next=next; } } public Stack() { this.head=new Node(null,null); this.N=0; } //Judge whether the number of elements in the current stack is 0 public boolean isEmpty() { return N==0; } //Gets the number of elements in the stack public int size() { return N; } //Pushing elements into the stack is almost the same as inserting nodes in the linked list, except that they are inserted from the beginning public void push(T t) { //Find the first node pointed to by the first node Node oldFirst=head.next; //Create a new node Node newNode =new Node(t,null); //Let the first node point to the new node head.next=newNode; //Let the new node point to the original first node newNode.next=oldFirst; N++;//The element is pressed in, and there must be more elements } //The pop-up element is actually that the first node crosses the first node and connects with the second node (pointing to the next node of the original first node) public T pop() { //Find the first node pointed to by the first node Node oldFirst=head.next;//It may be an empty linked list, so it should be handled safely if(oldFirst==null) { return null; } //Let the first node point to the next node of the original first node head.next=oldFirst.next; //Pop up elements. The number of elements must be reduced N--; return oldFirst.item; } public Iterator<T> iterator() { return new Literator(); } private class Literator implements Iterator{ private Node n; public Literator() { this.n=head; } public boolean hasNext() { return n.next!=null; } public Object next() { n=n.next; return n.item; } } }
Inverse Polish expression evaluation problem
Polish expression is a+b we usually write. This formula becomes ab +, and the operation symbol is behind it
a+(b-c) becomes abc-+
a+(b-c)*d becomes abc-d*+
The idea of code processing is similar to the principle of the previous bracket matching problem. When we traverse the string, we judge whether the character is an operator. If not, put the number on the stack. If it is an operator, two numbers will pop up and use the operator to calculate
Put the result on the stack so that it can be calculated with other numbers using operators. We mainly use the principle of our stack to put in the number first and then out Finally, check whether it reaches the end of the string and return the final result
code implementation
public class test { public static void main(String[] args) { //The Polish expression of 4 * (20-18) + 9 / 3 is as follows String[] notation= {"4","20","18","-","*","9","3","/","+"}; int result =caculate(notation); System.out.println("The calculation result of the inverse Polish expression is:"+result); } public static int caculate(String[] notation) { //Define a stack to store operands Stack<Integer> ap=new Stack<>(); //Traverse the inverse Polish expression from left to right to determine whether the current element is an operator or an operand If it is an operator, two operands will pop up from the stack //If it is an operand, put it on the stack for(int i =0;i<notation.length;i++) { String curr=notation[i]; Integer b1,b2,result; switch(curr) {//At this time, it should be noted that the second pop-up element minus (operate) the first pop-up element case"+": b1=ap.pop(); b2=ap.pop(); result=b2+b1; ap.push(result); break; case"-": b1=ap.pop(); b2=ap.pop(); result=b2-b1; ap.push(result); break; case"*": b1=ap.pop(); b2=ap.pop(); result=b2*b1; ap.push(result); break; case"/": b1=ap.pop(); b2=ap.pop(); result=b2/b1; ap.push(result); break; default: ap.push(Integer.parseInt(curr)); break; } } //Get the end element in the stack and return the result int result=ap.pop(); return result; } }
import java.util.Iterator; public class Stack<T> implements Iterable <T>{ //Record first node private Node head; //Record the number of elements in the stack private int N; private class Node{ public T item; public Node next; public Node(T item,Node next) { this.item=item; this.next=next; } } public Stack() { this.head=new Node(null,null); this.N=0; } //Judge whether the number of elements in the current stack is 0 public boolean isEmpty() { return N==0; } //Get the number of elements in the stack public int size() { return N; } //Pushing elements into the stack is almost the same as inserting nodes in the linked list, except that they are inserted from the beginning public void push(T t) { //Find the first node pointed to by the first node Node oldFirst=head.next; //Create a new node Node newNode =new Node(t,null); //Let the first node point to the new node head.next=newNode; //Let the new node point to the original first node newNode.next=oldFirst; N++;//The element is pressed in, and there must be more elements } //The pop-up element is actually that the first node crosses the first node and connects with the second node (pointing to the next node of the original first node) public T pop() { //Find the first node pointed to by the first node Node oldFirst=head.next;//It may be an empty linked list, so it should be handled safely if(oldFirst==null) { return null; } //Let the first node point to the next node of the original first node head.next=oldFirst.next; //Pop up elements. The number of elements must be reduced N--; return oldFirst.item; } public Iterator<T> iterator() { return new Literator(); } private class Literator implements Iterator{ private Node n; public Literator() { this.n=head; } public boolean hasNext() { return n.next!=null; } public Object next() { n=n.next; return n.item; } } }
Stack - first in and then out, never change from its origin
Learning is like sailing against the current. Come on with Xiao Wu!