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 ( ...
随机推荐
- Tomcat中部署web应用的三种方式
Tomcat中部署web应用的三种方式(静态部署) 第一种,针对war或解压后的war,最为常用的是直接操作webapp目录,将完整的war包或者web应用直接放到webapp目录下.使用 ...
- (EXPDP) Fails With Errors ORA-39079 ORA-25306 On One Node In RAC Environment
分类: Oracle DataPump export on one certain RAC instance fails with errors: ORA-39006: internal errorO ...
- 西门子 1500 1200 PLC,位访问, 字节访问
访问byte,word中的位 mybyte.%X1 myword.%X1 访问word/dword中的byte myword.%B0 myword.%B1 myDword.%B0 文中mybyte, ...
- Visual Studio 2012 未找到与约束 ContractName问题,及printf unsafe问题
1.用VS 2012 创建c++项目失败,提示未找到与约束 ContractName .............的错误 解决办法:下载VS 2012 补丁,很小的一个补丁,不过很管用 地址:http: ...
- 删除所有正在运行和退出的docker实例
docker ps -a能显示所有docker实例的状态,包含已经退出了的: 加上-q参数,只显示container id 使用这个命令,把docker ps -aq产生的输入作为输入传入到docke ...
- [原] Android自动打包之命令行打包
Android自动打包流程详细图: 总结为以下几个步骤: 1. 生成R文件 2. Java代码编译成class文件 3. class文件生成dex文件 4. 打包资源 5. 生成apk 6. 创建密匙 ...
- vuejs 开发中踩到的坑
用 v-for 循环式 每个item的值相等的情况下,会影响v-model的双向绑定: Modal 组件开发,主要用slot 标签来实现 <template> <transitio ...
- 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)
http://blog.csdn.net/hjf19790118/article/details/6919265 1.什么是面向对象编程(Object-Oriented Programming)? 面 ...
- BZOJ3874:[AHOI2014&JSOI2014]宅男计划(爬山法)
Description [故事背景] 自从迷上了拼图,JYY就变成了个彻底的宅男.为了解决温饱问题,JYY 不得不依靠叫外卖来维持生计. [问题描述] 外卖店一共有N种食物,分别有1到N编号.第i种 ...
- MVC学习六:Razor布局视图之【/Views/Shared/_Layout.cshtml】
_Layout代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...