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 ( ...
随机推荐
- vmware克隆的linux机器网络不通
当我使用vmware的完全克隆功能克隆出两台虚拟机之后,登录发现网络不通,仔细检查发现几个问题,由于克隆之后默认的eth0网卡在系统中会变成eth1,导致之前的eth0网卡配置信息无法加载,网络不通, ...
- 常用的shell语句 【awk】去重,排列
目的:从日志access.log中,筛选出IP来,并统计每个IP出现的次数,然后显示出来. 因为:awk = 扒IP shot = 排序 uniq = 去重 所以:awk '{print $1} ...
- Orchard Core 文档翻译 (七)Contents
CMS Modules »Contents Contents (OrchardCore.Contents) 此模块提供内容管理服务. Liquid 您可以使用“content ”属性从liquid 视 ...
- 《浪潮之巅》(第2版):精彩的IT商战史
2011年看过第一版.以为看新版会跳过大部分看过的内容,结果发现还是从头到尾看了一遍,2011年看过的内容已经记不太确切了:) 另外IT的历史太精彩了,许多故事都知道,再看还是挺有意思.当然作者的文字 ...
- May 13th 2017 Week 19th Saturday
Mountains look beautiful from a distance. 远处看山山更美. This gnomic seems to circulate very long, its mor ...
- March 23 2017 Week 12 Thursday
A bird is known by its note, and a man by his talk. 闻其声而知鸟,听其言而知人. One of the lessons I learned rece ...
- VMware,win7与linux centos6.4文件互传,linux下挂载windows共享文件夹,vmware tools安装方法
本方法是以win7,VMware9.0.1 ,centos6.4为基础实验的. 对于linux的初级使用阶段,都会Windows中使用linux虚拟机VMWare或者其它的.在Windows与linu ...
- BZOJ4066:简单题(K-D Tree)
Description 你有一个N*N的棋盘,每个格子内有一个整数,初始时的时候全部为0,现在需要维护两种操作: 命令 参数限制 内容 1 x y A 1<=x,y<=N,A是正整数 ...
- 最短路算法 —— Dijkstra算法
用途: 解决单源最短路径问题(已固定一个起点,求它到其他所有点的最短路问题) 算法核心(广搜): (1)确定的与起点相邻的点的最短距离,再根据已确定最短距离的点更新其他与之相邻的点的最短距离. (2) ...
- jstl有什么优势(简单介绍)
简单介绍 JSTL是一个不断完善的开放源代码的JSP标签库,是由apache的jakarta小组来维护的.JSTL只能运行在支持JSP1.2和Servlet2.3规范的容器上,如tomcat 4.x. ...