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"
這破題只有一個(gè)考點(diǎn):正則表達(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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72082.html
摘要:題目要求已知一些字母之間的關(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 ...
摘要:本篇博客將介紹該公式及其應(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)用,首...
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 ...
摘要:題目要求也就是給出一個(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...
摘要:建好圖之后就是查找了,圖里面查找用或者都可以,寫(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的...
閱讀 2436·2019-08-30 15:52
閱讀 2237·2019-08-30 12:51
閱讀 2833·2019-08-29 18:41
閱讀 2812·2019-08-29 17:04
閱讀 811·2019-08-29 15:11
閱讀 1720·2019-08-28 18:02
閱讀 3603·2019-08-26 10:22
閱讀 2510·2019-08-26 10:12