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 ...
随机推荐
- C:__attribute__ weak 的作用
关于 weak weak经常出现在各种c代码中,其作用是将当前文件的对应函数声明为弱函数符号,如果外部文件出现相同的函数名,最终编译出来的 文件会优先指向外部文件的函数符号: 通常需要使用__attr ...
- 设计模式GOF23大纲
创建型模式: 单例模式,工厂模式,抽象工厂模式 结构型模式: 适配器模式,桥接模式,装饰模式,组合模式,外观模式,享元模式,代理模式 行为型模式: 模板方法模式,命令模式,迭代器模式,观察者模式,中介 ...
- Spring MVC教程——检视阅读
Spring MVC教程--检视阅读 参考 Spring MVC教程--一点--蓝本 Spring MVC教程--c语言中午网--3.0版本太老了 Spring MVC教程--易百--4.0版本不是通 ...
- web2
0x01 <?php $miwen="a1zLbgQsCESEIqRLwuQAyMwLyq2L5VwBxqGA3RQAyumZ0tmMvSGM2ZwB4tws"; funct ...
- java经典问题 byte b=1、b=b+1、b+=1
直接问题: 首先 byte的范围 [-128,127] byte 类型可以自动转为int类型 int类型不能自动转为byte类型. 超过byte的范围,就会变成int类型了 byte b=1:正确, ...
- SD实现原理学习,以及SD失效的问题解决
SD失效的问题可能解决方案: 1.有可能是图片的url地址不对,有可能浏览器可以打开,但是这个地址浏览器是做了处理的,所以浏览器能打开. 2.如果图片地址是Http,那么就需要关闭ATS. ATS ( ...
- 这么简单的ES索引生命周期管理,不了解一下吗~
对于日志或指标(metric)类时序性强的ES索引,因为数据量大,并且写入和查询大多都是近期时间内的数据.我们可以采用hot-warm-cold架构将索引数据切分成hot/warm/cold的索引.h ...
- 静态MAC地址配置案例
目录导航: 1.静态MAC地址简介 2.组网需求 3.配置思路 4.配置步骤 5.配置文件 1.静态MAC地址简介 返回目录导航 >MAC地址表项是交换机通过报文的源MAC地址学习过程而自动生成 ...
- 使用Redis——拳打南山敬老院,脚踩北斗幼儿园
拳打南山敬老院,脚踩北斗幼儿园 Redis 你说你用过对吧,你们怎么用的? 面试官您好,因为传统的关系型数据库如Mysql已经不能适用所有的场景了,比如秒杀的库存扣减,APP首页的访问流量高峰等等,都 ...
- Template模式C++实现
#include <iostream> using namespace std; class AbstractClass { public: void TemplateMethod() { ...