|
49 | 49 |
|
50 | 50 | <!-- 这里可写通用的实现逻辑 -->
|
51 | 51 |
|
| 52 | +**方法一:数学** |
| 53 | + |
52 | 54 | <!-- tabs:start -->
|
53 | 55 |
|
54 | 56 | ### **Python3**
|
55 | 57 |
|
56 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
57 | 59 |
|
58 | 60 | ```python
|
59 |
| - |
| 61 | +class Solution: |
| 62 | + def fractionAddition(self, expression: str) -> str: |
| 63 | + x, y = 0, 6 * 7 * 8 * 9 * 10 |
| 64 | + if expression[0].isdigit(): |
| 65 | + expression = '+' + expression |
| 66 | + i, n = 0, len(expression) |
| 67 | + while i < n: |
| 68 | + sign = -1 if expression[i] == '-' else 1 |
| 69 | + i += 1 |
| 70 | + j = i |
| 71 | + while j < n and expression[j] not in '+-': |
| 72 | + j += 1 |
| 73 | + s = expression[i: j] |
| 74 | + a, b = s.split('/') |
| 75 | + x += sign * int(a) * y // int(b) |
| 76 | + i = j |
| 77 | + z = gcd(x, y) |
| 78 | + x //= z |
| 79 | + y //= z |
| 80 | + return f'{x}/{y}' |
60 | 81 | ```
|
61 | 82 |
|
62 | 83 | ### **Java**
|
63 | 84 |
|
64 | 85 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
65 | 86 |
|
66 | 87 | ```java
|
| 88 | +class Solution { |
| 89 | + public String fractionAddition(String expression) { |
| 90 | + int x = 0, y = 6 * 7 * 8 * 9 * 10; |
| 91 | + if (Character.isDigit(expression.charAt(0))) { |
| 92 | + expression = "+" + expression; |
| 93 | + } |
| 94 | + int i = 0, n = expression.length(); |
| 95 | + while (i < n) { |
| 96 | + int sign = expression.charAt(i) == '-' ? -1 : 1; |
| 97 | + ++i; |
| 98 | + int j = i; |
| 99 | + while (j < n && expression.charAt(j) != '+' && expression.charAt(j) != '-') { |
| 100 | + ++j; |
| 101 | + } |
| 102 | + String s = expression.substring(i, j); |
| 103 | + String[] t = s.split("/"); |
| 104 | + int a = Integer.parseInt(t[0]), b = Integer.parseInt(t[1]); |
| 105 | + x += sign * a * y / b; |
| 106 | + i = j; |
| 107 | + } |
| 108 | + int z = gcd(Math.abs(x), y); |
| 109 | + x /= z; |
| 110 | + y /= z; |
| 111 | + return x + "/" + y; |
| 112 | + } |
| 113 | + |
| 114 | + private int gcd(int a, int b) { |
| 115 | + return b == 0 ? a : gcd(b, a % b); |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
67 | 119 |
|
| 120 | +### **Go** |
| 121 | + |
| 122 | +```go |
| 123 | +func fractionAddition(expression string) string { |
| 124 | + x, y := 0, 6*7*8*9*10 |
| 125 | + if unicode.IsDigit(rune(expression[0])) { |
| 126 | + expression = "+" + expression |
| 127 | + } |
| 128 | + i, n := 0, len(expression) |
| 129 | + for i < n { |
| 130 | + sign := 1 |
| 131 | + if expression[i] == '-' { |
| 132 | + sign = -1 |
| 133 | + } |
| 134 | + i++ |
| 135 | + j := i |
| 136 | + for j < n && expression[j] != '+' && expression[j] != '-' { |
| 137 | + j++ |
| 138 | + } |
| 139 | + s := expression[i:j] |
| 140 | + t := strings.Split(s, "/") |
| 141 | + a, _ := strconv.Atoi(t[0]) |
| 142 | + b, _ := strconv.Atoi(t[1]) |
| 143 | + x += sign * a * y / b |
| 144 | + i = j |
| 145 | + } |
| 146 | + z := gcd(abs(x), y) |
| 147 | + x /= z |
| 148 | + y /= z |
| 149 | + return fmt.Sprintf("%d/%d", x, y) |
| 150 | +} |
| 151 | + |
| 152 | +func abs(x int) int { |
| 153 | + if x < 0 { |
| 154 | + return -x |
| 155 | + } |
| 156 | + return x |
| 157 | +} |
| 158 | + |
| 159 | +func gcd(a, b int) int { |
| 160 | + if b == 0 { |
| 161 | + return a |
| 162 | + } |
| 163 | + return gcd(b, a%b) |
| 164 | +} |
68 | 165 | ```
|
69 | 166 |
|
70 | 167 | ### **...**
|
|
0 commit comments