日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

求方程的解 Solve the Equation

發布時間:2025/3/8 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 求方程的解 Solve the Equation 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么80%的碼農都做不了架構師?>>> ??

問題:

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"

解決:

① 解方程。難點在于處理字符串,如何將x的系數合并起來,將常數合并起來,化簡成ax=b的形式來求解。

例:
x + 5 -2x = 6-3x;
leftPart:tokens= {x,+ 5,-2x}; 系數為x = 1-2 = -1; 常數= 5;
rightPart:tokens = {6,-3x}; 系數為x = -3; 常數= 6;

最終結果=(6-5)/(-1 - ( - 3))

class Solution { //12ms
? ? public String solveEquation(String equation) {
? ? ? ? String[] parts = equation.split("=");
? ? ? ? int[] leftPart = evaluate(parts[0]);
? ? ? ? int[] rightPart = evaluate(parts[1]);
? ? ? ? if (leftPart[0] == rightPart[0] && leftPart[1] == rightPart[1]){
? ? ? ? ? ? return "Infinite solutions";
? ? ? ? }else if (leftPart[0] == rightPart[0]){
? ? ? ? ? ? return "No solution";
? ? ? ? }
? ? ? ? return "x=" + (rightPart[1] - leftPart[1]) / (leftPart[0] - rightPart[0]);
? ? }
? ? public int[] evaluate(String str){
? ? ? ? String[] tokens = str.split("(?=[+-])");//()匹配組; ?=匹配并包含在res中; [+ - ]表示+或 - ;
? ? ? ? int[] res = new int[2];//記錄系數為x; 系數為常數
? ? ? ? for (String token : tokens){
? ? ? ? ? ? if (token.equals("+x") || token.equals("x")){
? ? ? ? ? ? ? ? res[0] ++;// x表示1x
? ? ? ? ? ? }else if (token.equals("-x")){
? ? ? ? ? ? ? ? res[0] --;
? ? ? ? ? ? }else if (token.contains("x")){
? ? ? ? ? ? ? ? res[0] += Integer.parseInt(token.substring(0,token.length() - 1));
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? res[1] += Integer.parseInt(token);
? ? ? ? ? ? }

? ? ? ? }
? ? ? ? return res;
? ? }
}

② 在discuss中看到的,思路相同。

class Solution { //9ms
? ? public String solveEquation(String equation) {
? ? ? ? if(equation == null || equation.length() == 0) return "No solution";
? ? ? ? String[] sides = equation.split("=");
? ? ? ? int[] left = parse(sides[0]), right = parse(sides[1]);
? ? ? ? if(left[0] == right[0]) {
? ? ? ? ? ? if(left[1] == right[1]) return "Infinite solutions";
? ? ? ? ? ? return "No solution";
? ? ? ? }
? ? ? ? int val = (right[1] - left[1]) / (left[0] - right[0]);
? ? ? ? return "x=" + val;
? ? }
? ??
? ? private int[] parse(String s) {
? ? ? ? int sign = 1, val = 0;
? ? ? ? int[] res = new int[2];
? ? ? ? for(int i = 0; i < s.length(); i ++) {
? ? ? ? ? ? char c = s.charAt(i);
? ? ? ? ? ? if(c == 'x') {
? ? ? ? ? ? ? ? if(i == 0) res[0] ++;
? ? ? ? ? ? ? ? else if(s.charAt(i-1) == '-' || s.charAt(i-1) == '+') res[0] += sign;
? ? ? ? ? ? ? ? else res[0] += sign * val;
? ? ? ? ? ? ? ? val = 0;
? ? ? ? ? ? } else if(c == '-' || c == '+') {
? ? ? ? ? ? ? ? res[1] += sign * val;
? ? ? ? ? ? ? ? val = 0;
? ? ? ? ? ? ? ? sign = c == '-' ? -1 : 1;
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? val = val * 10 + c - '0';
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? res[1] += sign * val;
? ? ? ? return res;
? ? }
}

轉載于:https://my.oschina.net/liyurong/blog/1606460

總結

以上是生活随笔為你收集整理的求方程的解 Solve the Equation的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。