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的更多相关文章

  1. 【LeetCode】640. Solve the Equation 解题报告(Python)

    [LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...

  2. LC 640. Solve the Equation

    Solve a given equation and return the value of x in the form of string "x=#value". The equ ...

  3. 【leetcode】640. Solve the Equation

    题目如下: 解题思路:本题的思路就是解析字符串,然后是小学时候学的解方程的思想,以"2x+3x-6x+1=x+2",先把左右两边的x项和非x项进行合并,得到"-x+1=x ...

  4. 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 ...

  5. hdu 2199 Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. hdu 2199:Can you solve this equation?(二分搜索)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  7. 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 ...

  8. HDU 2199 Can you solve this equation? (二分 水题)

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. hdoj 2199 Can you solve this equation?【浮点型数据二分】

    Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

随机推荐

  1. Class.forName(),classloader.loadclass用法详解

    为什么要把ClassLoader.loadClass(String name)和Class.forName(String name)进行比较呢,因为他们都能在运行时对任意一个类,都能够知道该类的所有属 ...

  2. MySQL命令行导入导出sql文件

    linux下 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构(常用):mysqldump -u用户名 -p密码 数据库名 > 数据库名. ...

  3. js:JSON对象与JSON字符串转换

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式. 同时,JSON是 JavaScript 原生格式,这 ...

  4. sqlserver 带输出参数的存储过程

    --创建存储过程create procedure proc_stu@sname varchar(20),@pwd varchar(50),@flag bit outputasif exists(sel ...

  5. 《O2O实战:二维码全渠道营销》读书笔记思维导图(530KB)

  6. Zabbix监控 windows agent安装配置

    下载Windows的zabbix客户端 载地址:http://www.zabbix.com/download.php 选择windows版本的agent下载 从官方下载Zabbix Agent后,压缩 ...

  7. POJ 2985 名次树

    题意:1~n个猫,有合并操作,有询问操作,合并两只猫所在的集合,询问第K大的集合. 分析:合并操作用并查集,用size维护,询问操作用Treap.注意优化,不能刚开始就把所有size = 1放到名次树 ...

  8. AOE网络的关键路径问题

    关于AOE网络的基本概念可以参考<数据结构>或者search一下就能找到,这里不做赘述. 寻找AOE网络的关键路径目的是:发现该活动网络中能够缩短工程时长的活动,缩短这些活动的时长,就可以 ...

  9. 根据GB2312编码表求汉字字节

    java中有8种基本数据类型,byte,short,int,long,float,double,boolean byte用1个字节表示,占8比特,取值范围 负2的7次方至正2的7次方减1 二进制000 ...

  10. Node.js 笔记03

    一.buffer(缓存区) Node里面的buffer,是一个二进制数据容器,数据结构类似与数组,专门用于Node中数据的存放 ​ 1. buffer的基本使用 历史上的使用方式:const buf1 ...