Leetcode 640.求解方程

求解方程

求解一个给定的方程,将x以字符串"x=#value"的形式返回。该方程仅包含‘+‘,‘ - ‘操作,变量 x 和其对应系数。

如果方程没有解,请返回"No solution"。

如果方程有无限解,则返回"Infinite solutions"。

如果方程中只有一个解,要保证返回值 x 是一个整数。

示例 1:

输入: "x+5-3+x=6+x-2"

输出: "x=2"

示例 2:

输入: "x=x"

输出: "Infinite solutions"

示例 3:

输入: "2x=x"

输出: "x=0"

示例 4:

输入: "2x+3x-6x=x+2"

输出: "x=-1"

示例 5:

输入: "x=x+2"

输出: "No solution"

 1 public class Solution {
 2     public String coeff(String x) {
 3         if (x.length() > 1 && x.charAt(x.length() - 2) >= ‘0‘ && x.charAt(x.length() - 2) <= ‘9‘)
 4             return x.replace("x", "");
 5         return x.replace("x", "1");
 6     }
 7     public String solveEquation(String equation) {
 8         String[] lr = equation.split("=");
 9         int lhs = 0, rhs = 0;
10         for (String x: breakIt(lr[0])) {
11             if (x.indexOf("x") >= 0) {
12                 lhs += Integer.parseInt(coeff(x));
13             } else
14                 rhs -= Integer.parseInt(x);
15         }
16         for (String x: breakIt(lr[1])) {
17             if (x.indexOf("x") >= 0)
18                 lhs -= Integer.parseInt(coeff(x));
19             else
20                 rhs += Integer.parseInt(x);
21         }
22         if (lhs == 0) {
23             if (rhs == 0)
24                 return "Infinite solutions";
25             else
26                 return "No solution";
27         }
28         return "x=" + rhs / lhs;
29     }
30     public List < String > breakIt(String s) {
31         List < String > res = new ArrayList < > ();
32         String r = "";
33         for (int i = 0; i < s.length(); i++) {
34             if (s.charAt(i) == ‘+‘ || s.charAt(i) == ‘-‘) {
35                 if (r.length() > 0)
36                     res.add(r);
37                 r = "" + s.charAt(i);
38             } else
39                 r += s.charAt(i);
40         }
41         res.add(r);
42         return res;
43     }
44 }

原文地址:https://www.cnblogs.com/kexinxin/p/10383055.html

时间: 2024-07-30 22:09:01

Leetcode 640.求解方程的相关文章

遗传算法的简单应用-求解方程

上篇初识遗传算法讲述了遗传算法的基本思想,这篇博客就用遗传算法求解方程. 具体的如下: 求解方程 -x^3+7*x+13=0 在[3,4]区间的解,解精确到0.001,交叉概率0.7 变异概率0.01,迭代次数为100,字符编码长度为10(二进制编码) 首先简单的分析一下: 1.编码与解码 题目要求的是采用二进制的编码方式来实现,既然已经编码了,自然就需要解码,给定的10 位二进制编码表示的区间范围就是0~1023,题目的区间是[3,4]很自然的就能想到10位二进 制编码中的0表示是就是[3,4

牛顿法求解方程

目的:求解方程{\displaystyle f(x)=0}的根

C++ 二分法求解方程的解

二分法是一种求解方程近似根的方法.对于一个函数 f(x)f(x),使用二分法求 f(x)f(x) 近似解的时候,我们先设定一个迭代区间(在这个题目上,我们之后给出了的两个初值决定的区间 [-20,20][?20,20]),区间两端自变量 xx 的值对应的 f(x)f(x) 值是异号的,之后我们会计算出两端 xx 的中点位置 x'x′ 所对应的 f(x')f(x′) ,然后更新我们的迭代区间,确保对应的迭代区间的两端 xx 的值对应的 f(x)f(x) 值还会是异号的. 重复这个过程直到我们某一次

用弦截法求解方程的根

//弦截法求解方程的根 //要求:输入左右两个端点x值 //结果:在一定精度范围内求解出方程的根 //难点:1)推导出x处的横坐标的求解公式 2)迭代掉原来的左端点或者右端点 #include "pch.h" #include <iostream> #include <cmath> #include <iomanip> using namespace std; double f(double x); double xpoint(double x1,

高斯消元法求解方程

#include<iostream> #include<math.h> #include<vector> #include<bits/stdc++.h> using namespace std; typedef vector<double> vec; typedef vector<vec> mat; const double EPS=1E-8; //求解Ax=b,其中A是方针 //当方程组无解或者有无穷多解时,返回一个长度为0的数组

数据拟合求解方程参数

首先引入三件套和scipy import numpy as np import pandas as pd import matplotlib.pyplot as plt from scipy.optimize import curve_fit 拿到实验数据,通过pandas读取为DataFrame data = pd.read_csv("W-900K.csv") data.head() .dataframe tbody tr th:only-of-type { vertical-ali

求解方程A5+B5+C5+D5+E5=F5

方程A5+B5+C5+D5+E5=F5刚好有一个满足0<A≤B≤C≤D≤E≤F≤75的整数解.请编写一个求出该解的程序: 1 using System; 2 3 namespace ReverseTheExponentiation 4 { 5 class Program 6 { 7 static void Main(string[] args) 8 { 9 Program P = new Program(); 10 P.ReverseTheExponentiation(); 11 } 12 13

hdoj 2899 Strange fuction【二分求解方程】

Strange fuction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4599    Accepted Submission(s): 3304 Problem Description Now, here is a fuction:  F(x) = 6 * x^7+8*x^6+7*x^3+5*x^2-y*x (0 <= x <=

UESTC 288 青蛙的约会 (扩展欧几里得求解方程)

Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置.不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的.但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的.为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面. 我们把这