java计算表达式的值加减法_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

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);

}

}

Leetcode 640&period;求解方程

求解方程 求解一个给定的方程,将x以字符串"x=#value"的形式返回.该方程仅包含'+',' - '操作,变量 x 和其对应系数. 如果方程没有解,请返回"No so ...

Java实现 LeetCode 762 二进制表示中质数个计算置位(位运算&plus;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 ...

随机推荐

蓝桥杯 十六进制转八进制(超大测试数据&comma;java实现)

问题描述 给定n个十六进制正整数,输出它们对应的八进制数.输入格式 输入的第一行为一个正整数n (1<=n<=10). 接下来n行,每行一个由0~9.大写字母A~F组成的字符串,表示要转换 ...

Ext&period;Net 学习随笔 002 默认按钮

在FormPanel中按回车按键,会触发默认按钮的click事件.设置方法为在FormPanel中设置DefaultButton属性,如果没有设置这个属性,默认为最后一个按钮. 1.缺省最后一个按钮为 ...

js获取iframe里的body内容

做个页面 需要加入a.html 使用的js动态添加iframe 直接JQ添加的 代码 $(".banner-box").after(“

Flex4&sol;Flash开发在线音乐播放器 &comma; 含演示地址

要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...

css学习之color&colon; window和color&colon; currentColor

一.易被忽略的属性 color: currentColor color: window   看完之后感觉眼前一亮,有的我之前根本没有用过,甚至都不知道有color: currentColor这么个东西 ...

DHCP源码分析--主流程

DHCP 服务器,客户端代码都采用了统一的事件轮询(event loop),包含了任务处理消息,定时器消息,socke收发消息等等. static struct { isc_appmethods_t ...

Pyinstaller安装以及参数使用

一.安装 pip install pyinstaller (注:win7系统安装有pywin32依赖包,先按pywin32在进行以上的步骤) 二.参数说明(这里要注意大小写) -F   表示生成单个可 ...

GEF最简单的入门-helloword(1)

最近做插件项目.主要负责GEF这块. 好吧.资料真少的可以.特别是入门.都是一大堆一大堆的.网上最火的八进制的文章但对于我这种菜鸟级别看了还是一头雾水.各种资料折腾了半天.终于折腾出一个真正的入门例子 ...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值