1. 题目

求解一个给定的方程,将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"

来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/solve-the-equation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 写的不是很简洁,可优化
  • =号分成两边,把系数加总
class Solution {public:string solveEquation(string equation) {int i, lnum = 0, rnum = 0, lcoe = 0, rcoe = 0, n=0;char ch;bool positive = true;for(i = 0; equation[i] != '='; ++i){ch = equation[i];if(ch == 'x'){lcoe += n==0 ? ((i>0&&equation[i-1]=='0')? 0 : (positive ? 1 : -1)) : (positive ? n : -n);// 注意考虑 "0x=0" n=0;}else if(ch == '-'){lnum += (positive ? n : -n);positive = false;n=0;}else if(ch == '+'){lnum += (positive ? n : -n);positive = true;n=0;}elsen = 10*n+ch-'0';}if(equation[i-1] != 'x')lnum += (positive ? n : -n);positive = true;n = 0;for(i++; i < equation.size(); ++i){ch = equation[i];if(ch == 'x'){rcoe += n==0 ? ((equation[i-1]=='0')? 0 : (positive ? 1 : -1)) : (positive ? n : -n);n=0;}else if(ch == '-'){rnum += (positive ? n : -n);positive = false;n=0;}else if(ch == '+'){rnum += (positive ? n : -n);positive = true;n=0;}elsen = 10*n+ch-'0';}if(equation[i-1] != 'x')rnum += (positive ? n : -n);if(lcoe == rcoe && lnum == rnum)return "Infinite solutions";if(lcoe == rcoe && lnum != rnum)return "No solution";int ans = (lnum-rnum)/(rcoe-lcoe);return "x="+to_string(ans);}
};

0 ms 6.1 MB

  • 优化代码重复段
class Solution {public:string solveEquation(string equation) {int i, lnum = 0, rnum = 0, lcoe = 0, rcoe = 0, n=0;int pos = equation.find('=');vector<int> coeff = cal_coeff(equation.substr(0,pos));lcoe = coeff[0];lnum = coeff[1];coeff = cal_coeff(equation.substr(pos+1));rcoe = coeff[0];rnum = coeff[1];if(lcoe == rcoe && lnum == rnum)return "Infinite solutions";if(lcoe == rcoe && lnum != rnum)return "No solution";int ans = (lnum-rnum)/(rcoe-lcoe);return "x="+to_string(ans);}vector<int> cal_coeff(string equation){char ch;bool positive = true;int i, n = 0, coe = 0, num = 0;for(i = 0; i < equation.size(); ++i){ch = equation[i];if(ch == 'x'){coe += n==0 ? ((i>0&&equation[i-1]=='0')? 0 : (positive ? 1 : -1)) : (positive ? n : -n);n=0;}else if(ch == '-'){num += (positive ? n : -n);positive = false;n=0;}else if(ch == '+'){num += (positive ? n : -n);positive = true;n=0;}elsen = 10*n+ch-'0';}if(equation[i-1] != 'x')num += (positive ? n : -n);return {coe, num};}
};

LeetCode 640. 求解方程(字符串)相关推荐

  1. leetcode 640. Solve the Equation | 640. 求解方程(字符串处理)

    题目 https://leetcode.com/problems/solve-the-equation/ 题解 本题重点在字符串的处理. 先用 tokenize 函数,整理出每一项,用 X 表示,记录 ...

  2. LeetCode——1888. 使二进制字符串字符交替的最少反转次数(Minimum Number of Flips to Make the Binary ...)[中等]——分析及代码(Java)

