class Solution {
public:
string fractionAddition(string expression) {
}
};
592. 分数加减运算
给定一个表示分数加减运算的字符串 expression
,你需要返回一个字符串形式的计算结果。
这个结果应该是不可约分的分数,即最简分数。 如果最终结果是一个整数,例如 2
,你需要将它转换成分数形式,其分母为 1
。所以在上述例子中, 2
应该被转换为 2/1
。
示例 1:
输入: expression
= "-1/2+1/2"
输出: "0/1"
示例 2:
输入: expression
= "-1/2+1/2+1/3"
输出: "1/3"
示例 3:
输入: expression
= "1/3-1/2"
输出: "-1/6"
提示:
'0'
到 '9'
的数字,以及 '/'
, '+'
和 '-'
。 ±分子/分母
。如果输入的第一个分数或者输出的分数是正数,则 '+'
会被省略掉。相似题目
原站题解
python3 解法, 执行用时: 32 ms, 内存消耗: 14.9 MB, 提交时间: 2022-12-14 12:41:47
class Solution: def fractionAddition(self, expression: str) -> str: # 可以不单写成函数,放在同一个循环里更简洁 def read_number(i: int): sign, numerator, denominator = -1 if expression[i] == '-' else 1, 0, 0 if expression[i] in "+-": i += 1 while i < len(expression) and expression[i] != '/': numerator = 10 * numerator + int(expression[i]) i += 1 i += 1 while i < len(expression) and expression[i] not in "+-": denominator = 10 * denominator + int(expression[i]) i += 1 return sign * numerator, denominator if denominator else 1, i idx = numer = 0 denom = 1 while idx < len(expression): num, den, idx = read_number(idx) lm = lcm(denom, den) numer, denom = numer * lm // denom + num * lm // den, lm g = gcd(abs(numer), denom) return f"{numer // g}/{denom // g}"
javascript 解法, 执行用时: 64 ms, 内存消耗: 41.1 MB, 提交时间: 2022-12-14 12:41:21
/** * @param {string} expression * @return {string} */ var fractionAddition = function(expression) { let x = 0, y = 1; // 分子,分母 let index = 0, n = expression.length; while (index < n) { // 读取分子 let x1 = 0, sign = 1; if (expression[index] === '-' || expression[index] === '+') { sign = expression[index] === '-' ? -1 : 1; index++; } while (index < n && isDigit(expression[index])) { x1 = x1 * 10 + expression[index].charCodeAt() - '0'.charCodeAt(); index++; } x1 = sign * x1; index++; // 读取分母 let y1 = 0; while (index < n && isDigit(expression[index])) { y1 = y1 * 10 + expression[index].charCodeAt() - '0'.charCodeAt(); index++; } x = x * y1 + x1 * y; y *= y1; } if (x === 0) { return "0/1"; } const g = gcd(Math.abs(x), y); // 获取最大公约数 return Math.floor(x / g) + "/" + Math.floor(y / g); } const gcd = (a, b) => { let remainder = a % b; while (remainder !== 0) { a = b; b = remainder; remainder = a % b; } return b; }; const isDigit = (ch) => { return parseFloat(ch).toString() === "NaN" ? false : true; }
golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2022-12-14 12:41:06
func fractionAddition(expression string) string { x, y := 0, 1 // 分子,分母 for i, n := 0, len(expression); i < n; { // 读取分子 x1, sign := 0, 1 if expression[i] == '-' || expression[i] == '+' { if expression[i] == '-' { sign = -1 } i++ } for i < n && unicode.IsDigit(rune(expression[i])) { x1 = x1*10 + int(expression[i]-'0') i++ } x1 = sign * x1 i++ // 读取分母 y1 := 0 for i < n && unicode.IsDigit(rune(expression[i])) { y1 = y1*10 + int(expression[i]-'0') i++ } x = x*y1 + x1*y y *= y1 } if x == 0 { return "0/1" } g := gcd(abs(x), y) return fmt.Sprintf("%d/%d", x/g, y/g) } func gcd(a, b int) int { for a != 0 { a, b = b%a, a } return b } func abs(x int) int { if x < 0 { return -x } return x }
java 解法, 执行用时: 4 ms, 内存消耗: 39.9 MB, 提交时间: 2022-12-14 12:40:50
class Solution { public String fractionAddition(String expression) { long x = 0, y = 1; // 分子,分母 int index = 0, n = expression.length(); while (index < n) { // 读取分子 long x1 = 0, sign = 1; if (expression.charAt(index) == '-' || expression.charAt(index) == '+') { sign = expression.charAt(index) == '-' ? -1 : 1; index++; } while (index < n && Character.isDigit(expression.charAt(index))) { x1 = x1 * 10 + expression.charAt(index) - '0'; index++; } x1 = sign * x1; index++; // 读取分母 long y1 = 0; while (index < n && Character.isDigit(expression.charAt(index))) { y1 = y1 * 10 + expression.charAt(index) - '0'; index++; } x = x * y1 + x1 * y; y *= y1; } if (x == 0) { return "0/1"; } long g = gcd(Math.abs(x), y); // 获取最大公约数 return Long.toString(x / g) + "/" + Long.toString(y / g); } public long gcd(long a, long b) { long remainder = a % b; while (remainder != 0) { a = b; b = remainder; remainder = a % b; } return b; } }
python3 解法, 执行用时: 36 ms, 内存消耗: 14.9 MB, 提交时间: 2022-12-14 12:39:55
class Solution: def fractionAddition(self, expression: str) -> str: x, y = 0, 1 # 分子,分母 i, n = 0, len(expression) while i < n: # 读取分子 x1, sign = 0, 1 if expression[i] == '-' or expression[i] == '+': if expression[i] == '-': sign = -1 i += 1 while i < n and expression[i].isdigit(): x1 = x1 * 10 + int(expression[i]) i += 1 x1 = sign * x1 i += 1 # 读取分母 y1 = 0 while i < n and expression[i].isdigit(): y1 = y1 * 10 + int(expression[i]) i += 1 x = x * y1 + x1 * y y *= y1 if x == 0: return "0/1" g = gcd(abs(x), y) return f"{x // g}/{y // g}"