The next learning data structure

The issue of matching parentheses:

T20: Effective brackets

class Solution {
    public boolean isValid(String s) {
        Deque<Character> dq=new LinkedList<>();
        char chSet[]=s.toCharArray();
        for(char ch1:chSet)
        {
            if(dq.isEmpty())
            {
                dq.push(ch1);
            }
            else
            {
                char ch2=dq.pop();
                if(!match(ch1,ch2))
                {
                    dq.push(ch2);
                    dq.push(ch1);
                }
            }

        }
        if(dq.isEmpty())
            return true;
        else
            return false;

    }
    public boolean match(char ch1,char ch2)
    {
        if(ch1=='[' && ch2==']')
        {
            return true;
        }
        if(ch1=='{' && ch2=='}')
        {
            return true;
        }
        if(ch1=='(' && ch2==')')
        {
            return true;
        }
        if(ch1=='}' && ch2=='{')
        {
            return true;
        }
        if(ch1==')' && ch2=='(')
        {
            return true;
        }
        if(ch1==']' && ch2=='[')
        {
            return true;
        }
        return false;
    }
}
 
(1) deque can be used as a push stack, pop, isEmpty operation
(2) deque solution, can also be used linkedList linkedlist queue actually be instantiated. 
(3) Another example of the method, where the learning is to learn, to an array implements the Stack, and arrays are possible linkedlist
(4) java and with or are: && and ||
(5) string transfer may be achieved charArray converted by .toCharArray. The char Array string to be converted of course also be split by other functions.
 
string to access the inside of the char, you can also use this method:

for(int i = 0 ; i < s.length() ; i ++){
char c = s.charAt(i);

The method is to use charAt tell you where to position the char

 

The corresponding queue, with the corresponding arrayQueue also be achieved.

Thing I learned is: .addfirst addlast these things, you need to add this function to call the best, because between them increases, narrowing the logical consistency of the most important    

public Array(){
this(10);
}

This mean: also call the constructor 10, 10 passed as a parameter

Guess you like

Origin www.cnblogs.com/startFrom0/p/12585587.html