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


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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值