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

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

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

  2. 640. Solve the Equation

    class Solution { public: string solveEquation(string equation) { int idx = equation.find('='); , v1 ...

  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. python解决导入自定义库失败: ModuleNotFoundError: No module named 'MyLib'

    python安装目录:...\python_3_6_1_64bit 新建文件:chenyeubai.pth,写入库所在的绝对路径E:\workSpace\my_code\learn\myLib 安装路 ...

  2. C# 修改Config文件,增删查改

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/BYH371256/article/det ...

  3. easyUi的组合表格

    公司之前的项目是用easyui写的里面还混搭着php...把分支下来,有点蒙.晚上回来恶补一下吧,今天渲染这个表格,我开始自己写假数据,然后用ajax操作再使用 obj.datagrid('loadD ...

  4. jumpserver跳板机docker安装小小趟坑

    最近日常运维的时候发现每次登陆服务器都要打开终端目录连接对应的服务器,闲暇的时候还好,运维任务很重的时候才发现这样的玩法很傻,浪费时间且一点儿都跟不上潮流,然后打开githup开始搞起来.docker ...

  5. 聚类算法之MeanShift

    机器学习的研究方向主要分为三大类:聚类,分类与回归. MeanShift作为聚类方法之一,在视觉领域有着广泛的应用,尤其是作为深度学习回归后的后处理模块而存在着. 接下来,我们先介绍下基本功能流程,然 ...

  6. kubernetes之download api

    download api作用: 可以通过环境变量或Volume挂载将pod信息注入到容器内部 apiVersion: apps/v1 kind: Deployment metadata: name: ...

  7. Hadoop_29_MapReduce_计数器应用

    在实际生产代码中,常常需要将数据处理过程中遇到的不合规数据行进行全局计数,类似这种需求可以借助mapreduce框架中 提供的全局计数器来实现 示例代码如下: public class MultiOu ...

  8. Kinect for windows的脸部识别

    需要引入的dll: 需要将下面两个dll复制到当前路径 Kinect for windows提供了脸部识别的功能,可以识出人脸.主要是通过FaceTrackFrame类的GetTriangles()来 ...

  9. JDK的收费问题

    关于JDK是否收费,我也有过疑问,查了一些资料,就在这里做一个简要的说明. 首先要明白JDK的发布模型.两年多以前,2017年9月21日,伴随着JDK9的发布,JDK就开启了新的发布模式(如下图所示) ...

  10. Bootstrap-轮播图-No.2

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...