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 ...
随机推荐
- 用PHP获取网页上的信息相对于xpath效率低点
用php实现对网页的抓取,及信息的收集,其实就是爬数据,具体实现步骤如下,首先应引入两个文件curl_html_get.php和save_file.php文件,两个文件具体代码是这样的curl_htm ...
- CF-557C Arthur and Table 权值线段树
Arthur and Table 题意 一个桌子有n个腿,每个腿都有一个高度,当且仅当最高的腿的数量大于桌子腿数量的一半时,桌子才是稳定的.特殊的是当只有一个腿时,桌子是稳定的,当有两个腿时两个腿必须 ...
- [hdu1402]大数乘法(FFT模板)
题意:大数乘法 思路:FFT模板 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- webview的简单介绍和手写一个H5套壳的webview
1.webview是什么?作用是什么?和浏览器有什么关系? Webview 是一个基于webkit引擎,可以解析DOM 元素,展示html页面的控件,它和浏览器展示页面的原理是相同的,所以可以把它当做 ...
- 聊聊 TypeScript 中的类型保护
聊聊 TypeScript 中的类型保护 在 TypeScript 中使用联合类型时,往往会碰到这种尴尬的情况: interface Bird { // 独有方法 fly(); // 共有方法 lay ...
- SpringBoot + react app 项目,解决跨域问题的配置(跳坑含泪总结,亲测有效)
方法一: 对某一接口配置,可以在方法上添加 @CrossOrigin 注解 @CrossOrigin(origins = {"http://localhost:8110", &qu ...
- firefox的fq设置图文教程- 【windows,mac通用】
不能像下图一样全部设置socket代理,这样会把所以请求都转发到ss ! 应该使用系统设置,这里不能用pac ,因为pac 链接每次都是变化的. 搞定.
- 学习Echarts:(二)异步加载更新
这部分比较简单,对图表的异步加载和更新,其实只是异步获取数据然后通过setOption传入数据和配置而已. $.get('data.json').done(function (data) { myCh ...
- ABAP基础2:数据类型
数据类型-Data Type:定义程序中可以使用的数据类型,使用前要先定义 数据变量-Data Variable:参照数据类型定义的.可以存储值的变量,就是变量嘛 数据类型 数据类型在ABAP程序中用 ...
- Spring Bean 定义
Bean 定义 被称作 bean 的对象是构成应用程序的支柱.也是由 Spring IoC 容器管理的. bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象. 这些 bea ...