224. Basic Calculator Hard

@problem@discussion
#Math#String#Stack#Recursion



1/**
2 * [224] Basic Calculator
3 *
4 * Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
5 * Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
6 *  
7 * Example 1:
8 * 
9 * Input: s = "1 + 1"
10 * Output: 2
11 * 
12 * Example 2:
13 * 
14 * Input: s = " 2-1 + 2 "
15 * Output: 3
16 * 
17 * Example 3:
18 * 
19 * Input: s = "(1+(4+5+2)-3)+(6+8)"
20 * Output: 23
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	1 <= s.length <= 3 * 10^5
26 * 	s consists of digits, '+', '-', '(', ')', and ' '.
27 * 	s represents a valid expression.
28 * 	'+' is not used as a unary operation (i.e., "+1" and "+(2 + 3)" is invalid).
29 * 	'-' could be used as a unary operation (i.e., "-1" and "-(2 + 3)" is valid).
30 * 	There will be no two consecutive operators in the input.
31 * 	Every number and running calculation will fit in a signed 32-bit integer.
32 * 
33 */
34pub struct Solution {}
35
36// problem: https://leetcode.com/problems/basic-calculator/
37// discuss: https://leetcode.com/problems/basic-calculator/discuss/?currentPage=1&orderBy=most_votes&query=
38
39// submission codes start here
40
41impl Solution {
42    pub fn calculate(s: String) -> i32 {
43        0
44    }
45}
46
47// submission codes end
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn test_224() {
55    }
56}
57


Back
© 2025 bowen.ge All Rights Reserved.