LeetCode224基本計算機のJava実装



Java Implementation Leetcode 224 Basic Calculator



224.基本的な計算機

単純な文字列式の値を計算するための基本的な計算機を実装します。

文字列式には、左括弧(、右括弧)、プラス記号+、マイナス記号-、非負の整数およびスペースを含めることができます。



例1:

入力: '1 + 1'
出力:2
例2:



入力:「2-1 + 2」
出力:3
例3:

入力: '(1+(4 + 5 + 2)-3)+(6 + 8)'
出力:23
説明:

与えられた式はすべて有効であると想定できます。
組み込みのライブラリ関数evalは使用しないでください。



class Solution { public int calculate(String s) { Stack stack = new Stack() // sign represents positive and negative int sign = 1, res = 0 int length = s.length() for (int i = 0 i