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 ( ...
随机推荐
- java面试题之----数据库事务的四大特性及隔离级别
本篇讲诉数据库中事务的四大特性(ACID),并且将会详细地说明事务的隔离级别. 如果一个数据库声称支持事务的操作,那么该数据库必须要具备以下四个特性: ⑴ 原子性(Atomicity) 原子性是指事务 ...
- #单元测试#以karma+mocha+chai 为测试框架的Vue webpack项目(二)
学习对vue组件进行单元测试,先参照官网编写组件和测试脚本. 1.简单的组件 组件无依赖,无props 对于无需导入任何依赖,也没有props的,直接编写测试案例即可. /src/testSrc/si ...
- mysqlimport导入简单测试
1 创建一个文本文档:[mysql@xxxycrdb]$ more /tmp/ldcmd1.txt 1,abc,abc@qq.com1,abc,abc@qq.com1,abc,abc@qq.co ...
- May 14th 2017 Week 20th Sunday
A smooth sea never made a skillful mariner. 平静的海洋练不出熟练的水手. A smooth sea never made a skillful marine ...
- Codeforces 7C 扩展欧几里得
扩展欧几里得是计算 ax + by = gcd(a,b) 的 x,y的整数解. 现在是ax + by + c = 0; 只要 -c 是 gcd(a,b) 的整数倍时有整数解,整数解是 x = x*(- ...
- html默认样式重置
几个著名的重置css goal https://meyerweb.com/eric/tools/css/reset/ 雅虎 https://yuilibrary.com/yui/docs/cssr ...
- Docker 常用指令
1.检查内核版本,必须是3.10及以上uname ‐r2.安装dockeryum install docker3.输入y确认安装4.启动docker[root@localhost ~]# system ...
- 解决Sublime Text乱码问题
第一步:安装Package Control 大家如果是在官网下载的Sublime Text 3,那么首先需要安装一个 Package Control包,这是一个用来安装其他插件的包,不管装什么插件,首 ...
- 【luogu P1774 最接近神的人_NOI导刊2010提高(02)】 题解
题目链接:https://www.luogu.org/problemnew/show/P1774 归并排序求逆序对. #include <cstdio> #define livelove ...
- SqlSugar操作Oracle的dblink时候@符号问题
用的这个版本,作者忘记删除Oracle中的代码了....下个版本作者应该就会更新了,到时候就不会存在这个问题,这里记录一下. 引用nuget出现的问题: 使用dblink的时候,查询的时候需要带@符号 ...