    LeetCode--1888. 使二进制字符串字符交替的最少反转次数[Minimum Number of Flips to Make the Binary String Alternating][中等 ...

  3. 使用MATLAB求解方程求根——学习笔记

    使用MATLAB求解方程求根--学习笔记 碎碎念:终于参加完了某比赛,连续大约摸了两天的鱼,就在昨天由于自己的操作失误,亲手将电脑给烧了,这就是上天在暗示我是时候加油为接下来的两场比赛和一个考试努力啦 ...

  4. Matlab求解方程或函数的根,root,fzero,solve,fsolve的区别

    1.引言 Matlab中有很多求解方程和方程组的函数,这些函数的使用可能有很多人都模棱两可,这里做一个简单的介绍,给个大方向,学会这些函数的基本使用场景.想要学习每个函数的更多细节和案例,Matlab ...

  5. R语言应用uniroot函数求解方程的根(一元解):仿真数据(方程式可视化、并添加y=0的水平横线)、uniroot函数求解方程的根(并添加方程根对应的垂直竖线)

    R语言应用uniroot函数求解方程的根(一元解):仿真数据(方程式可视化.并添加y=0的水平横线).uniroot函数求解方程的根(并添加方程根对应的垂直竖线) 目录

  6. 二分法求解方程的根java_C语言二分法求解方程根的两种方法

    本文实例为大家分享了C语言二分法求解方程根的具体代码,供大家参考,具体内容如下 对于二分法求根,其实和弦截法思想很像,甚至更简单. 原理:先看如下的图 A,B两个点为跟的一个边界,通过一直缩小跟的边界 ...

  7. scipy.optimize.fsolve:用Python求解方程的解

    例1: 求解方程组的一个解: x0*cos(x1) = 4, x1*x0 - x1 = 5. 需要注意两点: 1.定义方程组,方程组要写出f(x)=0的形式(=0不需要写出来),所以原方程右边4和5都 ...

  8. leetcode初级算法6.字符串转整数(atoi)

    leetcode初级算法6.字符串转整数(atoi) 仅为个人刷题记录,不提供解题思路 题解与收获 我的解法: public int myAtoi(String s) {//避免魔法值先设spaceS ...

  9. c语言实现二分法_C语言实现二分法求解方程在区间内的根

    C语言实现二分法求解方程在区间内的根. 设有非线性方程: 其中, 为 上连续函数且设 (不妨设方程在 内仅有一个实根),求上述方程实根的二分法过程,就是将含根区间[a,b]逐步分半,检查函数值符号的变 ...

最新文章

  1. SAP HUM 如何看哪些HU还在923包装区尚未上架?
  2. linux 进程隐藏常见方法
  3. 扫描项目里没有使用的图片mac工具,删除没有使用的图片以减小包的体积
  4. 解决redhat的未注册问题
  5. DevC++怎么更改背景颜色
  6. Anaconda+conda创建python沙箱环境
  7. “财务自由的15个阶段!说说你到哪个阶段了?”
  8. 数据结构——最小生成树之克鲁斯卡尔算法(Kruskal)
  9. matlab线性拉伸函数,采用线性变换对图像的每一个像素灰度作线性拉伸-Read.PPT
  10. Zookeeper的Quorum机制-谈谈怎样解决脑裂(split-brain)
  11. 16 Managing Undo
  12. JavaScript 执行覆盖测试分析
  13. Java算法——地图单点坐标判断是否存在于某个区域
  14. 渗透测试技术----工具使用(二)--Nessus工具下载及使用(安装在Kali上)
  15. LeetCode——1931. 用三种不同颜色为网格涂色(Painting a Grid With Three Different Colors)困难]——分析及代码(Java)
  16. 中国象棋AI在线弈游戏源码
  17. Zookeeper简介,架构,单机版搭建
  18. 深入解析网页防篡改技术
  19. mysql分组取最新
  20. 苹果商店App Store审核指南中文版(14-15-2016最新)-附:2015年App Store审核被拒Top10(官网)+被拒的23个理由(中英)

热门文章

  1. Exception in thread main java.lang.UnsupportedClassVersionError的另类解决办法
  2. 恩智浦智能车大赛2020_内蒙古科技大学第九届智能车大赛校内公开赛总决赛
  3. 如何访问另一台电脑的共享文件夹_如何远程控制另一台电脑
  4. CATia对计算机配置要求,【2人回答】求CATIA对电脑的详细配置要求-3D溜溜网
  5. 链路层基本问题 : 封装成帧、差错检测、流量控制
  6. c语言switch scanf语句,C语言中scanf函数与switch语句
  7. 虚拟机如何上网以及互ping问题
  8. dubbo的监控中心
  9. 泛型(模拟list)
  10. Java核心API需要掌握的程度