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"
這破題只有一個考點:正則表達(dá)式
Solutionclass 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)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/72082.html
摘要:題目要求已知一些字母之間的關(guān)系式,問是否能夠計算出其它字母之間的倍數(shù)關(guān)系如已知問是否能夠計算出的值。這里的值因為在條件中無法獲知是否等于零,因此也無法計算其真實結(jié)果,也需要返回。即代表點指向點的邊的權(quán)重為,而點指向點的邊的全中為。 題目要求 Equations are given in the format A / B = k, where A and B are variables ...
摘要:本篇博客將介紹該公式及其應(yīng)用,首先我們來看一下該公式的內(nèi)容及其證明。應(yīng)用循環(huán)三對角線性方程組的求解本篇博客將詳細(xì)講述公式在循環(huán)三對角線性方程組的求解中的應(yīng)用。 Sherman-Morrison公式 ??Sherman-Morrison公式以 Jack Sherman 和 Winifred J. Morrison命名,在線性代數(shù)中,是求解逆矩陣的一種方法。本篇博客將介紹該公式及其應(yīng)用,首...
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 ...
摘要:題目要求也就是給出一個解決數(shù)獨的方法,假設(shè)每個數(shù)獨只有一個解決方案。并將當(dāng)前的假象結(jié)果帶入下一輪的遍歷之中。如果最終遍歷失敗,則逐級返回,恢復(fù)上一輪遍歷的狀態(tài),并填入下一個合理的假想值后繼續(xù)遍歷。 題目要求 Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indi...
摘要:建好圖之后就是查找了,圖里面查找用或者都可以,寫起來簡單點。復(fù)雜度沒什么差別都是,這道題里面最多是,所以每次查找的復(fù)雜度是,有次查找。注意防止重復(fù)路徑,要用。 399. Evaluate Division 題目鏈接:https://leetcode.com/problems... 無向圖里找路徑的問題,用鄰接鏈或者鄰接矩陣來建圖,用鄰接鏈的話注意兩個方向,a/b的時候,既要把b加到a的...
閱讀 2504·2019-08-30 15:52
閱讀 2302·2019-08-30 12:51
閱讀 2899·2019-08-29 18:41
閱讀 2879·2019-08-29 17:04
閱讀 897·2019-08-29 15:11
閱讀 1826·2019-08-28 18:02
閱讀 3659·2019-08-26 10:22
閱讀 2573·2019-08-26 10:12