> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/02-09-2022-150.md).

# 02/09/2022 150

Method: Use Stack. Iterate the string array: if the element is a number, push it into stack. When you meet operands like "+ - \* /", pop two times from the stack and do the correponding calulation and push the value into the stack. Finally return the result poped from the stack.

Time O(n)

Space O(n)

Let nn be the length of the list.

* Time Complexity : O(n)O(n).

  We do a linear search to put all numbers on the stack, and process all operators. Processing an operator requires removing 2 numbers off the stack and replacing them with a single number, which is an O(1)O(1) operation. Therefore, the total cost is proportional to the length of the input array. Unlike before, we're no longer doing expensive deletes from the middle of an Array or List.
* Space Complexity : O(n)O(n).

  In the worst case, the stack will have all the numbers on it at the same time. This is never more than half the length of the input array.

```
// Some code
class Solution { 
    public int evalRPN(String[] tokens) { 
        Stack stack = new Stack<>(); 
        for(String token: tokens){ 
        switch(token){ 
            case "+": 
                stack.push(stack.pop() + stack.pop()); 
                break; 
            case "-": 
                stack.push(-stack.pop() + stack.pop()); 
                break; 
            case "*": 
                stack.push(stack.pop() * stack.pop()); 
                break; 
            case "/": 
                int number2 = stack.pop(); 
                int number1 = stack.pop(); 
                stack.push(number1 / number2); 
                break; 
            default: 
                stack.push(Integer.valueOf(token));
        }
    }     
    return stack.pop();
    } 
}
```

class Solution {

```
public int evalRPN(String[] tokens) {
    
    Stack<Integer> stack = new Stack<>();
    
    for (String token : tokens) {
        
        if (!"+-*/".contains(token)) {
            stack.push(Integer.valueOf(token));
            continue;
        }
        
        int number2 = stack.pop();
        int number1 = stack.pop();
        
        int result = 0;
        
        switch (token) {
            case "+":
                result = number1 + number2;
                break;
            case "-":
                result = number1 - number2;
                break;
            case "*":
                result = number1 * number2;
                break;
            case "/":
                result = number1 / number2;
                break;
        }
        
        stack.push(result);
        
    }
    
    return stack.pop();
}
```

}

switch usegae

```
class SwitchDemo2 {
    public static void main(String[] args) {

        int month = 2;
        int year = 2000;
        int numDays = 0;

        switch (month) {
            case 1: case 3: case 5:
            case 7: case 8: case 10:
            case 12:
                numDays = 31;
                break;
            case 4: case 6:
            case 9: case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && 
                     !(year % 100 == 0))
                     || (year % 400 == 0))
                    numDays = 29;
                else
                    numDays = 28;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }
        System.out.println("Number of Days = "
                           + numDays);
    }
}
```
