顺序遍历,str / strconv.Itoa / sprintf(_, "x=%d", 1) / to_string 数字转字符串,求解《640. 求解方程》

例题

640. 求解方程

求解一个给定的方程,将x以字符串 "x=#value" 的形式返回。该方程仅包含 '+' , '-' 操作,变量 x 和其对应系数。
如果方程没有解,请返回 "No solution" 。如果方程有无限解,则返回 “Infinite solutions” 。
如果方程中只有一个解,要保证返回值 'x' 是一个整数。
示例 1:
输入: equation = "x+5-3+x=6+x-2"
输出: "x=2"

答案

顺序遍历

var solveEquation = function(equation) {
  const n = equation.length
  let x = v = t = 0, reverse = sign = 1
  for (let i = 0; i < n; i++) {
    const e = equation[i]
    if (e >= '0' && e <= '9')  t = t * 10 + +e
    else if (e === 'x') {
      x += reverse * sign * (i > 0 && equation[i - 1] === '0' ? 0 : t || 1)
      t = 0
    } else {
        v += reverse * sign * t
        t = 0
        if (e === '-') sign = -1
        else {
          sign = 1
          if (e === '=') reverse = -1
        }
    }
  }
  v += reverse * sign * t
  if (x === 0) return v === 0 ? 'Infinite solutions' : 'No solution'
  if (v % x !== 0) return 'No solution'
  return 'x=' + (-v / x | 0)
};
function solveEquation(equation: string): string {
  const n = equation.length
  let x = 0, v = 0, t = 0, reverse = 1, sign = 1
  for (let i = 0; i < n; i++) {
    const e = equation[i]
    if (e >= '0' && e <= '9') t = t * 10 + +e
    else if (e === 'x') {
      x += reverse * sign * (i > 0 && equation[i - 1] === '0' ? 0 : t || 1)
      t = 0
    } else {
      v += reverse * sign * t
      t = 0
      if (e === '=') reverse = -1
      if (e === '-') sign = -1
      else sign = 1
    }
  }
  v += reverse * sign * t
  if (x === 0) return v === 0 ? 'Infinite solutions' : 'No solution'
  if (v % x !== 0) return 'No solution'
  return 'x=' + -v / x
};
class Solution {
  function solveEquation($equation) {
    $n = strlen($equation);
    $x = $v = $t = 0;
    $reverse = $sign = 1;
    for ($i = 0; $i < $n; $i++) {
      $e = $equation[$i];
      if ($e >= '0' && $e <= '9') $t = $t * 10 + +$e;
      else if ($e === 'x') {
        $x += $reverse * $sign * ($i > 0 && $equation[$i - 1] === '0' ? 0 : ($t ? $t : 1));
        $t = 0;
      } else {
        $v += $reverse * $sign * $t;
        $t = 0;
        if ($e === '=') $reverse = -1;
        if ($e === '-') $sign = -1;
        else $sign = 1;
      }
    }
    $v += $reverse * $sign * $t;
    if ($x === 0) return $v === 0 ? 'Infinite solutions' : 'No solution';
    if ($v % $x !== 0) return 'No solution';
    return 'x=' . (-$v / $x);
  }
}
func solveEquation(equation string) string {
  t, x, v, reverse, sign := 0, 0, 0, 1, 1
  for i, e := range equation {
    if e >= '0' && e <= '9' {
      t = t * 10 + int(e) - int('0')
    } else if e == 'x' {
      if i == 0 || equation[i - 1] != '0' {
        if t == 0 {
          t = 1
        }  
        x += reverse * sign * t 
        t = 0
      }
    } else {
      v += reverse * sign * t
      t = 0
      if e == '=' {
        reverse = -1
      }
      if e == '-' {
        sign = -1
      } else {
        sign = 1
      }
    }
  }
  v += reverse * sign * t
  if x == 0 {
    if v == 0 {
      return "Infinite solutions"
    } else {
      return "No solution"
    }
  }
  if v % x != 0 {
    return "No solution"
  }
  return "x=" + strconv.Itoa(-v / x)
}
class Solution:
  def solveEquation(self, equation: str) -> str:
    x, v, t, reverse, sign = 0, 0, 0, 1, 1
    for i, e in enumerate(equation):
      if e >= '0' and e <= '9': t = t * 10 + int(e) - int('0')
      elif e == 'x':
        x += reverse * sign * (0 if i > 0 and equation[i - 1] == '0' else t if t else 1)
        t = 0
      else:
        v += reverse * sign * t
        t = 0
        if e == '=': reverse = -1
        if e == '-': sign = -1
        else: sign = 1
    v += reverse * sign * t
    if x == 0: return 'Infinite solutions' if v == 0 else 'No solution'
    if v % x != 0: return 'No solution'
    return 'x=' + str(-v // x)
class Solution {
  public String solveEquation(String equation) {
    int n = equation.length(), x = 0, v = 0, t = 0, reverse = 1, sign = 1;
    for (int i = 0; i < n; i++) {
      char e = equation.charAt(i);
      if (e >= '0' && e <= '9') t = t * 10 + (e - '0');
      else if (e == 'x') {
        x += reverse * sign * (i > 0 && equation.charAt(i - 1) == '0' ? 0 : t != 0 ? t : 1);
        t = 0;
      } else {
        v += reverse * sign * t;
        t = 0;
        if (e == '=') reverse = -1;
        if (e == '-') sign = -1;
        else sign = 1;
      }
    }
    v += reverse * sign * t;
    if (x == 0) return v == 0 ? "Infinite solutions" : "No solution";
    if (v % x != 0) return "No solution";
    return "x=" + (-v / x);
  }
}
public class Solution {
  public string SolveEquation(string equation) {
    int n = equation.Length, x = 0, v = 0, t = 0, reverse = 1, sign = 1;
    for (int i = 0; i < n; i++) {
      char e = equation[i];
      if (e >= '0' && e <= '9') t = t * 10 + (e - '0');
      else if (e == 'x') {
        x += reverse * sign * (i > 0 && equation[i - 1] == '0' ? 0 : t != 0 ? t : 1);
        t = 0;
      } else {
        v += reverse * sign * t;
        t = 0;
        if (e == '=') reverse = -1;
        if (e == '-') sign = -1;
        else sign = 1;
      }
    }
    v += reverse * sign * t;
    if (x == 0) return v == 0 ? "Infinite solutions" : "No solution";
    if (v % x != 0) return "No solution";
    return "x=" + (-v / x);
  }
}
char * solveEquation(char * equation){
  int n = strlen(equation), x = 0, v = 0, t = 0, reverse = 1, sign = 1;
  for (int i = 0; i < n; i++) {
    char e = equation[i];
    if (e >= '0' && e <= '9') t = t * 10 + (e - '0');
    else if (e == 'x') {
      x += reverse * sign * (i > 0 && equation[i - 1] == '0' ? 0 : t ? t : 1);
      t = 0;
    } else {
      v += reverse * sign * t;
      t = 0;
      if (e == '=') reverse = -1;
      if (e == '-') sign = -1;
      else sign = 1;
    }
  }
  v += reverse * sign * t;
  if (x == 0) return v == 0 ? "Infinite solutions" : "No solution";
  if (v % x != 0) return "No solution";
  char* r = malloc(sizeof(char) * 32);
  sprintf(r, "x=%d", -v / x);
  return r;
}
class Solution {
public:
  string solveEquation(string equation) {
    int n = equation.size(), x = 0, v = 0, t = 0, reverse = 1, sign = 1;
    for (int i = 0; i < n; i++) {
      char e = equation[i];
      if (e >= '0' && e <= '9') t = t * 10 + (e - '0');
      else if (e == 'x') {
        x += reverse * sign * (i > 0 && equation[i - 1] == '0' ? 0 : t ? t : 1);
        t = 0;
      } else {
        v += reverse * sign * t;
        t = 0;
        if (e == '=') reverse = -1;
        if (e == '-') sign = -1;
        else sign = 1;
      }
    }
    v += reverse * sign * t;
    if (x == 0) return v == 0 ? "Infinite solutions" : "No solution";
    if (v % x != 0) return "No solution";
    return "x=" + to_string(-v / x);
  }
};

哈希表(用 uthash / Dictionary / dict / HashMap / unordered_map 等 + 定长列表实现),排序 + 拼接字符串 2 算法,求解《1460. 通过翻转子数组使两个数组相等》
哈希表(用 uthash / Dictionary / dict / HashMap / unordered_map 等 + 定长列表实现),排序 + 拼接字符串 2 算法,用 join / implode / fmt.Sprint 将列表转字符串比较,用 Array.equal/ (int[]).SequenceEqual 比较列表,C++ 直接比较 vector<……
位图(位集),0b 和 C# Convert.ToInt32("字符串", 2) 表示二进制数字,求解《782. 变为棋盘》
位图(位集),0b 和 C# Convert.ToInt32("字符串", 2) 表示二进制数字,求解《782. 变为棋盘》
广度优先搜索,深度优先搜索(前序 / 中序(含莫里斯) / 后序 遍历,递归和迭代(单栈 / 双栈)),数字转为字符串,求解《655. 输出二叉树》
广度优先搜索,深度优先搜索(前序 / 中序(含莫里斯) / 后序 遍历,递归和迭代(单栈 / 双栈)),''+ int / strconv.Itoa / Integer.toString / int.ToString / sprintf(char*, "%d", int) / to_string / str 将数字转为字符串,求解《655. 输出二叉树》