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 ( ...
随机推荐
- 第一章、VUE-挂载点-实例成员-数据-过滤器-文本指令-事件指令-属性指令-表单指令-01
目录 路飞项目 vue vue 导读 vue 的优势 渐进式框架 引入 vue 实例成员 - 挂载点 el js 对象(字典)补充 实例成员 - 数据 data 实例成员 - 过滤器 filters ...
- 03.Zabbix应用服务监控
一.Zabbix监控Nginx 1.1 实验环境 服务器系统 角色 IP CentOS 7.4 x86_64 Zabbix-Server 192.168.90.10 CentOS 7.4 x86_64 ...
- GOLANG利用断言调用结构体内特有的方法-
package main import( "fmt" _"sort" _"math/rand" ) //多态的特征是通过接口来实现的 //多 ...
- Hadoop_20_MapReduce程序的运行模式
1.MapReduce程序的运行模式 1. Windows中运行MapReduce程序 (1)mapreduce程序是被提交给LocalJobRunner在本地以单进程的形式运行 (2)而处理的数据及 ...
- nodejs建站+github page 建站问题总结
本文介绍 昨天吃晚饭的时候,在B站偶然看到一个关于搭建自己博客的视频,过程讲的很详细,于是就有了自己想尝试一下的冲动,所以,在晚上的时候,尝试了下,但是,过程并没有视频中说的那么顺利,看了网上很多帖子 ...
- linux基础_使用指令2
1.cat指令 功能:查看文件内容,是以只读的方式打开. 语法:cat [] 要查看的文件 选项: -n:显示行号 末尾加 | more:分页 使用细节: cat只能浏览文件,而不能修改文件,为了浏览 ...
- vs2015下载
VS2015 专业版下载链接http://download.microsoft.com/download/B/8/9/B898E46E-CBAE-4045-A8E2-2D33DD36F3C4/vs20 ...
- jQuery+masonry实现瀑布流
增加jQuery组件 <script src="//cdn.bootcss.com/jquery/2.2.1/jquery.min.js "></script&g ...
- gcc/g++以c++11编译
方法一: //在程序头加上预定义编译器命令 #pragma GCC diagnostic error "-std=c++11" //通过#pragma 指示 GCC编译器处理错误的 ...
- 【luoguP1196】 [NOI2002]银河英雄传说--边带权并查集 ,
题目描述 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶 ...