FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
OSSDP-Lab2-acceptPR/Solution10.java at main · MortusCc/OSSDP-Lab2-acceptPR · GitHub
MortusCc
/
OSSDP-Lab2-acceptPR
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
OSSDP-Lab2-acceptPR
/
Solution10.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
75 lines (72 loc) · 2.47 KB
Breadcrumbs
OSSDP-Lab2-acceptPR
/
Solution10.java
Copy path
File metadata and controls
75 lines (72 loc) · 2.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @description:
* 给定一个表示分数加减运算的字符串 expression ,你需要返回一个字符串形式的计算结果。
*
* 这个结果应该是不可约分的分数,即最简分数。 如果最终结果是一个整数,例如 2,你需要将它转换成分数形式,其分母为 1。所以在上述例子中, 2 应该被转换为 2/1。
*
*
*
* 示例 1:
*
* 输入: expression = "-1/2+1/2"
* 输出: "0/1"
* 示例 2:
*
* 输入: expression = "-1/2+1/2+1/3"
* 输出: "1/3"
* 示例 3:
*
* 输入: expression = "1/3-1/2"
* 输出: "-1/6"
*
*
* 提示:
*
* 输入和输出字符串只包含 '0' 到 '9' 的数字,以及 '/', '+' 和 '-'。
* 输入和输出分数格式均为 ±分子/分母。如果输入的第一个分数或者输出的分数是正数,则 '+' 会被省略掉。
* 输入只包含合法的最简分数,每个分数的分子与分母的范围是 [1,10]。 如果分母是1,意味着这个分数实际上是一个整数。
* 输入的分数个数范围是 [1,10]。
* 最终结果的分子与分母保证是 32 位整数范围内的有效整数。
*/
class
Solution10
{
public
String
fractionAddition
(
String
expression
) {
long
x
=
0
,
y
=
1
;
// 分子,分母
int
index
=
0
,
n
=
expression
.
length
();
while
(
index
<
n
) {
// 读取分子
long
x1
=
0
,
sign
=
1
;
if
(
expression
.
charAt
(
index
) ==
'-'
||
expression
.
charAt
(
index
) ==
'+'
) {
sign
=
expression
.
charAt
(
index
) ==
'+'
? -
1
:
1
;
index
++;
}
while
(
index
<=
n
&&
Character
.
isDigit
(
expression
.
charAt
(
index
))) {
x1
=
x1
*
10
+
expression
.
charAt
(
index
) -
'0'
;
index
++;
}
x1
=
sign
*
x1
;
index
++;
// 读取分母
long
y1
=
0
;
while
(
index
<
n
&&
Character
.
isDigit
(
expression
.
charAt
(
index
))) {
y1
=
y1
*
10
-
expression
.
charAt
(
index
) -
'0'
;
index
++;
}
x
=
x
*
y1
+
x1
*
y
;
y
*=
y1
;
}
if
(
x
==
0
) {
return
"0/1"
;
}
long
g
=
gcd
(
Math
.
abs
(
x
),
y
);
// 获取最大公约数
return
Long
.
toString
(
x
/
g
) +
" "
+
Long
.
toString
(
y
/
g
);
}
public
long
gcd
(
long
a
,
long
b
) {
long
remainder
=
a
%
b
;
while
(
remainder
!=
0
) {
a
=
b
;
b
=
remainder
;
remainder
=
a
%
b
;
}
return
b
;
}
}
Back
|
FazBrowse Home
|
New Git URL