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. shell数组处理

    linux shell在编程方面比windows 批处理强大太多,无论是在循环.运算.已经数据类型方面都是不能比较的. 下面是个人在使用时候,对它在数组方面一些操作进行的总结.   1.数组定义   ...

  2. win10下 switchhost权限修改问题

    switchhost提示没有切换权限:C:\WINDOWS\system32\drivers\etc\host 文件无法修改   1.找到host文件 C:\Windows\System32\driv ...

  3. Django:forms局部函数、cookie、sesion、auth模块

    一.forms组件 二.cookie和session组件 三.auth组件 一.forms组件 1.校验字段功能 针对一个实例:注册用户讲解 模型:models class UserInfo(mode ...

  4. 踏步-java工具类

    /** * @Title:removeDuplicate * @author:踏步 * @date:2019年5月23日 下午2:31:40 * @Description:TODO 去除list的重复 ...

  5. C# 对象和类型(2) 持续更新

    类 class PhoneClass  { public const string DayOfSendingBill = "Monday"; public int Customer ...

  6. SpringBoot集成Druid实现监控

    application.properties文件完整信息 #连接数据库 spring.datasource.driver-class-name=org.mariadb.jdbc.Driver spri ...

  7. AtCoder Beginner Contest 137 D题【贪心】

    [题意]一共有N个任务和M天,一个人一天只能做一个任务,做完任务之后可以在这一天之后的(Ai-1)天拿到Bi的工资,问M天内最多可以拿到多少工资. 链接:https://atcoder.jp/cont ...

  8. [Vue] : Vue概述

    什么是Vue.js Vue.js 是目前最火的一个前端框架,React是最流行的一个前端框架. Vue.js 是前端的主流框架之一,和Angular.js.React.js 一起,并成为前端三大主流框 ...

  9. javascript数据结构之顺序表

    关于线性表的概念这里就不赘述了,可以自行百度和查阅资料,线性表按照存储(物理)结构分为顺序存储和链式存储,每种存储方式的不同决定了它的实现代码是不同的: 顺序存储的特点就是在内存中选一块连续的地址空间 ...

  10. sudo 问题:sorry, you must have a tty to run sudo

    写定时任务时候遇到 如下问题 To start backup... -- :: sudo: sorry, you must have a tty to run sudo The backup to c ...