棒球比赛(simple)
https://leetcode-cn.com/circle/article/48kq9d/
class Solution {
public int calPoints(String[] ops) {
Stack<Integer> stack = new Stack();
for(String op : ops) {
if (op.equals("+")) {
int top = stack.pop();
int newtop = top + stack.peek();
stack.push(top);
stack.push(newtop);
} else if (op.equals("C")) {
stack.pop();
} else if (op.equals("D")) {
stack.push(2 * stack.peek());
} else {
stack.push(Integer.valueOf(op));
}
}
int ans = 0;
for(int score : stack) ans += score;
return ans;
}
}
作者:LeetCode
链接:https://leetcode-cn.com/problems/baseball-game/solution/bang-qiu-bi-sai-by-leetcode/
来源:力扣(LeetCode)
本方法思路和代码来源:
作者:Jade_Xie
链接:https://leetcode-cn.com/problems/baseball-game/solution/java-1ms-ji-bai-100-shu-zu-de-si-xiang-b-yvo3/
来源:力扣(LeetCode)
class Solution {
public int calPoints(String[] ops) {
//数组长度
int length = ops.length;
//建立一个数字数组,用来存储每次的操作结果
int[] num = new int[length];
//数字数组的当前下标
int index = 0;
//遍历符号数组
for(int i = 0; i < length; i++)
{
//当前符号为“C”,需要清除一个数字
if(ops[i].equals("C"))
{
//下标回退一格,且回退后位置上的数字置零
index --;
num[index] = 0;
}
//当前符号为“D”,需要翻倍
else if(ops[i].equals("D"))
{
//当前数字等于前一个数字的两倍
num[index] = 2 * num[index - 1];
index ++;
}
//当前符号为“+”,需要相加
else if(ops[i].equals("+"))
{
//当前数字等于前两个数字之和
num[index] = num[index - 1] + num[index - 2];
index ++;
}
//当前符号为数字,直接填入数字数组
else
{
num[index] = Integer.parseInt(ops[i]);
index ++;
}
}
//对数字数组进行求和
int sum = 0;
for(int j = 0; j < length; j++)
{
sum += num[j];
}
return sum;
}
}
作者:Jade_Xie
链接:https://leetcode-cn.com/problems/baseball-game/solution/java-1ms-ji-bai-100-shu-zu-de-si-xiang-b-yvo3/
来源:力扣(LeetCode)
简化路径(medium难度)
https://leetcode-cn.com/problems/simplify-path/
代码及思路来源:
作者:LeetcodeFrom20210207
链接:https://leetcode-cn.com/problems/simplify-path/solution/71-jian-hua-lu-jing-by-leetcodefrom20210-c9uv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
class Solution {
public String simplifyPath(String path) {
LinkedList<String> stack = new LinkedList<String>();//用栈来模拟
String[] strArr = path.split("/");//用/分割,多个/也视为/
for(String str: strArr){//遍历
//如果等于空或者等于.,那就没有影响
if(str.equals("") || str.equals(".")){
continue;
}
//如果等于..,那就要返回上一级目录,因此栈中弹出当前目录
//此时可能栈是空
if(str.equals("..")){
if(!stack.isEmpty()){
stack.pop();
}
continue;
}
//否则,栈中压入当前目录
stack.push(str);
}
StringBuffer sb = new StringBuffer();
Collections.reverse(stack);
while(!stack.isEmpty()){
String tmp = stack.pop();
sb.append("/").append(tmp);
}
if(sb.length() == 0){
sb.append("/");
}
String res = sb.toString();
return res;
}
}
作者:LeetcodeFrom20210207
链接:https://leetcode-cn.com/problems/simplify-path/solution/71-jian-hua-lu-jing-by-leetcodefrom20210-c9uv/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
逆波兰表达式求值(medium难度)
https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
class Solution {
public int evalRPN(String[] tokens) {
Deque<Integer> stack = new LinkedList<Integer>();
int n = tokens.length;
for (int i = 0; i < n; i++) {
String token = tokens[i];
if (isNumber(token)) {
stack.push(Integer.parseInt(token));
} else {
int num2 = stack.pop();
int num1 = stack.pop();
switch (token) {
case "+":
stack.push(num1 + num2);
break;
case "-":
stack.push(num1 - num2);
break;
case "*":
stack.push(num1 * num2);
break;
case "/":
stack.push(num1 / num2);
break;
default:
}
}
}
return stack.pop();
}
public boolean isNumber(String token) {
return !("+".equals(token) || "-".equals(token) || "*".equals(token) || "/".equals(token));
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/solution/ni-bo-lan-biao-da-shi-qiu-zhi-by-leetcod-wue9/
来源:力扣(LeetCode)
class Solution {
public int evalRPN(String[] tokens) {
int n = tokens.length;
int[] stack = new int[(n + 1) / 2];
int index = -1;
for (int i = 0; i < n; i++) {
String token = tokens[i];
switch (token) {
case "+":
index--;
stack[index] += stack[index + 1];
break;
case "-":
index--;
stack[index] -= stack[index + 1];
break;
case "*":
index--;
stack[index] *= stack[index + 1];
break;
case "/":
index--;
stack[index] /= stack[index + 1];
break;
default:
index++;
stack[index] = Integer.parseInt(token);
}
}
return stack[index];
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/solution/ni-bo-lan-biao-da-shi-qiu-zhi-by-leetcod-wue9/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。