国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 640. Solve the Equation

pf_miles / 1033人閱讀

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;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://m.specialneedsforspecialkids.com/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元查看
<