LC 640. Solve the Equation
Solve a given equation and return the value of x in the form of string "x=#value". The equation contains only '+', '-' operation, the variable x and its coefficient.
If there is no solution for the equation, return "No solution".
If there are infinite solutions for the equation, return "Infinite solutions".
If there is exactly one solution for the equation, we ensure that the value of x is an integer.
Example 1:
Input: "x+5-3+x=6+x-2"
Output: "x=2"
Example 2:
Input: "x=x"
Output: "Infinite solutions"
Example 3:
Input: "2x=x"
Output: "x=0"
Example 4:
Input: "2x+3x-6x=x+2"
Output: "x=-1"
Example 5:
Input: "x=x+2"
Output: "No solution"
Runtime: 4 ms, faster than 99.39% of Java online submissions for Solve the Equation.
class Solution {
public static int[] mergenumber(String s){
int cnt = 0, tmpcnt = 0, xval = 0, constval = 0, prev = 1;
if(s.charAt(tmpcnt) == '+' || s.charAt(tmpcnt) == '-'){
cnt = ++tmpcnt;
prev = s.charAt(cnt-1) == '+' ? 1 : -1;
}
while(cnt < s.length()){
while(tmpcnt < s.length() && s.charAt(tmpcnt) != '+' && s.charAt(tmpcnt) != '-'){
tmpcnt++;
}
if(s.charAt(tmpcnt-1) == 'x'){
if(tmpcnt -1 > cnt) xval += prev * Integer.parseInt(s.substring(cnt, tmpcnt-1));
else xval += prev * 1;
}else constval += prev * Integer.parseInt(s.substring(cnt, tmpcnt));
if(tmpcnt == s.length()) break;
cnt = ++tmpcnt;
prev = s.charAt(tmpcnt-1) == '+' ? 1 : -1;
}
int[] ret = {xval, constval};
return ret;
}
public static String solveEquation(String equation) {
int idx = 0;
for(; idx<equation.length(); idx++){
if(equation.charAt(idx) == '=') break;
}
int[] left = mergenumber(equation.substring(0,idx));
// System.out.println(left[0]);
// System.out.println(left[1]);
int[] right = mergenumber(equation.substring(idx+1));
// System.out.println(right[0]);
// System.out.println(right[1]);
if(left[0] == right[0]){
if (left[1] == right[1]) return "Infinite solutions";
else return "No solution";
} else {
int ret = -1 * (right[1] - left[1]) / (right[0] - left[0]);
String rets = "x=" + String.valueOf(ret);
return rets;
}
}
}
LC 640. Solve the Equation的更多相关文章
- 【LeetCode】640. Solve the Equation 解题报告(Python)
[LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 640. Solve the Equation
class Solution { public: string solveEquation(string equation) { int idx = equation.find('='); , v1 ...
- 【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 ( ...
随机推荐
- CompletionService异步非阻塞获取并行任务执行结果
第1部分 问题引入 <Java并发编程实践>一书6.3.5节CompletionService:Executor和BlockingQueue,有这样一段话: "如果向Execut ...
- Dart的导包
Dart中的库主要有三种: 1.我们自定义的库 import 'lib/xxx.dart'; 2.系统内置库 import 'dart:math'; import 'dart:io'; import ...
- Linux特殊权限及ACL权限
一.SetUID与SGID 只能用于二进制程序,脚本不能设置 执行者需要有该二进制程序的x权限 执行具有SUID权限的二进制程序,那么执行者将具有该二进制程序所有者的权限. 举例来说,/etc/pas ...
- 【CodeForces】915 G. Coprime Arrays 莫比乌斯反演,前缀和,差分
Coprime Arrays CodeForces - 915G Let's call an array a of size n coprime iff gcd(a1, a2, ..., *a**n) ...
- Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...
- 关于网站子目录绑定二级域名的方法(php网站手机端)
最近帮客户做zencart网站手机模板用到了二级域名,通过判断手机访问来调用二级目录程序,http://afish.cnblogs.com/ 怎么说都比 http://www.cnblogs.com/ ...
- jquery.table2excel,将HTML的table标签数据导出成excel
<!DOCTYPE> <html> <head> <meta http-equiv="Content-Type" content=&quo ...
- Mysql中用exists代替in
exists对外表用loop逐条查询,每次查询都会查看exists的条件语句,当 exists里的条件语句能够返回记录行时(无论记录行是的多少,只要能返回),条件就为真,返回当前loop到的 ...
- [MySQL优化] -- 如何使用SQL Profiler 性能分析器
mysql 的 sql 性能分析器主要用途是显示 sql 执行的整个过程中各项资源的使用情况.分析器可以更好的展示出不良 SQL 的性能问题所在. 下面我们举例介绍一下 MySQL SQL Profi ...
- 牛客练习赛3 贝伦卡斯泰露——队列&&爆搜
题目 链接 题意:给出一个长度为 $n$ 的数列 $A_i$,问是否能将这个数列分解为两个长度为n/2的子序列,满足: 两个子序列不互相重叠(是值不能有共同元素,但位置可以交错). 两个子序列中的数要 ...