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

資訊專(zhuān)欄INFORMATION COLUMN

[LeetCode] 640. Solve the Equation

pf_miles / 1025人閱讀

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

這破題只有一個(gè)考點(diǎn):正則表達(dá)式

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

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72082.html

相關(guān)文章

  • leetcode399. Evaluate Division

    摘要:題目要求已知一些字母之間的關(guān)系式,問(wèn)是否能夠計(jì)算出其它字母之間的倍數(shù)關(guān)系如已知問(wèn)是否能夠計(jì)算出的值。這里的值因?yàn)樵跅l件中無(wú)法獲知是否等于零,因此也無(wú)法計(jì)算其真實(shí)結(jié)果,也需要返回。即代表點(diǎn)指向點(diǎn)的邊的權(quán)重為,而點(diǎn)指向點(diǎn)的邊的全中為。 題目要求 Equations are given in the format A / B = k, where A and B are variables ...

    Jensen 評(píng)論0 收藏0
  • Sherman-Morrison公式及其應(yīng)用

    摘要:本篇博客將介紹該公式及其應(yīng)用,首先我們來(lái)看一下該公式的內(nèi)容及其證明。應(yīng)用循環(huán)三對(duì)角線性方程組的求解本篇博客將詳細(xì)講述公式在循環(huán)三對(duì)角線性方程組的求解中的應(yīng)用。 Sherman-Morrison公式 ??Sherman-Morrison公式以 Jack Sherman 和 Winifred J. Morrison命名,在線性代數(shù)中,是求解逆矩陣的一種方法。本篇博客將介紹該公式及其應(yīng)用,首...

    lookSomeone 評(píng)論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 評(píng)論0 收藏0
  • leetcode37 Sudoku Solver

    摘要:題目要求也就是給出一個(gè)解決數(shù)獨(dú)的方法,假設(shè)每個(gè)數(shù)獨(dú)只有一個(gè)解決方案。并將當(dāng)前的假象結(jié)果帶入下一輪的遍歷之中。如果最終遍歷失敗,則逐級(jí)返回,恢復(fù)上一輪遍歷的狀態(tài),并填入下一個(gè)合理的假想值后繼續(xù)遍歷。 題目要求 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indi...

    pepperwang 評(píng)論0 收藏0
  • 399. Evaluate Division

    摘要:建好圖之后就是查找了,圖里面查找用或者都可以,寫(xiě)起來(lái)簡(jiǎn)單點(diǎn)。復(fù)雜度沒(méi)什么差別都是,這道題里面最多是,所以每次查找的復(fù)雜度是,有次查找。注意防止重復(fù)路徑,要用。 399. Evaluate Division 題目鏈接:https://leetcode.com/problems... 無(wú)向圖里找路徑的問(wèn)題,用鄰接鏈或者鄰接矩陣來(lái)建圖,用鄰接鏈的話注意兩個(gè)方向,a/b的時(shí)候,既要把b加到a的...

    yanest 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

pf_miles

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<