今天是栈操作。
682. 棒球比赛
你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。
比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i] 是你需要记录的第 i 项操作,ops 遵循下述规则:
整数 x - 表示本回合新获得分数 x
"+" - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。
"D" - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。
"C" - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。
请你返回记录中所有得分的总和。
示例 1:
输入:ops = ["5","2","C","D","+"]
输出:30
解释:
"5" - 记录加 5 ,记录现在是 [5]
"2" - 记录加 2 ,记录现在是 [5, 2]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5].
"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10].
"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15].
所有得分的总和 5 + 10 + 15 = 30
示例 2:
输入:ops = ["5","-2","4","C","D","9","+","+"]
输出:27
解释:
"5" - 记录加 5 ,记录现在是 [5]
"-2" - 记录加 -2 ,记录现在是 [5, -2]
"4" - 记录加 4 ,记录现在是 [5, -2, 4]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2]
"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4]
"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9]
"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5]
"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14]
所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
示例 3:
输入:ops = ["1"]
输出:1
提示:
1 <= ops.length <= 1000
ops[i] 为 "C"、"D"、"+",或者一个表示整数的字符串。整数范围是 [-3 * 104, 3 * 104]
对于 "+" 操作,题目数据保证记录此操作时前面总是存在两个有效的分数
对于 "C" 和 "D" 操作,题目数据保证记录此操作时前面总是存在一个有效的分数
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/baseball-game
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路及方法
用栈,遇到数字直接入栈,即代表分数;
遇到“+”,先弹出栈顶,然后计算弹出栈顶和当前栈顶之和,再push弹出栈顶,然后push和;
遇到“D”,直接push当前栈顶乘以2;
遇到“C”,弹出当前栈顶。
最后对栈求和。
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> score = new Stack<>();
for (String str : ops) {
if (str.equals("+")) {
int last = score.pop();
int cur = last + score.peek();
score.push(last);
score.push(cur);
} else if (str.equals("D")) {
score.push(score.peek() * 2);
} else if (str.equals("C")) {
score.pop();
} else {
score.push(Integer.parseInt(str));
}
}
int res = 0;
for (int i : score) {
res += i;
}
return res;
}
}
结果如下:
面试题 03.04. 化栈为队
实现一个MyQueue类,该类用两个栈来实现一个队列。
示例:
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
说明:
你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size 和 is empty 操作是合法的。
你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路及方法
一个栈用来写入数据,另一个栈用来读取数据。
因为栈是先进后出,队列是先进先出,为了模拟队列,最先入栈的数据应该最先弹出,即用第二个栈read栈来逆序存放输入进write栈里的数据,那么read栈的顶部就是write的底部,即最先进栈的数据,这时候对其弹出peek操作都可。操作后又重新push进write栈。
class MyQueue {
public Stack<Integer> write;
public Stack<Integer> read;
/** Initialize your data structure here. */
public MyQueue() {
write = new Stack<>();
read = new Stack<>();
}
/** Push element x to the back of queue. */
public void push(int x) {
write.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
// 将write栈逆序存入read栈
while (!write.isEmpty()) {
read.push(write.pop());
}
// 出队并删除
int res = read.pop();
// 将出队并删除后的read栈重新存入write栈
while (!read.isEmpty()) {
write.push(read.pop());
}
return res;
}
/** Get the front element. */
public int peek() {
// 将write栈逆序存入read栈
while (!write.isEmpty()) {
read.push(write.pop());
}
// 出队不删除
int res = read.peek();
// 将出队不删除后的read栈重新存入write栈
while (!read.isEmpty()) {
write.push(read.pop());
}
return res;
}
/** Returns whether the queue is empty. */
public boolean empty() {
return write.isEmpty() && read.isEmpty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
结果如下:
网友评论