<题目描述>

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

  • 如果方程没有解,请返回“No solution”。
  • 如果方程有无限解,则返回“Infinite solutions”。
  • 如果方程中只有一个解,要保证返回值 x 是一个整数。

<原题链接>

https://leetcode-cn.com/problems/solve-the-equation

<理明思路>

※解释:

  • string solveEquation(string):题目给出初始函数。
  • string anlz_equal(string,int):输入原始字符串,参数0取方程左边的式子并返回;参数1取方程右边的式子并返回。
  • void anlz_numbr(string,int):将字符串(0=左边 1=右边)的字符串转换为数字并入栈。(其中默认将所有系数放在等式左边,所有常数放在等式右边)
  • stack1< int>:存放系数的栈。
  • stack2< int>:存放常数的栈。
  • 最终将方程化为ax=b型(若有解),即可解出。

※大致思路:

<样例代码>

ps:本题只是按照自己的思路来编辑代码,执行用时32ms,相比之下并不是多么出彩的算法。。。

#include<stack>
#include<iostream>
#include<sstream>
#include<cstring>
using namespace std;
class Solution {
public:void function(string str,int n);           //将字符串变成数字、入栈。string anlz_equal(string equation,int n); //分割字串,参数0:等号左,参数1:等号右。string solveEquation(string equation) {int a=0,b=0;     //a=系数之和;   b=常数之和;double c;string ans = "x=";stringstream num;function(anlz_equal(equation,0),0);    //方程左边常数&系数入栈。function(anlz_equal(equation,1),1);    //方程右边常数&系数入栈。while( !xishu.empty() ){a += xishu.top();       //系数之和xishu.pop();}while( !changshu.empty() ){b += changshu.top();  //常数之和changshu.pop();}if(a==0&&b!=0)return "No solution";if(a==0&&b==0)return "Infinite solutions";c = (double)b/(double)a;num << c;            //将整数转换为字符串。if(num.str() == "-0")num.str="0"ans += num.str();return ans;}
private:stack<int> xishu;stack<int> changshu;
};
void Solution::function(string str,int n)
{int a=0,j=1;switch(n){case 0:      //等式左边:系数为本身,常数为相反数。{for(int i=str.length()-1;i>=0;i--){if(str[i] == 'x' && i==0){xishu.push(1);break;}if(str[i] == 'x'){i--;if(str[i]=='+') {xishu.push(1);continue;}else if(str[i]=='-') {xishu.push(-1);continue;}while(str[i]!='-'){if(str[i]=='+') break;a += (str[i]-48)*j;j *= 10;i--;if(i<0) break;}j=1;if(str[i]=='-')a = (-1)*a; //取负数。xishu.push(a);a=0;continue;}if(str[i] != 'x')      //等号左边常数部分{while(str[i]!='-'){if(str[i]=='+') break;a += (str[i]-48)*j;j *= 10;i--;if(i<0) break;}j=1;if(str[i]=='-')a = (-1)*a; //取负数。changshu.push(-a);a=0;continue;}}}//end case 0break;case 1:      //等式右边:常数为本身,系数为相反数。{for(int i=str.length()-1;i>=0;i--){if(str[i] == 'x' && i==0){xishu.push(-1);break;}if(str[i] == 'x'){i--;if(str[i]=='+') {xishu.push(-1);continue;}else if(str[i]=='-') {xishu.push(1);continue;}while(str[i]!='-'){if(str[i]=='+') break;a += (str[i]-48)*j;j *= 10;i--;if(i<0) break;}j=1;if(str[i]=='-')a = (-1)*a; //取负数。xishu.push(-a);a=0;continue;}if(str[i] != 'x')       //等号左边常数部分{while(str[i]!='-'){if(str[i]=='+') break;a += (str[i]-48)*j;j *= 10;i--;if(i<0) break;}j=1;if(str[i]=='-')a = (-1)*a; //取负数。changshu.push(a);a=0;continue;}}}//end case 1break;default:cout<<"error"<<endl;exit(0);}//end switch
}
string Solution::anlz_equal(string equation,int n)
{string str;switch(n){case 0:for(int i=0;equation[i]!='=';i++)str.push_back(equation[i]);return str;case 1:for(int i=equation.find("=")+1;i<equation.length();i++)str.push_back(equation[i]);return str;default:cout<<"error"<<endl;exit(0);}
}

试解leetcode算法题--求解方程相关推荐

  1. LeetCode算法题-Minimum Depth of Binary Tree(Java实现)

    这是悦乐书的第168次更新,第170篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111).给定二叉树,找到它的最小深度.最小深度是沿从根节点到最近的 ...

  2. LeetCode算法题整理(200题左右)

    目录 前言 一.树(17) 1.1.后序遍历 1.2.层次遍历 1.3.中序 1.4.前序 二.回溯(20) 2.1.普通回溯 2.2.线性回溯:组合.排列.子集.分割 2.3.矩阵回溯 三.二分查找 ...

  3. LeetCode算法题-Nth Digit(Java实现)

    这是悦乐书的第215次更新,第228篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第83题(顺位题号是400).找到无限整数序列的第n个数字1,2,3,4,5,6,7,8 ...

  4. LeetCode算法题-Reverse Linked List(Java实现)

    这是悦乐书的第192次更新,第195篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第51题(顺位题号是206).反转单链表.例如: 输入:1-> 2-> 3- ...

  5. LeetCode算法题-Convert a Number to Hexadecimal(Java实现)

    这是悦乐书的第219次更新,第231篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第86题(顺位题号是405).给定一个整数,写一个算法将其转换为十六进制.对于负整数,使 ...

  6. leetcode算法题--零钱兑换

    原题链接:https://leetcode-cn.com/problems/coin-change/ 相关题目:leetcode算法题–完全平方数★ 动态规划 dp[i] i从0到amount,dp[ ...

  7. leetcode算法题-- 买卖股票的最佳时机

    原题链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 这类股票题目请见leetcode算法题–最佳买卖股票时机含 ...

  8. leetcode算法题--买卖股票的最佳时机 II

    原题链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 这类股票题目请见leetcode算法题–最佳买卖股票 ...

  9. leetcode算法题--买卖股票的最佳时机含手续费

    原题链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 这类股票题目请见 ...

