【leetcode】640. Solve the Equation
题目如下:
解题思路:本题的思路就是解析字符串,然后是小学时候学的解方程的思想,以"2x+3x-6x+1=x+2",先把左右两边的x项和非x项进行合并,得到"-x+1=x+2",接下来就是移项,把x项移到左边,常数项移到右边,得到"2x=-1",最后的解就是x=-1/2。对于任意一个表达式ax+b = cx+d来说,最终都能得到解x=(d-b)/(a-c),这里要对(a-c)是否为0做判断,同时根据(d-b)是否为0等到Infinite solutions,No solution,常规解三种结果。
代码如下:
class Solution(object):
def parse(self,expression):
x,v = 0,0
if expression[0] == '-':
expression = '' + expression
item = ''
operators = ['+', '-']
operator = ''
for i in (expression + '#'):
if i in operators or i == '#':
if item == 'x':
item = '1x'
if operator == '':
operator = i
if item[-1] == 'x':
x = int(item[:-1])
else:
v = int(item)
item = ''
else:
if operator == '+' and item[-1] == 'x':
x += int(item[:-1])
elif operator == '-' and item[-1] == 'x':
x -= int(item[:-1])
elif operator == '+' and item[-1] != 'x':
v += int(item)
else:
v -= int(item)
item = ''
operator = i
else:
item += i
return x,v
def solveEquation(self, equation):
"""
:type equation: str
:rtype: str
"""
left,right = equation.split('=')
lx,lv = self.parse(left)
rx,rv = self.parse(right) if lx - rx == 0 and rv - lv == 0:
return "Infinite solutions"
elif lx - rx == 0 and rv - lv != 0:
return "No solution"
else:
return "x=" + str((rv - lv)/(lx - rx))
【leetcode】640. Solve the Equation的更多相关文章
- 【LeetCode】640. Solve the Equation 解题报告(Python)
[LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【Leetcode】Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)
[LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】692. Top K Frequent Words 解题报告(Python)
[LeetCode]692. Top K Frequent Words 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/top ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- HTTP Error 500.30 - ANCM In-Process Start Failure
环境 windown 10 IIS 10 net core 2.2 vs2019 背景 在vs2019使用net core 2.2发布到IIS 10上(见在 ASP.NET Core 中使用多个环境) ...
- 【leetcode】331. Verify Preorder Serialization of a Binary Tree
题目如下: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null ...
- spring boot jar的支持
- 【Dart学习】--之Duration相关方法总结
一,概述 Duration表示从一个时间点到另一个时间点的时间差 如果是一个较晚的时间点和一个较早的时间点,Duration可能是负数 二,创建Duration 唯一的构造函数创建Duration对象 ...
- mysql查看sql执行情况的几种方法
mysql系统变量分为全局变量和会话变量,全局变量的修改影响到整个服务器,会话变量修改只影响当前的会话. 查看log日志是否开启 show variables like 'general_log' s ...
- codeforces gym100418J
题目简述 给定N 求1到N中有多少个幸运数字 幸运数字的定义为 这个数能被它二进制表示下1的个数整除 其中(1 ≤ N ≤ 1019) -------------------------------- ...
- 关于vsftp出现Restarting vsftpd (via systemctl): Job for vsftpd.service failed because the control 的解决办法
转载于:http://blog.csdn.net/it_dream_er/article/details/50783111 刚刚在搭建ftp服务器时,在配置好一切的参数之后,在我重启时,出现了无法启动 ...
- appium常见问题11_小米手机初次启动app,报错255“Requires permission android.permission.WRITE_SECURE_SETTINGS”
问题: 新申请的测试机到啦,申请机型是小米9.打开开发者模式.USB调试后,连接电脑,准备跑一下自动化脚本.但是在pycharm中点击run后,出现报错,报错code:255,提示“Requires ...
- B. pSort
题目链接: http://codeforces.com/problemset/problem/28/B 题意: 给一个n,原本有个1到n按顺序排列的序列,给一个序列问,在给一个数组,表示这个位置的数可 ...
- LeetCode 求众数 II
题目链接:https://leetcode-cn.com/problems/majority-element-ii/ 题目大意: 略. 分析: k个一起删, 最后check一下即可. 代码如下: #d ...