227. Basic Calculator II Medium

@problem@discussion
#Math#String#Stack



1/**
2 * [227] Basic Calculator II
3 *
4 * Given a string s which represents an expression, evaluate this expression and return its value. 
5 * The integer division should truncate toward zero.
6 * You may assume that the given expression is always valid. All intermediate results will be in the range of [-2^31, 2^31 - 1].
7 * Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
8 *  
9 * Example 1:
10 * Input: s = "3+2*2"
11 * Output: 7
12 * Example 2:
13 * Input: s = " 3/2 "
14 * Output: 1
15 * Example 3:
16 * Input: s = " 3+5 / 2 "
17 * Output: 5
18 *  
19 * Constraints:
20 * 
21 * 	1 <= s.length <= 3 * 10^5
22 * 	s consists of integers and operators ('+', '-', '*', '/') separated by some number of spaces.
23 * 	s represents a valid expression.
24 * 	All the integers in the expression are non-negative integers in the range [0, 2^31 - 1].
25 * 	The answer is guaranteed to fit in a 32-bit integer.
26 * 
27 */
28pub struct Solution {}
29
30// problem: https://leetcode.com/problems/basic-calculator-ii/
31// discuss: https://leetcode.com/problems/basic-calculator-ii/discuss/?currentPage=1&orderBy=most_votes&query=
32
33// submission codes start here
34
35impl Solution {
36    pub fn calculate(s: String) -> i32 {
37        0
38    }
39}
40
41// submission codes end
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46
47    #[test]
48    fn test_227() {
49    }
50}
51


Back
© 2025 bowen.ge All Rights Reserved.