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 ( ...
随机推荐
- Redis缓存策略设计及常见问题
Redis缓存设计及常见问题 缓存能够有效地加速应用的读写速度,同时也可以降低后端负载,对日常应用的开发至关重要.下面会介绍缓存使用技巧和设计方案,包含如下内容:缓存的收益和成本分析.缓存更新策略的选 ...
- Image Processing and Computer Vision_Review:HPatches A benchmark and evaluation of handcrafted and learned local descriptors——2017.04
翻译 HPatches:手工和学习本地描述符的基准和评估——http://tongtianta.site/paper/8979 摘要:在本文中,我们提出了一个评估本地图像描述符的新基准.我们证明现有数 ...
- thymeleaf的内联th:inline(在javascript访问model中的数据)
thymeleaf模板引擎为前端数据的获取提供了较大的便利,在html标签内可通过th标签加${}表达式访问model里的对象数据.但如果不想通过th标签而是简单地访问model对象数据,或是想在ja ...
- mysql—数据库优化——如何选择合适的索引
索引的分类: 普通索引: 唯一索引: 主键索引:特殊的唯一索引,唯一且不能有null值: 全文索引:全文索引用来对表中的文本域(char, varchar, text)进行索引 全文索引针对myisa ...
- okhttp初识拦截器
拦截器流程: 简单回顾同步 / 异步: 同步请求就是执行请求的操作是阻塞式,直到HTTP响应返回. 异步请求就类似于非阻塞式的请求,它的执行结果一般都是通过接口回调的方式告知调用者. okHttp拦截 ...
- Python项目列表
70个Python项目列表: 1.[Python 图片转字符画]2.[200行Python代码实现2048]3.[Python3 实现火车票查询工具]4.[高德API+Python解决租房问题 ]5. ...
- linux基础_用户和组的三个文件
1./etc/passwd文件 用户(user)的配置文件,记录用户的各种信息 每行的含义:用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录shell 2./etc/shadow文件 口令 ...
- unreal 抓mobile 管线
把renderdoc挂到生成的exe上 用命令行 “路径\xx.uproject” scenename -game -FeatureLevelES31 -windowed -resx=1920 -re ...
- 11 git第二部分(未完成)
https://www.cnblogs.com/shangchunhong/p/9444335.html
- Java xml和map,list格式的转换-摘抄
import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.HashMap; import j ...