资讯专栏INFORMATION COLUMN

[LeetCode] 640. Solve the Equation

pf_miles / 867人阅读

Problem

Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only "+", "-" operation, the variable x and its coefficient.

If there is no solution for the equation, return "No solution".

If there are infinite solutions for the equation, return "Infinite solutions".

If there is exactly one solution for the equation, we ensure that the value of x is an integer.

Example 1:
Input: "x+5-3+x=6+x-2"
Output: "x=2"
Example 2:
Input: "x=x"
Output: "Infinite solutions"
Example 3:
Input: "2x=x"
Output: "x=0"
Example 4:
Input: "2x+3x-6x=x+2"
Output: "x=-1"
Example 5:
Input: "x=x+2"
Output: "No solution"

Note

这破题只有一个考点:正则表达式

Solution
class Solution {
    public String solveEquation(String equation) {
        String[] sides = equation.split("=");
        String left = sides[0], right = sides[1];
        int[] ls = getCounts(left);
        int[] rs = getCounts(right);
        int countX = ls[0]-rs[0];
        int countNum = rs[1]-ls[1];
        if (countX == 0) {
            if (countNum == 0) return "Infinite solutions";
            else return "No solution";
        }
        
        int x = countNum/countX;
        StringBuilder sb = new StringBuilder();
        sb.append("x=").append(x);
        return sb.toString();
    }
    private int[] getCounts(String str) {
        int[] res = new int[2];
        String[] parts = str.split("(?=[+-])"); //this is what this problem is
        for (String part: parts) {
            if (part.equals("x") || part.equals("+x")) res[0]++;
            else if (part.equals("-x")) res[0]--;
            else if (part.contains("x")) res[0] += Integer.valueOf(part.substring(0, part.indexOf("x")));
            else res[1] += Integer.valueOf(part);
        }
        return res;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72082.html

相关文章

  • leetcode399. Evaluate Division

    摘要:题目要求已知一些字母之间的关系式,问是否能够计算出其它字母之间的倍数关系如已知问是否能够计算出的值。这里的值因为在条件中无法获知是否等于零,因此也无法计算其真实结果,也需要返回。即代表点指向点的边的权重为,而点指向点的边的全中为。 题目要求 Equations are given in the format A / B = k, where A and B are variables ...

    Jensen 评论0 收藏0
  • Sherman-Morrison公式及其应用

    摘要:本篇博客将介绍该公式及其应用,首先我们来看一下该公式的内容及其证明。应用循环三对角线性方程组的求解本篇博客将详细讲述公式在循环三对角线性方程组的求解中的应用。 Sherman-Morrison公式   Sherman-Morrison公式以 Jack Sherman 和 Winifred J. Morrison命名,在线性代数中,是求解逆矩阵的一种方法。本篇博客将介绍该公式及其应用,首...

    lookSomeone 评论0 收藏0
  • [LeetCode] 37. Sudoku Solver

    Problem Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row.Each ...

    alaege 评论0 收藏0
  • leetcode37 Sudoku Solver

    摘要:题目要求也就是给出一个解决数独的方法,假设每个数独只有一个解决方案。并将当前的假象结果带入下一轮的遍历之中。如果最终遍历失败,则逐级返回,恢复上一轮遍历的状态,并填入下一个合理的假想值后继续遍历。 题目要求 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indi...

    pepperwang 评论0 收藏0
  • 399. Evaluate Division

    摘要:建好图之后就是查找了,图里面查找用或者都可以,写起来简单点。复杂度没什么差别都是,这道题里面最多是,所以每次查找的复杂度是,有次查找。注意防止重复路径,要用。 399. Evaluate Division 题目链接:https://leetcode.com/problems... 无向图里找路径的问题,用邻接链或者邻接矩阵来建图,用邻接链的话注意两个方向,a/b的时候,既要把b加到a的...

    yanest 评论0 收藏0

发表评论

0条评论

pf_miles

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<