224:基本计算器
This commit is contained in:
parent
44bfc0817b
commit
b057235fee
89
src/main/java/leetcode/editor/cn/BasicCalculator.java
Normal file
89
src/main/java/leetcode/editor/cn/BasicCalculator.java
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
//给你一个字符串表达式 s ,请你实现一个基本计算器来计算并返回它的值。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "1 + 1"
|
||||||
|
//输出:2
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = " 2-1 + 2 "
|
||||||
|
//输出:3
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 3:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:s = "(1+(4+5+2)-3)+(6+8)"
|
||||||
|
//输出:23
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1 <= s.length <= 3 * 105
|
||||||
|
// s 由数字、'+'、'-'、'('、')'、和 ' ' 组成
|
||||||
|
// s 表示一个有效的表达式
|
||||||
|
//
|
||||||
|
// Related Topics 栈 数学
|
||||||
|
// 👍 571 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
//224:基本计算器
|
||||||
|
public class BasicCalculator {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new BasicCalculator().new Solution();
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int calculate(String s) {
|
||||||
|
Deque<Integer> ops = new LinkedList<Integer>();
|
||||||
|
ops.push(1);
|
||||||
|
int sign = 1;
|
||||||
|
|
||||||
|
int ret = 0;
|
||||||
|
int n = s.length();
|
||||||
|
int i = 0;
|
||||||
|
while (i < n) {
|
||||||
|
if (s.charAt(i) == ' ') {
|
||||||
|
i++;
|
||||||
|
} else if (s.charAt(i) == '+') {
|
||||||
|
sign = ops.peek();
|
||||||
|
i++;
|
||||||
|
} else if (s.charAt(i) == '-') {
|
||||||
|
sign = -ops.peek();
|
||||||
|
i++;
|
||||||
|
} else if (s.charAt(i) == '(') {
|
||||||
|
ops.push(sign);
|
||||||
|
i++;
|
||||||
|
} else if (s.charAt(i) == ')') {
|
||||||
|
ops.pop();
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
long num = 0;
|
||||||
|
while (i < n && Character.isDigit(s.charAt(i))) {
|
||||||
|
num = num * 10 + s.charAt(i) - '0';
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
ret += sign * num;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
35
src/main/java/leetcode/editor/cn/BasicCalculator.md
Normal file
35
src/main/java/leetcode/editor/cn/BasicCalculator.md
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<p>给你一个字符串表达式 <code>s</code> ,请你实现一个基本计算器来计算并返回它的值。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "1 + 1"
|
||||||
|
<strong>输出:</strong>2
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = " 2-1 + 2 "
|
||||||
|
<strong>输出:</strong>3
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 3:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>s = "(1+(4+5+2)-3)+(6+8)"
|
||||||
|
<strong>输出:</strong>23
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>1 <= s.length <= 3 * 10<sup>5</sup></code></li>
|
||||||
|
<li><code>s</code> 由数字、<code>'+'</code>、<code>'-'</code>、<code>'('</code>、<code>')'</code>、和 <code>' '</code> 组成</li>
|
||||||
|
<li><code>s</code> 表示一个有效的表达式</li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>栈</li><li>数学</li></div></div>\n<div><li>👍 571</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user