LeetCode--[栈]--不定时更新
1._20_有效括号
1.1链接
https://leetcode-cn.com/problems/valid-parentheses/
1.2题目描述
1.3解决思路
1.4代码实现
java版本
import java.util.HashMap; import java.util.Stack; public class _20_有效的括号 { private static HashMap<Character, Character> map = new HashMap<>(); static { // key - value map.put('(', ')'); map.put('{', '}'); map.put('[', ']'); } public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); int len = s.length(); for (int i = 0; i < len; i++) { char c = s.charAt(i); if (map.containsKey(c)) { // 左括号 stack.push(c); } else { // 右括号 if (stack.isEmpty()) return false; if (c != map.get(stack.pop())) return false; } } return stack.isEmpty(); } public boolean isValid1(String s) { Stack<Character> stack = new Stack<>(); int len = s.length(); for (int i = 0; i < len; i++) { char c = s.charAt(i); if (c == '(' || c == '{' || c == '[') { // 左括号 stack.push(c); } else { // 右括号 if (stack.isEmpty()) return false; char left = stack.pop(); if (left == '(' && c != ')') return false; if (left == '{' && c != '}') return false; if (left == '[' && c != ']') return false; } } return stack.isEmpty(); } public boolean isValid2(String s) { while (s.contains("{}") || s.contains("[]") || s.contains("()")) { s = s.replace("{}", ""); s = s.replace("()", ""); s = s.replace("[]", ""); } return s.isEmpty(); } }
2._232_用栈实现队列
2.1链接
https://leetcode-cn.com/problems/implement-queue-using-stacks/
2.2题目描述
2.3解决思路
2.4代码实现
public class _232_用栈实现队列 { private Stack<Integer> inStack; private Stack<Integer> outStack; /** Initialize your data structure here. */ public _232_用栈实现队列() { inStack = new Stack<>(); outStack = new Stack<>(); } /** 入队 */ public void push(int x) { inStack.push(x); } /** 出队 */ public int pop() { checkOutStack(); return outStack.pop(); } /** 获取队头元素 */ public int peek() { checkOutStack(); return outStack.peek(); } /** 是否为空 */ public boolean empty() { return inStack.isEmpty() && outStack.isEmpty(); } private void checkOutStack() { if (outStack.isEmpty()) { while (!inStack.isEmpty()) { outStack.push(inStack.pop()); } } } }
3._150_逆波兰表达式
3.1链接
https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
3.2题目描述
3.3解题思路
简介
逆波兰式(Reverse Polish notation,RPN,或逆波兰记法),也叫后缀表达式(将运算符写在操作数之后)
作用
计算机内存架构使用栈式结构它执行先进后出的顺序
算法结构
3.4代码实现
java版本
class Solution { // 栈实现 8 ms 36.7 MB public static int evalRPN(String[] tokens) { Stack<Integer> numStack = new Stack<>(); Integer op1, op2; for (String s : tokens) { switch (s) { case "+": op2 = numStack.pop(); op1 = numStack.pop(); numStack.push(op1 + op2); break; case "-": op2 = numStack.pop(); op1 = numStack.pop(); numStack.push(op1 - op2); break; case "*": op2 = numStack.pop(); op1 = numStack.pop(); numStack.push(op1 * op2); break; case "/": op2 = numStack.pop(); op1 = numStack.pop(); numStack.push(op1 / op2); break; default: numStack.push(Integer.valueOf(s)); break; } } return numStack.pop(); } }
4._224_基本计算器
4.1链接
https://leetcode-cn.com/problems/basic-calculator/
4.2题目描述
4.3解题思路
用栈来存储括号前的值和该值的正负号
4.4代码实现
python版本
def calculate(s: str) -> int: res = 0 stack = [] sign = 1 i = 0 n = len(s) while i < n: if s[i] == " ": i += 1 elif s[i] == "-": sign = -1 i += 1 elif s[i] == "+": sign = 1 i += 1 elif s[i] == "(": #存储(括号前的值是多少 和是整数还是负数 方便)时候 进行结果提取和相加 stack.append(res) stack.append(sign) res = 0 sign = 1 i += 1 elif s[i] == ")": # print(stack) res = res * stack.pop() + stack.pop() i += 1 elif s[i].isdigit(): tmp = int(s[i]) i += 1 #因为是按照一个一个字符进行处理的所以像18 192这样的大于1位的数字需要特殊处理 while i < n and s[i].isdigit(): tmp = tmp * 10 + int(s[i]) i += 1 res += tmp * sign return res res1=calculate('(10-2+(15+2))') print(res1)
5._856_括号的分数
5.1链接
https://leetcode-cn.com/problems/score-of-parentheses/
5.2题目描述
5.3解题思路
5.4代码实现
python版本
def scoreOfParentheses(S): stack = [0] #The score of the current frame for x in S: if x == '(': stack.append(0) else: v = stack.pop() stack[-1] += max(2 * v, 1) return stack.pop() score1=scoreOfParentheses('(((())))') score2=scoreOfParentheses('()()()') print(score1)
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本
X._XXX_用栈实现队列
X.1链接
X.2题目描述
X.3解题思路
X.4代码实现
java版本