返回介绍

solution / 0500-0599 / 0592.Fraction Addition and Subtraction / README_EN

发布于 2024-06-17 01:03:36 字数 4805 浏览 0 评论 0 收藏 0

592. Fraction Addition and Subtraction

中文文档

Description

Given a string expression representing an expression of fraction addition and subtraction, return the calculation result in string format.

The final result should be an irreducible fraction. 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.

 

Example 1:

Input: expression = "-1/2+1/2"
Output: "0/1"

Example 2:

Input: expression = "-1/2+1/2+1/3"
Output: "1/3"

Example 3:

Input: expression = "1/3-1/2"
Output: "-1/6"

 

Constraints:

  • The input string only contains '0' to '9', '/', '+' and '-'. So does the output.
  • Each fraction (input and output) has the format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
  • 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.
  • The number of given fractions will be in the range [1, 10].
  • The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.

Solutions

Solution 1

class Solution:
  def fractionAddition(self, expression: str) -> str:
    x, y = 0, 6 * 7 * 8 * 9 * 10
    if expression[0].isdigit():
      expression = '+' + expression
    i, n = 0, len(expression)
    while i < n:
      sign = -1 if expression[i] == '-' else 1
      i += 1
      j = i
      while j < n and expression[j] not in '+-':
        j += 1
      s = expression[i:j]
      a, b = s.split('/')
      x += sign * int(a) * y // int(b)
      i = j
    z = gcd(x, y)
    x //= z
    y //= z
    return f'{x}/{y}'
class Solution {
  public String fractionAddition(String expression) {
    int x = 0, y = 6 * 7 * 8 * 9 * 10;
    if (Character.isDigit(expression.charAt(0))) {
      expression = "+" + expression;
    }
    int i = 0, n = expression.length();
    while (i < n) {
      int sign = expression.charAt(i) == '-' ? -1 : 1;
      ++i;
      int j = i;
      while (j < n && expression.charAt(j) != '+' && expression.charAt(j) != '-') {
        ++j;
      }
      String s = expression.substring(i, j);
      String[] t = s.split("/");
      int a = Integer.parseInt(t[0]), b = Integer.parseInt(t[1]);
      x += sign * a * y / b;
      i = j;
    }
    int z = gcd(Math.abs(x), y);
    x /= z;
    y /= z;
    return x + "/" + y;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }
}
func fractionAddition(expression string) string {
  x, y := 0, 6*7*8*9*10
  if unicode.IsDigit(rune(expression[0])) {
    expression = "+" + expression
  }
  i, n := 0, len(expression)
  for i < n {
    sign := 1
    if expression[i] == '-' {
      sign = -1
    }
    i++
    j := i
    for j < n && expression[j] != '+' && expression[j] != '-' {
      j++
    }
    s := expression[i:j]
    t := strings.Split(s, "/")
    a, _ := strconv.Atoi(t[0])
    b, _ := strconv.Atoi(t[1])
    x += sign * a * y / b
    i = j
  }
  z := gcd(abs(x), y)
  x /= z
  y /= z
  return fmt.Sprintf("%d/%d", x, y)
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}

func gcd(a, b int) int {
  if b == 0 {
    return a
  }
  return gcd(b, a%b)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文