最新文章

  1. 行转列 oracle nvl,oracle 行转列 decode
  2. Servlet中如何获取param-name对应的值?
  3. yuzu模拟器linux,Yuzu Early Acces
  4. R3 data related to category and hierarchy mapping logic in CRM
  5. [html] html的元素有哪些(包含H5)?
  6. 科立捷7代写频软件_天大厦大“两硕士论文雷同”通报,代写买卖论文
  7. concat合并的数组会有顺序么_javascript concat 数组与数组或数组与字符串的合并
  8. pycharm Debug调试
  9. 35岁的测试是测试的天花板吗?
  10. 频频被关注的 AI,怎样才能用着舒心?
  11. 问题六十二:怎么求一元十次方程在区间内的所有不相等的实根(2)——修正“区间端点零值”问题
  12. bootstrap 固定最底部_防腐木立柱怎么固定
  13. 缓存块着色算法和优化的缓存块着色算法
  14. Vue抽离公共方法并全局注册使用
  15. 了解 InfoPath 2007十大优势
  16. 教你10分钟电脑配置挑选装机速成攻略
  17. 如何搭建自己CDN服务器
  18. CSS设置html网页背景图片 CSS设置网页背景颜色
  19. 输入某辆小轿车三次的 耗油量(升)和行驶里程(公里),计算平均油耗(升/百公里)。
  20. python调用有道翻译_如何用python“优雅的”调用有道翻译?

热门文章

  1. java计算机毕业设计东理咨询交流论坛源码+系统+lw文档+mysql数据库+部署
  2. win10计算机的时间格式,win10系统更改excel时间和日期的格式的图文步骤
  3. C语言入门阶段08:C语言指针
  4. 数据结构--一元多项式
  5. 创建自己的手机条形码Thingy
  6. 使用Matlab对矩阵元素进行大小排序(开源)
  7. 李成名:科学就是较真 数字城市/智慧城市就是跑马圈地
  8. CentOS7修改MySQL密码
  9. python网格搜索优化参数_python - 用于管道的网格搜索参数网格的说明 - SO中文参考 - www.soinside.com...
  10. matlab拟合斜椭圆,椭圆拟合及拟合度评价