640. 求解方程

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

如果方程没有解,请返回“No solution”。

如果方程有无限解,则返回“Infinite solutions”。

如果方程中只有一个解,要保证返回值 x 是一个整数。

  1. 示例 1
  2. 输入: "x+5-3+x=6+x-2"
  3. 输出: "x=2"
  4. 示例 2:
  5. 输入: "x=x"
  6. 输出: "Infinite solutions"
  7. 示例 3:
  8. 输入: "2x=x"
  9. 输出: "x=0"
  10. 示例 4:
  11. 输入: "2x+3x-6x=x+2"
  12. 输出: "x=-1"
  13. 示例 5:
  14. 输入: "x=x+2"
  15. 输出: "No solution"

PS:

标记一下正负,再用一个指针指到数字的第一位

  1. class Solution {
  2. public String solveEquation(String equation) {
  3. int coff = 0, sum = 0, index = 0, sign = 1;
  4. int n = equation.length();
  5. for(int i=0;i<n;i++){
  6. char c = equation.charAt(i);
  7. if(c == '='){
  8. if(index < i){
  9. sum += Integer.valueOf(equation.substring(index, i)) * sign;
  10. }
  11. sign = -1;
  12. index = i + 1;
  13. }else if(c == 'x'){
  14. if(index == i || equation.charAt(i - 1) == '+'){
  15. coff += sign;
  16. }else if(equation.charAt(i - 1) == '-'){
  17. coff -= sign;
  18. }else{
  19. coff += Integer.valueOf(equation.substring(index, i)) * sign;
  20. }
  21. index = i+1;
  22. }else if(c == '-' || c == '+'){
  23. if(index < i){
  24. sum += Integer.valueOf(equation.substring(index, i)) * sign;
  25. }
  26. index = i;
  27. }
  28. }
  29. if(index < n){
  30. sum += Integer.valueOf(equation.substring(index)) * sign;
  31. }
  32. if(sum == 0 && coff == 0) return "Infinite solutions";
  33. if(coff == 0) return "No solution";
  34. return "x=" + String.valueOf(-sum / coff);
  35. }
  36. }

Java实现 LeetCode 640 求解方程(计算器的加减法计算)的更多相关文章

  1. Leetcode 640.求解方程

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

  2. Java实现 LeetCode 762 二进制表示中质数个计算置位(位运算+JDK的方法)

    762. 二进制表示中质数个计算置位 给定两个整数 L 和 R ,找到闭区间 [L, R] 范围内,计算置位位数为质数的整数个数. (注意,计算置位代表二进制表示中1的个数.例如 21 的二进制表示 ...

  3. 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 ...

  4. 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. ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. OpenWrt(LEDE)2020.4.12编译 UnPnP+NAS+多拨+网盘+DNS优化+帕斯沃 无缝集成

    固件说明 基于Lede OpenWrt R2020.4.8版本(源码截止2020.4.12)Lienol Feed及若干自行维护的软件包 结合家庭x86软路由场景需要定制 按照家庭应用场景对固件及软件 ...

  2. [hdu5319]二进制表示,简单模拟

    题意:给一个矩形,矩形里面画了4种符号,'.'表示没画线,'R'表示画了红线,'B'表示画了蓝线,'G'表示红线和蓝线同时画了,并且矩形主对角线上只能画红线,副对角线上只能画蓝线,问最少画多少条线才能 ...

  3. jstree 反选,测试400条数据左右有点卡

    $("#reversecheckallmachines").on("change", function () { var checkedNodes = []; ...

  4. select 标签的数据绑定

    修改数据的页面 进入页面绑定select的值 会value绑定但是没有显示相应的option <script> $("#id option[value=${item.decora ...

  5. Android fragment 使用replace并保存状态

    Fragment的地位在开发中可是举足轻重的,掌握它的的生命周期以及使用特性是非常重要的,例如在开发中常使用的模板: 点击菜单,中心内容跟随菜单变化,但是在菜单间切换时,需要保存之前输入的信息或其他状 ...

  6. c#与js客户端之间相互传值

    RegisterStartupScript(key, script) RegisterClientScriptBlock(key, script) 第一个参数 key 是插入的客户端脚本的唯一标识符. ...

  7. 修改jupyter默认保存文件的目录

    1.打开 Anaconda Prompt输入命令 jupyter notebook --generate-config 2.可以看到生成了目录及jupyter notebook的配置文件,打开该文件. ...

  8. RabbitMQ及延时队列

    一.简介 我用过RabbirMQ的发布订阅模式,以及一对一的延迟队列. 1.RabbitMQ的有消息确认机制,消费一条则队列中少一条,也有对应的消费到消息及认为是消费成功这样的模式,一般使用前者. 发 ...

  9. elasticsearch 小总结

    elasticsearch 小总结 0. 起因 距离初次写关于es的文章 https://blog.csdn.net/aca_jingru/article/details/44488703 已经过去4 ...

  10. 关于键盘事件-查询:有多个input框,任意一个支持enter键查询

    应用场景:同一个界面有多个input框支持任意一个Enter查询. 实现:在input框中添加onkeypress="函数名()". 函数里面编写对应键盘code值,在里面直接调用 ...