640. Solve the Equation
class Solution {
public:
string solveEquation(string equation) {
int idx = equation.find('='); int x1 = , v1 = , x2 = , v2 = , idx1 = , idx2 = ;
helper(equation.substr(, idx), idx1, x1, v1);
helper(equation.substr(idx+), idx2, x2, v2); int x = x1 - x2;
int v = v2 - v1;
if (x == && v == )
return "Infinite solutions";
else if (x == )
return "No solution";
else
return "x=" + to_string(v / x);
}
void helper(const string& s, int& idx, int &x, int &v) {
int n = s.length();
if (idx >= n) return; int flag = ;
if (s[idx] == '+') {
idx++;
}
if (s[idx] == '-') {
flag = -;
idx++;
}
if (idx >= n) return; if (isdigit(s[idx])) {
int t = ;
while (idx < n && isdigit(s[idx])) {
t = t * + s[idx] - '';
idx++;
}
if (idx >= n || s[idx] != 'x')
v += t * flag;
else {
idx++;
x += t * flag;
}
}
else {
x += * flag;
idx++;
} helper(s, idx, x, v);
}
};
640. Solve the Equation的更多相关文章
- 【LeetCode】640. Solve the Equation 解题报告(Python)
[LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- LC 640. Solve the Equation
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- 【leetcode】640. Solve the Equation
题目如下: 解题思路:本题的思路就是解析字符串,然后是小学时候学的解方程的思想,以"2x+3x-6x+1=x+2",先把左右两边的x项和非x项进行合并,得到"-x+1=x ...
- ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分
Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199 Can you solve this equation?(高精度二分)
http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS ...
- HDU 2199 Can you solve this equation? (二分 水题)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdoj 2199 Can you solve this equation?【浮点型数据二分】
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- 面向对象设计之------Is-A(继承关系)、Has-A(合成关系,组合关系)和Use-A(依赖关系)(转)
原文url:http://blog.csdn.net/loveyou128144/article/details/4749576 @Is-A,Has-A,Use-A则是用来描述类与类之间关系的.简单的 ...
- SQL Server ->> 在SQL Server中创建ASSEMBLY
首先要把数据库的TRUSTWORTHY属性改为ON ALTER DATABASE [MYDB] SET TRUSTWORTHY ON GO 接下来直接创建ASSEMBLY应该就没问题了.但是往往有可能 ...
- 5.Zabbix 3.0案例
请查看我的有道云笔记: http://note.youdao.com/noteshare?id=15d986179cb65b45c5824e90995109ee&sub=D2E73572D97 ...
- Wi-Fi
AP就是一个无线的交换机,提供无线信号发射接收的功能 Wi-Fi是一种可以将个人电脑.手持设备(如PDA.手机)等终端以无线方式互相连接的技术 两个不一样的东西,无法比较的 你说的应该是无线路由器和无 ...
- 一点一点学写Makefile(2)-自动搜所当前目录下的所有源文件
上个博客我们使用的是笨方法添加源文件,本次我要实现的是遍历文件夹来获得所有的cpp文件 //makefile CROSS = CC = $(CROSS)gcc CXX = $(CROSS)g++ DE ...
- chpasswd
功能说明:从标准输入中读取一定格式的用户名.密码来批量更新用户的密码,其格式为 “用户名:密码”. 参数选项:-e 默认格式是明文密码,使用-e参数则需要加密的密码.
- 2.Swift教程翻译系列——Swift概览
英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 依照传统学习程序语言都是从hello,world開始,在Swfit里面仅仅须要一 ...
- HDU 2048 错排
错排递推公式: d(n) = (n-1)*(d[n-1]+d[n-2]): 证明:将第n个元素放到第k处,第k处的元素如果放到第n处,就是d(n-2),否则,先假设放到第n处,然后错排,就是d(n-1 ...
- 深度优先搜索(dfs),城堡问题
题目链接:http://poj.org/problem?id=1164 1.深搜,每个点都访问一次,没有标记的话,就做深搜,同时标记. #include <iostream> #inclu ...
- 【[USACO16OPEN]262144】
发现这个数列的范围特别大但是值域的范围特别小 于是可以大胆猜测这道题值域肯定需要开到状态里去 又发现\(262144=2^{18}\)这个暗示非常明显啊,暗示这道题跟二进制有关系 其实也没什么关系 设 ...