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 ...
随机推荐
- RabbitMQ的使用(二)- RabbitMQ服务在单机中做集群
RabbitMQ的使用(二)- RabbitMQ服务在单机中做集群 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/128371 ...
- input在IOS中的聚焦问题
关于input输入框在iPhone手机中的聚焦问题,开发中是会经常遇到的,在一般的浏览器中,我们一般是通过 document.getElementById('opop').focus(); 来获取焦点 ...
- Matlab中 awgn 函数输入参数带有‘measured’ 时snr的含义
MATLAB中awgn 函数可以为输入信号x 添加一定大小的噪声. out = awgn(in,snr,'measured'); 是一种常见的使用方法,意思是在添加噪声前先测量一下输入信号的功率,再 ...
- Elasticsearchdump 数据导入/导出
一.安装过程 Elasticsearchdump 仓库地址,详细使用情况 当前工具主要是用来对ES中的数据进行数据导入/导出,以及对数据迁移相关,使用elasticdump工具需要使用到npm,所以需 ...
- ql的python学习之路-day15
前言:本节主要讲解的是文件路径 在实际的软件开发中会设计一个项目的文件目录,按照执行包bin.配置包config.核心包core等来设计,在执行包里面要运行核心包里的主程序mian,由于不在同一级的目 ...
- HTTP请求格式
HTTP请求格式 URL包含:/index/index2?a=1&b=2:路径和参数都在这里. 请求头部: · content-length表示请求体里面的数据长度: · ...
- 06.drf(django)的权限
默认配置已经启用权限控制 settings 'django.contrib.auth', 默认 migrate 会给每个模型赋予4个权限,如果 ORM 类不托管给django管理,而是直接在数据库中建 ...
- Windows Terminal安装并美化
介绍 Windows Teminal是一款新式.快速.高效.强大的终端应用程序,适用于命令行工具.命令提示符.PowerShell.WSL(Linux子系统)等等的Shell用户,主要功能包括多选项卡 ...
- 苏浪浪 201771010120 《面向对象程序设计(java)》第七章学习总结
第七周 1.实验目的与要求 (1)进一步理解4个成员访问权限修饰符的用途: (2)掌握Object类的常用API用法: (3)掌握ArrayList类用法与常用API: (4)掌握枚举类使用方法: ( ...
- Docker 错误:network xxx id xxxx has active endpoints
问题描述:Docker Compose 部署的项目,使用docker-compose down 命令关闭时,提示错误: Removing network xxxl_default ERROR: net ...