Java实现 LeetCode 640 求解方程(计算器的加减法计算)
640. 求解方程
求解一个给定的方程,将x以字符串"x=#value"的形式返回。该方程仅包含’+’,’ - '操作,变量 x 和其对应系数。
如果方程没有解,请返回“No solution”。
如果方程有无限解,则返回“Infinite solutions”。
如果方程中只有一个解,要保证返回值 x 是一个整数。
示例 1:
输入: "x+5-3+x=6+x-2"
输出: "x=2"
示例 2:
输入: "x=x"
输出: "Infinite solutions"
示例 3:
输入: "2x=x"
输出: "x=0"
示例 4:
输入: "2x+3x-6x=x+2"
输出: "x=-1"
示例 5:
输入: "x=x+2"
输出: "No solution"
PS:
标记一下正负,再用一个指针指到数字的第一位
class Solution {
public String solveEquation(String equation) {
int coff = 0, sum = 0, index = 0, sign = 1;
int n = equation.length();
for(int i=0;i<n;i++){
char c = equation.charAt(i);
if(c == '='){
if(index < i){
sum += Integer.valueOf(equation.substring(index, i)) * sign;
}
sign = -1;
index = i + 1;
}else if(c == 'x'){
if(index == i || equation.charAt(i - 1) == '+'){
coff += sign;
}else if(equation.charAt(i - 1) == '-'){
coff -= sign;
}else{
coff += Integer.valueOf(equation.substring(index, i)) * sign;
}
index = i+1;
}else if(c == '-' || c == '+'){
if(index < i){
sum += Integer.valueOf(equation.substring(index, i)) * sign;
}
index = i;
}
}
if(index < n){
sum += Integer.valueOf(equation.substring(index)) * sign;
}
if(sum == 0 && coff == 0) return "Infinite solutions";
if(coff == 0) return "No solution";
return "x=" + String.valueOf(-sum / coff);
}
}
Java实现 LeetCode 640 求解方程(计算器的加减法计算)的更多相关文章
- Leetcode 640.求解方程
求解方程 求解一个给定的方程,将x以字符串"x=#value"的形式返回.该方程仅包含'+',' - '操作,变量 x 和其对应系数. 如果方程没有解,请返回"No so ...
- Java实现 LeetCode 762 二进制表示中质数个计算置位(位运算+JDK的方法)
762. 二进制表示中质数个计算置位 给定两个整数 L 和 R ,找到闭区间 [L, R] 范围内,计算置位位数为质数的整数个数. (注意,计算置位代表二进制表示中1的个数.例如 21 的二进制表示 ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
- Java for LeetCode 200 Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- OpenWrt(LEDE)2020.4.12编译 UnPnP+NAS+多拨+网盘+DNS优化+帕斯沃 无缝集成
固件说明 基于Lede OpenWrt R2020.4.8版本(源码截止2020.4.12)Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及软件 ...
- [hdu5319]二进制表示,简单模拟
题意:给一个矩形,矩形里面画了4种符号,'.'表示没画线,'R'表示画了红线,'B'表示画了蓝线,'G'表示红线和蓝线同时画了,并且矩形主对角线上只能画红线,副对角线上只能画蓝线,问最少画多少条线才能 ...
- jstree 反选,测试400条数据左右有点卡
$("#reversecheckallmachines").on("change", function () { var checkedNodes = []; ...
- select 标签的数据绑定
修改数据的页面 进入页面绑定select的值 会value绑定但是没有显示相应的option <script> $("#id option[value=${item.decora ...
- Android fragment 使用replace并保存状态
Fragment的地位在开发中可是举足轻重的,掌握它的的生命周期以及使用特性是非常重要的,例如在开发中常使用的模板: 点击菜单,中心内容跟随菜单变化,但是在菜单间切换时,需要保存之前输入的信息或其他状 ...
- c#与js客户端之间相互传值
RegisterStartupScript(key, script) RegisterClientScriptBlock(key, script) 第一个参数 key 是插入的客户端脚本的唯一标识符. ...
- 修改jupyter默认保存文件的目录
1.打开 Anaconda Prompt输入命令 jupyter notebook --generate-config 2.可以看到生成了目录及jupyter notebook的配置文件,打开该文件. ...
- RabbitMQ及延时队列
一.简介 我用过RabbirMQ的发布订阅模式,以及一对一的延迟队列. 1.RabbitMQ的有消息确认机制,消费一条则队列中少一条,也有对应的消费到消息及认为是消费成功这样的模式,一般使用前者. 发 ...
- elasticsearch 小总结
elasticsearch 小总结 0. 起因 距离初次写关于es的文章 https://blog.csdn.net/aca_jingru/article/details/44488703 已经过去4 ...
- 关于键盘事件-查询:有多个input框,任意一个支持enter键查询
应用场景:同一个界面有多个input框支持任意一个Enter查询. 实现:在input框中添加onkeypress="函数名()". 函数里面编写对应键盘code值,在里面直接调用 ...