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 ...
随机推荐
- 心路历程-安装Docker
心路历程-安装Docker 本机环境 Windows10 激活HyperV功能 新建CentOS虚拟机 centos docker安装 由于是新的虚拟机,所以没有docker旧版本的问题,不需要卸载旧 ...
- vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
框架介绍 这是一个基于vue.element-ui.iview..netcore3.1 可支持前端.后台动态扩展业务代码快速开发框架. 框架内置定制开发的代码生成器,生成的代码不需要复制也不需要更改, ...
- 【STM32系列汇总】小白博主的STM32实战快速进阶之路(持续更新)
我把之前在学习和工作中使用STM32进行嵌入式开发的经验和教程等相关整理到这里,方便查阅学习,如果能帮助到您,请帮忙点个赞: 本文的宗旨 STM32 只是一个硬件平台,同样地他可以换成MSP430,N ...
- 网络通信-在浏览器输入url,基于TCP/IP协议,浏览器渲染的解释
知识点1: 网络模型 TCP/IP四层 和ISO七层模型 (统一省略后面层字.比如传输代表传输层) 知识点2: 在应用层中TCP建立连接,经历的三次握手协议 首先:,TCP协议是什么? 为什么要三次握 ...
- 关于layui数据表格的各种事件
table.on('tool(demo)', function(obj){}):监听工具条事件,tool 是工具条事件名,demo 是 table 原始容器的属性 lay-filter="对 ...
- 小程序externalClasses介绍
小程序externalClasses 1.介绍:我们在封装组件的时候,有时候需要对外暴露出class,可以由调用者来决定组件中一部分的样式,此时就需要使用它了 // components/dong/i ...
- python实现简易工资管理系统(Salary Manage)源码
一.需求: 1.导入文件读取员工的信息和工资信息,最后将增加.删除或修改的员工工资信息写入原来的文件中 2.能够实现员工工资信息的增删改查 3.当增加和修改员工信息时用户用空格分隔员工姓名和薪资 4. ...
- HTML标签和属性二
五.文本标记 7.文本样式 <b></b> <strong></strong> 加粗 <i></i> <em> ...
- MySQL的转义字符“\”
\0 一个ASCII 0 (NUL)字符. \n 一个新行符. \t 一个定位符. \r 一个回车符. \b 一个退格符. \' ...
- Form action 方法上传文件
<form method="post" id="form1" runat="server" enctype="multipa ...