155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- getMin() – Retrieve the minimum element in the stack.
Example 1:
1 | Input |
Constraints:
Methods
pop
,top
andgetMin
operations will always be called on non-empty stacks.1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87/*
* @lc app=leetcode.cn id=155 lang=java
*
* [155] 最小栈
*/
// @lc code=start
class MinStack {
Stack<Integer> stack;
int min;//Integer比较的是范围,是地址,Interger类型之间的比较应该用equals()
/** initialize your data structure here. */
public MinStack() {
stack = new Stack<>();
min = Integer.MAX_VALUE;//初始化Min
}
public void push(int x) {
if (x <= min) {
stack.push(min);
min = x;
}
stack.push(x);
}
public void pop() {
if (stack.pop() == min) {
min = stack.pop();
}
}
public int top() {
return stack.peek();
}
public int getMin() {
return min;
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
// @lc code=end
// class MinStack {
// private Stack<Integer> stack;
// private Stack<Integer> minStack;
// /** initialize your data structure here. */
// public MinStack() {
// stack = new Stack<>();
// minStack = new Stack<>();
// }
// public void push(int x) {
// stack.push(x);
// if(!minStack.isEmpty()) {
// int min = minStack.peek();
// if(x <= min) {
// minStack.push(x);
// }
// } else {
// minStack.push(x);
// }
// }
// public void pop() {
// int x = stack.pop();
// if(!minStack.isEmpty()) {
// if(x == minStack.peek()) {
// minStack.pop();
// }
// }
// }
// public int top() {
// return stack.peek();
// }
// public int getMin() {
// return minStack.peek();
// }
// }
232.Implement Queue using Stacks
Implement the following operations of a queue using stacks.
- push(x) – Push element x to the back of queue.
- pop() – Removes the element from in front of queue.
- peek() – Get the front element.
- empty() – Return whether the queue is empty.
Example:
1 | MyQueue queue = new MyQueue(); |
Notes:
- You must use only standard operations of a stack – which means only
push to top
,peek/pop from top
,size
, andis empty
operations are valid. - Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
- You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
1 | 没有的注释的为O(1) |
225. Implement Stack using Queues
Implement the following operations of a stack using queues.
- push(x) – Push element x onto stack.
- pop() – Removes the element on top of the stack.
- top() – Get the top element.
- empty() – Return whether the stack is empty.
Example:
1 | MyStack stack = new MyStack(); |
Notes:
- You must use only standard operations of a queue – which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
1 | /* |
20. Valid Parentheses
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
1 | Input: "()" |
Example 2:
1 | Input: "()[]{}" |
Example 3:
1 | Input: "(]" |
Example 4:
1 | Input: "([)]" |
Example 5:
1 | Input: "{[]}" |
1 | /* |
150. Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +
, -
, *
, /
. Each operand may be an integer or another expression.
Note:
- Division between two integers should truncate toward zero.
- The given RPN expression is always valid. That means the expression would always evaluate to a result and there won’t be any divide by zero operation.
Example 1:
1 | Input: ["2", "1", "+", "3", "*"] |
Example 2:
1 | Input: ["4", "13", "5", "/", "+"] |
Example 3:
1 | Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] |
逆波兰表达式:
逆波兰表达式是一种后缀表达式,所谓后缀就是指算符写在后面。
- 平常使用的算式则是一种中缀表达式,如
( 1 + 2 ) * ( 3 + 4 )
。 - 该算式的逆波兰表达式写法为
( ( 1 2 + ) ( 3 4 + ) * )
。
逆波兰表达式主要有以下两个优点:
去掉括号后表达式无歧义,上式即便写成
1 2 + 3 4 + *
也可以依据次序计算出正确结果。适合用栈操作运算:遇到数字则入栈;遇到算符则取出栈顶两个数字进行计算,并将结果压入栈中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31/*
* @lc app=leetcode.cn id=150 lang=java
*
* [150] 逆波兰表达式求值
*/
// @lc code=start
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String s : tokens) {
if(s.equals("+")) {
stack.push(stack.pop() + stack.pop());
} else if (s.equals("-")) {
int a = stack.pop();//先pop出来的数是减数
int b = stack.pop();
stack.push(b - a);
} else if (s.equals("*")) {
stack.push(stack.pop() * stack.pop());
} else if (s.equals("/")) {
int a = stack.pop();
int b = stack.pop();
stack.push(b / a);
} else {
stack.push(Integer.parseInt(s));//从String转为Int
}
}
return stack.pop();
}
}
// @lc code=end
224. Basic Calculator
Share
Implement a basic calculator to evaluate a simple expression string.
The expression string may contain open (
and closing parentheses )
, the plus +
or minus sign -
, non-negative integers and empty spaces .
Example 1:
1 | Input: "1 + 1" |
Example 2:
1 | Input: " 2-1 + 2 " |
Example 3:
1 | Input: "(1+(4+5+2)-3)+(6+8)" |
Note:
- You may assume that the given expression is always valid.
- Do not use the
eval
built-in library function.
1 | /* |
161. One Edit Distance
字符串S
和T
只差一个距离,返回True
, 否则返回False
1 | case1:abcre Abere |
168. Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
For example:
1 | 1 -> A |
Example 1:
1 | Input: 1 |
Example 2:
1 | Input: 28 |
Example 3:
1 | Input: 701 |
1 | /* |
171. Excel Sheet Column Number
Given a column title as appear in an Excel sheet, return its corresponding column number.
For example:
1 | A -> 1 |
Example 1:
1 | Input: "A" |
Example 2:
1 | Input: "AB" |
Example 3:
1 | Input: "ZY" |
1 | /* |
38. Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:
1 | 1. 1 |
1
is read off as "one 1"
or 11
.11
is read off as "two 1s"
or 21
.21
is read off as "one 2
, then one 1"
or 1211
.
Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence. You can do so recursively, in other words from the previous member read off the digits, counting the number of digits in groups of the same digit.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
1 | Input: 1 |
Example 2:
1 | Input: 4 |
1 | /* |