Day 11 | 20. Valid Parentheses | 1047. Remove All Adjacent Duplicates In String | 150. Evaluate RPN

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists
Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum
Day 7 | 454. 4Sum II | 383. Ransom Note | 15. 3Sum | 18. 4Sum
Day 8 | 344. Reverse String | 541. Reverse String II | 替换空格 | 151.Reverse Words in a String | 左旋转字符串
Day 9 | 28. Find the Index of the First Occurrence in a String | 459. Repeated Substring Pattern
Day 10 | 232. Implement Queue using Stacks | 225. Implement Stack using Queue

Directory

  • LeetCode 20. Valid Parentheses
  • LeetCode 1047. Remove All Adjacent Duplicates In String
  • LeetCode 150. Evaluate Reverse Polish Notation


LeetCode 20. Valid Parentheses

Question Link

Solution:

class Solution {
    public boolean isValid(String s) {
        // if the length of s is an odd number, s must be invalid
        if(s.length()%2 != 0) return false;

        Stack<Character> stack   = new Stack<>();
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(ch == '(' )
                stack.push(')');
            else if(ch == '[')
                stack.push(']');
            else if(ch == '{')
                stack.push('}');
            // First check if the stack is empty, then judge whether the top element is equal to the current letter
            // Situation 3 and 2
            else if(stack.isEmpty() || stack.peek() != ch)
                return false;
            else
                stack.pop();    
        }
        //Int 1 situation, the stack is not empty
        return stack.isEmpty();
    }
}

Thought:

  • We need to deal with three situations:
    • ( [ { } ] ( )
    • [ { ( ] } ]
    • [ { } ] ( ) ) )

LeetCode 1047. Remove All Adjacent Duplicates In String

Question Link

Solution:

class Solution {
    public String removeDuplicates(String s) {
        Stack<Character> stack = new Stack();
        for(int i = 0; i < s.length(); i++){
            char ch = s.charAt(i);
            if(stack.isEmpty() || stack.peek()!=ch)
                stack.push(ch);
            else
                stack.pop();
        }
        // The remaining elements in the stack are unrepeated.
        String str = "";
        while(!stack.isEmpty())
            str = stack.pop() + str;
        return str.toString();
    }
}

Thought:

  • Use a stack to save traversed elements. When traversing the current element, check the stack to see whether there is an adjacent duplicated element.
  • If exists repeated element, perform elimination operation pop(). Otherwise do push()
  • The remaining elements in the stack are unrepeated.

LeetCode 150. Evaluate Reverse Polish Notation

Question Link

Solution:

class Solution {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for(String s : tokens){
            if(s.equals("+"))
                stack.push(stack.pop() + stack.pop());
            else if(s.equals("-"))  
                stack.push(-stack.pop() + stack.pop());
            else if(s.equals("*"))
                stack.push(stack.pop() * stack.pop());
            else if(s.equals("/")){
                int temp1 = stack.pop();
                int temp2 = stack.pop();
                stack.push(temp2/temp1);
            }
            else
                stack.push(Integer.valueOf(s));
        }
        return stack.pop();
    }
}

Thoughts:

  • Reverse Polish Notation is a suffix notation. The so-called suffix means the indicator is in the back.
  • For example: This RPE of ( 1 + 2 ) * ( 3 + 4 ) is written as ( ( 1 2 + ) ( 3 4 + ) * )
  • Use the stack to operate RPN calculation. If it encounters a number, do push(). If it meets an indicator, get the numbers in the stack to do the calculation. Then push the result to the stack.
  • Note: - and / require additional processing.

你可能感兴趣的:(LeetCode,leetcode,stack)