leetcode 640. Solve the Equation 等式计算 + C++stringstream的一个很好地示范
2017-12-21 11:11
591 查看
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”
本题题意很简单,就是直接分析即可,注意使用C++的stringstream来处理,就是有点烦,别的都很简单
建议和leetcode 609. Find Duplicate File in System C++stringstream的一个很好地示范 一起学习
代码如下:
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”
本题题意很简单,就是直接分析即可,注意使用C++的stringstream来处理,就是有点烦,别的都很简单
建议和leetcode 609. Find Duplicate File in System C++stringstream的一个很好地示范 一起学习
代码如下:
#include <iostream> #include <vector> #include <map> #include <unordered_map> #include <set> #include <unordered_set> #include <queue> #include <stack> #include <string> #include <climits> #include <algorithm> #include <sstream> #include <functional> #include <bitset> #include <numeric> #include <cmath> #include <regex> #include <iomanip> using namespace std; class Solution { public: string solveEquation(string equation) { int index = equation.find('='); vector<int> left = getRes(equation.substr(0, index)); vector<int> right = getRes(equation.substr(index + 1)); int x = left[0] - right[0]; int num = right[1] - left[1]; string res = ""; if (x == 0 && num == 0) res = "Infinite solutions"; else if (x == 0 && num != 0) res = "No solution"; else res = "x=" + to_string(num / x); return res; } vector<int> getRes(string eq) { vector<int> res; stringstream ss(eq); char c; int num = 0, x = 0, tmp; string a = ""; for (int i = 0; i <= eq.length(); i++) { if (i<eq.length()) ss >> c; if (c == '+' || c == '-' || i == eq.length()) { if (a.size() >= 1) { if (a.back() == 'x') { if (a == "x" || a == "+x") tmp = 1; else if (a == "-x") tmp = -1; else tmp = stoi(a.substr(0, a.length() - 1)); x += tmp; } else { tmp = stoi(a); num += tmp; } } if (c == '-') a = "-"; else a = ""; } else a += c; } res = { x,num }; return res; } };
相关文章推荐
- leetcode 592. Fraction Addition and Subtraction 分数加法+C++stringstream的一个很好的示例
- leetcode 434. Number of Segments in a String 按照空格分割字符串 + C++的stringstream的一个很好应用示范
- leetcode 71. Simplify Path C++的stringstream分割字符串的一个很好地例子
- 输入读入一个String 序列,计算元音字母的个数,然后再顺序输出--C++
- leetcode 636. Exclusive Time of Functions 函数的单独运行时间 + stringstream很好应用
- qLibc 对于C C++都是一个很好的框架,提供Tree Hash Stack String I/O File Time等功能
- leetcode之92. Reverse Linked List II(C++读错题版本,交换一个链表中指定的两个位置上的元素)
- LeetCode 557. Reverse Words in a String III(C++版)
- 一个很好的C++ API函数例子的网站
- 【转】 一个十分有用的标准C++写的split(string) 函数
- C/C++练习题 定义一个日期变量,进行年、月、日的输入,计算该日期是本年中的第几天。
- c++中怎么判断一个string类型的字符串变量是否为数字?
- C++ stringstream介绍,使用方法与例子
- C++中的 istringstream 的用法
- LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )
- C++ stringstream介绍,使用方法与例子
- 网络通信中,DELPHI的string类型数据转换为C++字符串发送的一个陷阱
- 实战c++中的string系列--std:vector<char> 和std:string相互转换(vector to stringstream)
- [初]一个C++关于string的小问题
- Hypertable 简介 (一个 C++ 的Bigtable开源实现) 【很好的入门的例子】