592. Fraction Addition and Subtraction Medium

@problem@discussion
#Math#String#Simulation



1/**
2 * [592] Fraction Addition and Subtraction
3 *
4 * Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.
5 * The final result should be an <a href="https://en.wikipedia.org/wiki/Irreducible_fraction" target="_blank">irreducible fraction</a>. If your final result is an integer, change it to the format of a fraction that has a denominator 1. So in this case, 2 should be converted to 2/1.
6 *  
7 * Example 1:
8 * 
9 * Input: expression = "-1/2+1/2"
10 * Output: "0/1"
11 * 
12 * Example 2:
13 * 
14 * Input: expression = "-1/2+1/2+1/3"
15 * Output: "1/3"
16 * 
17 * Example 3:
18 * 
19 * Input: expression = "1/3-1/2"
20 * Output: "-1/6"
21 * 
22 *  
23 * Constraints:
24 * 
25 * 	The input string only contains '0' to '9', '/', '+' and '-'. So does the output.
26 * 	Each fraction (input and output) has the format &plusmn;numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
27 * 	The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1, 10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
28 * 	The number of given fractions will be in the range [1, 10].
29 * 	The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.
30 * 
31 */
32pub struct Solution {}
33
34// problem: https://leetcode.com/problems/fraction-addition-and-subtraction/
35// discuss: https://leetcode.com/problems/fraction-addition-and-subtraction/discuss/?currentPage=1&orderBy=most_votes&query=
36
37// submission codes start here
38
39impl Solution {
40    pub fn fraction_addition(expression: String) -> String {
41        String::new()
42    }
43}
44
45// submission codes end
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_592() {
53    }
54}
55


Back
© 2025 bowen.ge All Rights Reserved.