当前位置:网站首页>[leetcode] 592. Fraction addition and subtraction
[leetcode] 592. Fraction addition and subtraction
2022-07-27 13:42:00 【pass night】
subject
592. Fractional addition and subtraction
Given a string representing the addition and subtraction of fractions expression , You need to return a string calculation .
This result should be an irreducible score , namely Simplest fraction . If the final result is an integer , for example 2, You need to convert it into fractions , Its denominator is 1. So in the above example , 2 Should be converted to 2/1.
Example 1:
Input : expression = "-1/2+1/2"
Output : "0/1"
Example 2:
Input : expression = "-1/2+1/2+1/3"
Output : "1/3"
Example 3:
Input : expression = "1/3-1/2"
Output : "-1/6"
Tips :
- Input and output strings contain only
'0'To'9'The number of , as well as'/','+'and'-'. - The input and output fractional formats are
± molecular / The denominator. If the first score entered or the score output is a positive number , be'+'Will be omitted . - The input only contains legal Simplest fraction , Of each score molecular And The denominator The range is [1,10]. If the denominator is 1, Which means that the score is actually an integer .
- The range of scores entered is [1,10].
- final result The numerator and denominator of are guaranteed to be 32 Valid integers in the range of bit integers .
Ideas
- Traversal string , adopt
=-The symbol separates the string into multiple fractional operations - The method of fractional operation is :
- Divide fractions into numerators and denominators , And convert it to an integer
- The product of two denominators
- Carry out operations
- Apposition : Divide the numerator and denominator by their greatest common factor
Code
class Solution:
def fractionAddition(self, expression: str) -> str:
def calc(a: str, b: str, operator: str):
operands = a.split("/")
a1 = int(operands[0])
a2 = int(operands[1])
operands = b.split("/")
b1 = int(operands[0])
b2 = int(operands[1])
b3 = a2*b2
a3 = a1 * b2 + a2*b1 if operator == "+" else a1 * b2 - a2*b1
mod = math.gcd(a3,b3)
a3 //= mod
b3 //= mod
return F"{
a3}/{
b3}"
if expression[0] == "-": expression = "0/1"+expression
if len(expression) <3: return expression
i = 0
while i < len(expression) and expression[i] not in "+-": i += 1
ret = expression[:i]
while i < len(expression):
start = i
i+=1
while i < len(expression) and expression[i] not in "+-": i += 1
ret = calc(ret, expression[start+1:i], expression[start])
return ret
Complexity
- Time complexity : O ( n + log C ) O(n+\log C) O(n+logC)
- Spatial complexity : O ( 1 ) O(1) O(1)
边栏推荐
- Tencent cloud and the China Federation of industry released the research results of industrial AI quality inspection standardization to accelerate the intelligent transformation of manufacturing indus
- Redis summary: cache avalanche, cache breakdown, cache penetration and cache preheating, cache degradation
- 腾讯云联合中国工联院发布工业AI质检标准化研究成果加速制造业智能化转型
- Method of changing thread state
- Data enhancement in image processing
- 图像特征及提取
- Classification and application of slip rings
- Fiddler bag capturing Tool + night God simulator
- 数据库内核开发人员,值一个马克杯!!!
- 四大线程池简析
猜你喜欢

opencv图像的缩放平移及旋转

双料第一!

Common types of electric slip rings

Gray histogram

Cute image classification -- a general explanation of the article "what are the common flops in CNN model?"

期货手续费标准和保证金比例

Intranet penetration based on FRP -- SSH Remote connection to intranet server with the help of public server

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

echart折线图默认显示最后一个点以及纵向虚线
![[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (IX)](/img/cf/44b3983dd5d5f7b92d90d918215908.png)
[300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (IX)
随机推荐
浏览器内核模块组成
期货开户的条件和流程
Install the wireless network card driver
Application of responsibility chain model in transfer accurate valuation
Preliminary discussion on NetGen and Gmsh mesh generation of any multiple sub models of CAD based on osg+occ
SQL group by statement
产品经理经验谈100篇(十一)-策略产品经理:模型与方法论
SNMP (Simple Network Management Protocol)
51:第五章:开发admin管理服务:4:开发【新增admin账号,接口】;(只开发了【用户名+密码的,方式】;【@T…】注解控制事务;设置cookie时,是否需要使用URLEncoder去编码;)
7.26 simulation summary
52: Chapter 5: developing admin management services: 5: developing [paging query admin account list, interface]; (swagger's @apiparam(), annotate the method parameters; PageHelper paging plug-in; Inte
数据库内核开发人员,值一个马克杯!!!
滑环使用如何固定
字节跳动 AI Lab 总监李航:语言模型的过去、现在和未来
"Digital economy, science and technology for the good" talk about dry goods
How to fix the slip ring
OPPO 自研大规模知识图谱及其在数智工程中的应用
Image features and extraction
网络异常流量分析系统设计
Figure 8 shows you how to configure SNMP